| 197 |
Kevin |
1 |
#include <xc.h>
|
| 202 |
Kevin |
2 |
#include <string.h>
|
|
|
3 |
#include "main.h"
|
|
|
4 |
#include "TIMER.h"
|
|
|
5 |
#include "INTERRUPT.h"
|
|
|
6 |
#include "UART.h"
|
|
|
7 |
#include "I2C.h"
|
| 223 |
Kevin |
8 |
#include "L3G.h"
|
|
|
9 |
#include "LSM303.h"
|
| 224 |
Kevin |
10 |
#include "RN-42.h"
|
|
|
11 |
#include "MAX17040.h"
|
| 197 |
Kevin |
12 |
|
| 202 |
Kevin |
13 |
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
|
| 197 |
Kevin |
14 |
// CONFIG1
|
|
|
15 |
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
|
|
|
16 |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
|
|
|
17 |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
|
|
|
18 |
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
|
|
|
19 |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
|
|
20 |
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
|
|
21 |
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
|
|
22 |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
|
|
23 |
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
|
|
|
24 |
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
|
|
|
25 |
|
|
|
26 |
// CONFIG2
|
|
|
27 |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
| 202 |
Kevin |
28 |
#pragma config PLLEN = ON // PLL Enable (4x PLL disabled)
|
| 197 |
Kevin |
29 |
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
|
|
|
30 |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
|
| 202 |
Kevin |
31 |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
|
|
|
32 |
// </editor-fold>
|
| 197 |
Kevin |
33 |
|
| 202 |
Kevin |
34 |
UART_DATA uart_data;
|
|
|
35 |
I2C_DATA i2c_data;
|
|
|
36 |
|
| 222 |
Kevin |
37 |
char LED_R_ON = 0;
|
|
|
38 |
char LED_G_ON = 0;
|
|
|
39 |
char LED_B_ON = 0;
|
| 223 |
Kevin |
40 |
char timer_2_pwm = 0;
|
| 224 |
Kevin |
41 |
char connected = 0;
|
| 222 |
Kevin |
42 |
|
| 202 |
Kevin |
43 |
void Error(char ID) {
|
|
|
44 |
while(1) {
|
|
|
45 |
for (char i = 0; i < ID; i++) {
|
| 222 |
Kevin |
46 |
LED_R_ON = 1;
|
| 202 |
Kevin |
47 |
__delay_ms(120);
|
| 222 |
Kevin |
48 |
LED_R_ON = 0;
|
| 202 |
Kevin |
49 |
__delay_ms(150);
|
|
|
50 |
}
|
|
|
51 |
__delay_ms(1000);
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
void Startup_Check(void) {
|
|
|
56 |
char buffer[20];
|
|
|
57 |
char result, length;
|
|
|
58 |
|
| 222 |
Kevin |
59 |
// BLE_RESET_LAT = 0;
|
|
|
60 |
// __delay_ms(3000);
|
|
|
61 |
// BLE_RESET_LAT = 1;
|
|
|
62 |
// __delay_ms(200);
|
| 202 |
Kevin |
63 |
|
| 222 |
Kevin |
64 |
// // Check BLE Module
|
|
|
65 |
// length = UART_Read(buffer);
|
|
|
66 |
// if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
|
|
|
67 |
// Error(1);
|
|
|
68 |
// }
|
|
|
69 |
// UART_Write("AT\r", 3);
|
|
|
70 |
// __delay_ms(10);
|
|
|
71 |
// length = UART_Read(buffer);
|
|
|
72 |
// if (memcmp(buffer, "\r\nOK\r\n", 6)) {
|
|
|
73 |
// Error(1);
|
|
|
74 |
// }
|
| 202 |
Kevin |
75 |
|
|
|
76 |
// Check Battery Gauge
|
|
|
77 |
I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
|
|
|
78 |
do {
|
|
|
79 |
result = I2C_Get_Status();
|
|
|
80 |
} while (!result);
|
|
|
81 |
if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
|
|
|
82 |
Error(2);
|
|
|
83 |
}
|
|
|
84 |
length = I2C_Read_Buffer(buffer);
|
|
|
85 |
if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
|
|
|
86 |
Error(2);
|
|
|
87 |
}
|
| 222 |
Kevin |
88 |
|
| 202 |
Kevin |
89 |
// Check Gyroscope
|
|
|
90 |
I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
|
|
|
91 |
do {
|
|
|
92 |
result = I2C_Get_Status();
|
|
|
93 |
} while (!result);
|
|
|
94 |
if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
|
|
|
95 |
Error(3);
|
|
|
96 |
}
|
|
|
97 |
length = I2C_Read_Buffer(buffer);
|
|
|
98 |
if ((buffer[0] != 0xD4) || (length != 1)) {
|
|
|
99 |
Error(3);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Check Accelerometer
|
|
|
103 |
I2C_Master_Restart(ADDRESS_ACCL, 0x20, 1);
|
|
|
104 |
do {
|
|
|
105 |
result = I2C_Get_Status();
|
|
|
106 |
} while (!result);
|
|
|
107 |
if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
|
|
|
108 |
Error(4);
|
|
|
109 |
}
|
|
|
110 |
length = I2C_Read_Buffer(buffer);
|
|
|
111 |
if ((buffer[0] != 0x07) || (length != 1)) {
|
|
|
112 |
Error(4);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// Check Magnometer
|
|
|
116 |
I2C_Master_Restart(ADDRESS_MAGN, 0x0A, 1);
|
|
|
117 |
do {
|
|
|
118 |
result = I2C_Get_Status();
|
|
|
119 |
} while (!result);
|
|
|
120 |
if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
|
|
|
121 |
Error(4);
|
|
|
122 |
}
|
|
|
123 |
length = I2C_Read_Buffer(buffer);
|
|
|
124 |
if ((buffer[0] != 0x48) || (length != 1)) {
|
|
|
125 |
Error(4);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
| 223 |
Kevin |
129 |
void Timer_2_Callback(void) {
|
|
|
130 |
// Here we manually 'PWM' the LEDs
|
|
|
131 |
// Note: this is terribly inefficient but we need to do this
|
|
|
132 |
// otherwise we will blow out the blue LED (max 10mA)
|
|
|
133 |
if (timer_2_pwm == 0) {
|
|
|
134 |
LED_R_LAT = (LED_R_ON) ? 0 : 1;
|
|
|
135 |
LED_G_LAT = (LED_G_ON) ? 0 : 1;
|
|
|
136 |
LED_B_LAT = (LED_B_ON) ? 0 : 1;
|
|
|
137 |
}
|
|
|
138 |
if (timer_2_pwm == LED_R_MAX_BRIGHTNESS) {
|
|
|
139 |
LED_R_LAT = 1;
|
|
|
140 |
}
|
|
|
141 |
if (timer_2_pwm == LED_G_MAX_BRIGHTNESS) {
|
|
|
142 |
LED_G_LAT = 1;
|
|
|
143 |
}
|
|
|
144 |
if (timer_2_pwm == LED_B_MAX_BRIGHTNESS) {
|
|
|
145 |
LED_B_LAT = 1;
|
|
|
146 |
}
|
|
|
147 |
timer_2_pwm++;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
void Timer_1_Callback(void) {
|
|
|
151 |
// int A_X,A_Y,A_Z;
|
|
|
152 |
// int G_X,G_Y,G_Z;
|
|
|
153 |
// int M_X,M_Y,M_Z;
|
|
|
154 |
|
|
|
155 |
// LSM303_Read_Accl(&A_X, &A_Y, &A_Z);
|
|
|
156 |
// L3G_Read_Gyro(&G_X, &G_Y, &G_Z);
|
|
|
157 |
// LSM303_Read_Magn(&output[6], &output[7], &output[8]);
|
|
|
158 |
|
|
|
159 |
// UART_Write("Hello", 5);
|
|
|
160 |
}
|
|
|
161 |
|
| 197 |
Kevin |
162 |
int main() {
|
| 202 |
Kevin |
163 |
|
| 222 |
Kevin |
164 |
OSCCON = 0xF0; // Software PLL enabled, 32MHz
|
| 202 |
Kevin |
165 |
ANSELA = 0;
|
|
|
166 |
ANSELB = 0;
|
|
|
167 |
ANSELC = 0;
|
|
|
168 |
|
| 222 |
Kevin |
169 |
LED_R_TRIS = 0;
|
|
|
170 |
LED_R_LAT = 1;
|
|
|
171 |
LED_G_TRIS = 0;
|
|
|
172 |
LED_G_LAT = 1;
|
|
|
173 |
LED_B_TRIS = 0;
|
|
|
174 |
LED_B_LAT = 1;
|
| 202 |
Kevin |
175 |
|
|
|
176 |
BLE_RESET_TRIS = 0;
|
| 222 |
Kevin |
177 |
BLE_RESET_LAT = 1;
|
| 202 |
Kevin |
178 |
|
| 222 |
Kevin |
179 |
UART_CTS_TRIS = 1;
|
|
|
180 |
UART_RTS_TRIS = 0;
|
|
|
181 |
UART_RTS_LAT = 0;
|
| 202 |
Kevin |
182 |
|
| 222 |
Kevin |
183 |
// Initialize all peripherals
|
| 223 |
Kevin |
184 |
TIMER_1_Init(&Timer_1_Callback);
|
|
|
185 |
TIMER_2_Init(&Timer_2_Callback);
|
| 202 |
Kevin |
186 |
UART_Init(&uart_data);
|
|
|
187 |
I2C_Init(&i2c_data);
|
|
|
188 |
INTERRUPT_Init();
|
|
|
189 |
|
| 223 |
Kevin |
190 |
I2C_Configure_Master(I2C_400KHZ);
|
| 202 |
Kevin |
191 |
INTERRUPT_Enable();
|
| 222 |
Kevin |
192 |
// TIMER_1_Start();
|
|
|
193 |
TIMER_2_Start();
|
|
|
194 |
|
|
|
195 |
// A small delay is needed for the sensors to start up
|
| 224 |
Kevin |
196 |
__delay_ms(1000);
|
| 222 |
Kevin |
197 |
|
| 223 |
Kevin |
198 |
// Run a check to ensure that all sensors are properly connected
|
| 202 |
Kevin |
199 |
Startup_Check();
|
|
|
200 |
|
| 223 |
Kevin |
201 |
// Initialze the sensors
|
|
|
202 |
L3G_Init();
|
|
|
203 |
LSM303_Init();
|
| 224 |
Kevin |
204 |
RN42_Init();
|
|
|
205 |
MAX17040_Init();
|
| 202 |
Kevin |
206 |
|
| 224 |
Kevin |
207 |
char output[21] = {0};
|
|
|
208 |
output[20] = '\n';
|
| 223 |
Kevin |
209 |
|
| 224 |
Kevin |
210 |
// char len, buffer[30];
|
|
|
211 |
|
| 223 |
Kevin |
212 |
LED_B_ON = 1;
|
| 224 |
Kevin |
213 |
|
|
|
214 |
while(1) {
|
|
|
215 |
__delay_ms(10);
|
| 223 |
Kevin |
216 |
|
| 224 |
Kevin |
217 |
// len = UART_Read(buffer);
|
|
|
218 |
// if (len != 0) {
|
|
|
219 |
// if (!strncmp(buffer, "!CONNECT", 8)) {
|
|
|
220 |
// connected = 1;
|
|
|
221 |
// LED_G_ON = 1;
|
|
|
222 |
// }
|
|
|
223 |
// if (!strncmp(buffer, "!DISCONNECT", 11)) {
|
|
|
224 |
// connected = 0;
|
|
|
225 |
// LED_G_ON = 0;
|
|
|
226 |
// }
|
|
|
227 |
// }
|
| 223 |
Kevin |
228 |
|
| 224 |
Kevin |
229 |
// if (connected) {
|
|
|
230 |
LSM303_Read_Accl(&output[0], &output[1], &output[2], &output[3], &output[4], &output[5]);
|
|
|
231 |
L3G_Read_Gyro(&output[6], &output[7], &output[8], &output[9], &output[10], &output[11]);
|
|
|
232 |
LSM303_Read_Magn(&output[12], &output[13], &output[14], &output[15], &output[16], &output[17]);
|
|
|
233 |
MAX17040_Read_Batt(&output[18], &output[19]);
|
| 223 |
Kevin |
234 |
|
| 224 |
Kevin |
235 |
UART_Write((char *)&output[0], 21);
|
|
|
236 |
// }
|
| 223 |
Kevin |
237 |
|
|
|
238 |
// LED_B_ON = 0;
|
|
|
239 |
// LED_R_ON = 1;
|
|
|
240 |
// __delay_ms(250);
|
|
|
241 |
// LED_R_ON = 0;
|
|
|
242 |
// LED_G_ON = 1;
|
|
|
243 |
// __delay_ms(250);
|
|
|
244 |
// LED_G_ON = 0;
|
|
|
245 |
// LED_B_ON = 1;
|
|
|
246 |
// __delay_ms(250);
|
| 222 |
Kevin |
247 |
}
|
|
|
248 |
}
|