| 283 |
Kevin |
1 |
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
|
|
|
2 |
// PIC16F1825 Configuration Bit Settings
|
|
|
3 |
|
|
|
4 |
// CONFIG1
|
|
|
5 |
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
|
|
|
6 |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled)
|
|
|
7 |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
|
|
|
8 |
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
|
|
|
9 |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
|
|
10 |
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
|
|
11 |
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
|
|
12 |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
|
|
13 |
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
|
|
|
14 |
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
|
|
|
15 |
|
|
|
16 |
// CONFIG2
|
|
|
17 |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
|
|
18 |
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
|
|
|
19 |
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
|
|
|
20 |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
|
|
|
21 |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
|
|
|
22 |
// </editor-fold>
|
|
|
23 |
|
|
|
24 |
#include "defines.h"
|
|
|
25 |
#include "INTERRUPTS.h"
|
|
|
26 |
#include "PWM.h"
|
|
|
27 |
#include "I2C1.h"
|
| 285 |
Kevin |
28 |
#include "UART.h"
|
| 283 |
Kevin |
29 |
#include "CONTROLLER.h"
|
|
|
30 |
|
|
|
31 |
void Pins_Init(void) {
|
|
|
32 |
// Set all pins to digital I/O
|
|
|
33 |
ANSELA = 0x0;
|
|
|
34 |
ANSELC = 0x0;
|
|
|
35 |
|
|
|
36 |
// // Enable weak pull-up if WPU bit is set
|
|
|
37 |
// OPTION_REGbits.nWPUEN = 0;
|
|
|
38 |
|
|
|
39 |
// CCP2 on RC3
|
|
|
40 |
APFCON1bits.CCP2SEL = 0;
|
| 285 |
Kevin |
41 |
|
|
|
42 |
// TX/CK function on RA0
|
|
|
43 |
APFCON0bits.TXCKSEL = 1;
|
|
|
44 |
// RX/DT function on RA1
|
|
|
45 |
APFCON0bits.RXDTSEL = 1;
|
| 283 |
Kevin |
46 |
|
|
|
47 |
LED_1_LAT = 1;
|
|
|
48 |
LED_1_TRIS = 0;
|
|
|
49 |
LED_2_LAT = 0;
|
|
|
50 |
LED_2_TRIS = 0;
|
|
|
51 |
|
|
|
52 |
CCP_1_LAT = 0;
|
|
|
53 |
CCP_1_TRIS = 0;
|
|
|
54 |
CCP_2_LAT = 0;
|
|
|
55 |
CCP_2_TRIS = 0;
|
|
|
56 |
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
void LED_1_On(void) {
|
|
|
60 |
LED_1_LAT = 0;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
void LED_1_Off(void) {
|
|
|
64 |
LED_1_LAT = 1;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
void LED_2_On(void) {
|
|
|
68 |
LED_2_LAT = 1;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
void LED_2_Off(void) {
|
|
|
72 |
LED_2_LAT = 0;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
int main(void) {
|
|
|
76 |
// Set internal oscillator speed to 32MHz
|
|
|
77 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
78 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
79 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
80 |
|
|
|
81 |
// Initialize I/O
|
|
|
82 |
Pins_Init();
|
|
|
83 |
|
|
|
84 |
// Initialize PWM
|
|
|
85 |
PWM_1_Init();
|
|
|
86 |
PWM_2_Init();
|
|
|
87 |
|
|
|
88 |
// Initialize I2C
|
|
|
89 |
I2C1_DATA i2c_data;
|
|
|
90 |
I2C1_Init(&i2c_data);
|
|
|
91 |
I2C1_Configure_Master(I2C_100KHZ);
|
|
|
92 |
|
| 285 |
Kevin |
93 |
UART_DATA uart_data;
|
|
|
94 |
UART_Init(&uart_data);
|
|
|
95 |
|
| 283 |
Kevin |
96 |
Controller_Init();
|
|
|
97 |
|
|
|
98 |
// Initialize interrupts
|
|
|
99 |
Interrupt_Init();
|
|
|
100 |
Interrupt_Enable();
|
|
|
101 |
|
|
|
102 |
PWM_1_Set(PWM_NOMINAL);
|
|
|
103 |
PWM_2_Set(PWM_NOMINAL);
|
|
|
104 |
|
|
|
105 |
LED_1_On();
|
|
|
106 |
LED_2_On();
|
|
|
107 |
|
|
|
108 |
__delay_ms(1000);
|
|
|
109 |
|
|
|
110 |
LED_1_Off();
|
|
|
111 |
LED_2_Off();
|
|
|
112 |
|
|
|
113 |
CTRL_BTN_STATUS ctrl;
|
|
|
114 |
Controller_Read(&ctrl);
|
|
|
115 |
|
|
|
116 |
uint16_t pwm_left = PWM_NOMINAL;
|
|
|
117 |
uint16_t pwm_right = PWM_NOMINAL;
|
|
|
118 |
|
| 286 |
Kevin |
119 |
uint8_t median;
|
|
|
120 |
int16_t L_X, L_Y, R_X, R_Y;
|
|
|
121 |
|
| 285 |
Kevin |
122 |
uint8_t buffer[32];
|
|
|
123 |
uint8_t length;
|
|
|
124 |
uint8_t uart_state = UART_STATE_READ_CMD;
|
|
|
125 |
uint8_t uart_cmd;
|
|
|
126 |
|
| 283 |
Kevin |
127 |
while (1) {
|
|
|
128 |
if (!Controller_Read(&ctrl)) {
|
|
|
129 |
median = ctrl.REF / 2;
|
|
|
130 |
L_X = (int16_t)median - (int16_t)ctrl.L_X_CH;
|
|
|
131 |
L_Y = (int16_t)median - (int16_t)ctrl.L_Y_CH;
|
|
|
132 |
R_X = (int16_t)median - (int16_t)ctrl.R_X_CH;
|
|
|
133 |
R_Y = (int16_t)median - (int16_t)ctrl.R_Y_CH;
|
|
|
134 |
|
|
|
135 |
// Left stick = throttle, Right stick = steering
|
|
|
136 |
// if (L_X > -5 && L_X < 5 &&
|
|
|
137 |
// R_Y > -5 && R_Y < 5) {
|
|
|
138 |
// pwm_left = PWM_NOMINAL;
|
|
|
139 |
// pwm_right = PWM_NOMINAL;
|
|
|
140 |
// } else {
|
|
|
141 |
// pwm_left = PWM_NOMINAL + (L_X * 4);
|
|
|
142 |
// pwm_right = PWM_NOMINAL + (L_X * 4);
|
|
|
143 |
//
|
|
|
144 |
// pwm_left += R_Y * 4;
|
|
|
145 |
// pwm_right -= R_Y * 4;
|
|
|
146 |
// }
|
|
|
147 |
|
|
|
148 |
// Left stick = left control, Right stick = right control
|
|
|
149 |
if (L_X > -5 && L_X < 5 &&
|
|
|
150 |
R_X > -5 && R_X < 5) {
|
|
|
151 |
pwm_left = PWM_NOMINAL;
|
|
|
152 |
pwm_right = PWM_NOMINAL;
|
|
|
153 |
} else {
|
|
|
154 |
pwm_left = PWM_NOMINAL + (R_X * 4);
|
|
|
155 |
pwm_right = PWM_NOMINAL + (L_X * 4);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if (L_X < -5 || L_X > 5) LED_2_On();
|
|
|
159 |
else LED_2_Off();
|
|
|
160 |
|
|
|
161 |
if (R_X < -5 || R_X > 5) LED_1_On();
|
|
|
162 |
else LED_1_Off();
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
if (pwm_left > PWM_MAX)
|
|
|
166 |
pwm_left = PWM_MAX;
|
|
|
167 |
if (pwm_left < PWM_MIN)
|
|
|
168 |
pwm_left = PWM_MIN;
|
|
|
169 |
|
|
|
170 |
if (pwm_right > PWM_MAX)
|
|
|
171 |
pwm_right = PWM_MAX;
|
|
|
172 |
if (pwm_right < PWM_MIN)
|
|
|
173 |
pwm_right = PWM_MIN;
|
|
|
174 |
|
|
|
175 |
PWM_1_Set(pwm_left);
|
|
|
176 |
PWM_2_Set(pwm_right);
|
|
|
177 |
} else {
|
|
|
178 |
PWM_1_Set(PWM_NOMINAL);
|
|
|
179 |
PWM_2_Set(PWM_NOMINAL);
|
|
|
180 |
|
|
|
181 |
LED_1_Off();
|
|
|
182 |
LED_2_Off();
|
| 286 |
Kevin |
183 |
|
|
|
184 |
// If no controller is attached, switch control to UART
|
|
|
185 |
while(1) {
|
|
|
186 |
length = UART_Read(buffer);
|
|
|
187 |
for (uint8_t i = 0; i < length; i++) {
|
|
|
188 |
if (uart_state == UART_STATE_READ_CMD) {
|
|
|
189 |
if (buffer[i] == UART_CMD_RESET) {
|
|
|
190 |
RESET();
|
|
|
191 |
} else {
|
|
|
192 |
// Read and save first byte (command)
|
|
|
193 |
uart_cmd = buffer[i];
|
|
|
194 |
uart_state = UART_STATE_READ_DATA;
|
|
|
195 |
}
|
|
|
196 |
} else if (uart_state == UART_STATE_READ_DATA) {
|
|
|
197 |
uart_state = UART_STATE_READ_CMD;
|
|
|
198 |
|
|
|
199 |
// Process received data
|
|
|
200 |
if (uart_cmd == UART_CMD_LEFT_FORWARD) {
|
|
|
201 |
pwm_left = PWM_NOMINAL + (uint16_t) (buffer[i] * 2);
|
|
|
202 |
if (buffer[i] != 0) LED_2_On();
|
|
|
203 |
else LED_2_Off();
|
|
|
204 |
} else if (uart_cmd == UART_CMD_LEFT_BACKWARD) {
|
|
|
205 |
pwm_left = PWM_NOMINAL - (uint16_t) (buffer[i] * 2);
|
|
|
206 |
if (buffer[i] != 0) LED_2_On();
|
|
|
207 |
else LED_2_Off();
|
|
|
208 |
} else if (uart_cmd == UART_CMD_RIGHT_FORWARD) {
|
|
|
209 |
pwm_right = PWM_NOMINAL + (uint16_t) (buffer[i] * 2);
|
|
|
210 |
if (buffer[i] != 0) LED_1_On();
|
|
|
211 |
else LED_1_Off();
|
|
|
212 |
} else if (uart_cmd == UART_CMD_RIGHT_BACKWARD) {
|
|
|
213 |
pwm_right = PWM_NOMINAL - (uint16_t) (buffer[i] * 2);
|
|
|
214 |
if (buffer[i] != 0) LED_1_On();
|
|
|
215 |
else LED_1_Off();
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
if (pwm_left > PWM_MAX)
|
|
|
219 |
pwm_left = PWM_MAX;
|
|
|
220 |
if (pwm_left < PWM_MIN)
|
|
|
221 |
pwm_left = PWM_MIN;
|
|
|
222 |
|
|
|
223 |
if (pwm_right > PWM_MAX)
|
|
|
224 |
pwm_right = PWM_MAX;
|
|
|
225 |
if (pwm_right < PWM_MIN)
|
|
|
226 |
pwm_right = PWM_MIN;
|
|
|
227 |
|
|
|
228 |
PWM_1_Set(pwm_right);
|
|
|
229 |
PWM_2_Set(pwm_left);
|
|
|
230 |
}
|
|
|
231 |
}
|
|
|
232 |
}
|
| 283 |
Kevin |
233 |
}
|
|
|
234 |
}
|
|
|
235 |
}
|