| 119 |
Kevin |
1 |
#include "maindefs.h"
|
|
|
2 |
#include "interrupts.h"
|
|
|
3 |
#include "uart.h"
|
| 120 |
Kevin |
4 |
#include "i2c.h"
|
| 121 |
Kevin |
5 |
#include "spi.h"
|
|
|
6 |
#include "nfc.h"
|
|
|
7 |
#include "led_backpack.h"
|
|
|
8 |
#include "oled_ssd1306.h"
|
|
|
9 |
#include "oled_ssd1331.h"
|
| 123 |
Kevin |
10 |
#include "adc.h"
|
| 127 |
Kevin |
11 |
#include "xbee.h"
|
| 119 |
Kevin |
12 |
#include <delays.h>
|
| 121 |
Kevin |
13 |
#include <string.h>
|
| 119 |
Kevin |
14 |
|
|
|
15 |
#pragma config WDTEN = OFF // Turn off watchdog timer
|
|
|
16 |
#pragma config XINST = OFF // Turn off extended instruction set
|
| 127 |
Kevin |
17 |
#ifdef USE_EXT_OSC
|
| 119 |
Kevin |
18 |
#pragma config OSC = HSPLL // Use external oscillator (101)
|
|
|
19 |
#pragma config PLLDIV = 3 // Set PPL prescaler to 3 (to get 4MHz)
|
| 127 |
Kevin |
20 |
#else
|
|
|
21 |
#pragma config OSC = INTOSCPLL // Use internal oscillator
|
|
|
22 |
#pragma config PLLDIV = 2 // Set PPL prescaler to 2 (to get 4MHz)
|
|
|
23 |
#endif
|
| 119 |
Kevin |
24 |
#pragma config CFGPLLEN = ON // Enable PLL on startup
|
|
|
25 |
#pragma config PLLSEL = PLL96 // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
|
|
|
26 |
//#pragma config SOSCSEL = HIGH // High Power T1OSC/SOSC circuit selected
|
| 126 |
Kevin |
27 |
//#pragma config ADCSEL = BIT12 // 12-bit ADrC
|
| 119 |
Kevin |
28 |
//#pragma config IOL1WAY = OFF // IOLOCK bit can be set and cleared as needed
|
|
|
29 |
|
| 121 |
Kevin |
30 |
#ifdef _TEST_UART
|
| 119 |
Kevin |
31 |
|
|
|
32 |
void main(void) {
|
| 121 |
Kevin |
33 |
unsigned char length = 0;
|
|
|
34 |
unsigned char buffer[100];
|
|
|
35 |
|
|
|
36 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
37 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
38 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
39 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
40 |
/* -------------------------------------------------------------------- */
|
|
|
41 |
|
|
|
42 |
// Set all ports as digial I/O
|
|
|
43 |
ANCON0 = 0xFF;
|
|
|
44 |
ANCON1 = 0x1F;
|
|
|
45 |
|
|
|
46 |
UART1_Init(); // Initialize the UART handler code
|
|
|
47 |
|
|
|
48 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
49 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
50 |
|
|
|
51 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
52 |
|
|
|
53 |
while (1) {
|
|
|
54 |
|
| 127 |
Kevin |
55 |
length = UART1_Read_Buffer((unsigned char *) buffer);
|
| 121 |
Kevin |
56 |
if (length != 0) {
|
|
|
57 |
UART1_WriteB((char *) buffer, length);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
Delay10KTCYx(255);
|
|
|
61 |
Delay10KTCYx(255);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
#endif
|
|
|
65 |
|
|
|
66 |
#ifdef _TEST_I2C_MASTER
|
|
|
67 |
|
|
|
68 |
void main(void) {
|
| 119 |
Kevin |
69 |
unsigned char i = 0;
|
|
|
70 |
unsigned char length = 0;
|
| 120 |
Kevin |
71 |
unsigned char result = 0;
|
| 119 |
Kevin |
72 |
unsigned char buffer[100];
|
| 121 |
Kevin |
73 |
|
| 119 |
Kevin |
74 |
/* --------------------- Oscillator Configuration --------------------- */
|
| 121 |
Kevin |
75 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
76 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
| 119 |
Kevin |
77 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
78 |
/* -------------------------------------------------------------------- */
|
|
|
79 |
|
|
|
80 |
// Set all ports as digial I/O
|
|
|
81 |
ANCON0 = 0xFF;
|
|
|
82 |
ANCON1 = 0x1F;
|
|
|
83 |
|
|
|
84 |
UART1_Init(); // Initialize the UART handler code
|
| 120 |
Kevin |
85 |
I2C_Init(); // Initialize the I2C handler code
|
| 121 |
Kevin |
86 |
|
|
|
87 |
I2C_Configure_Master();
|
|
|
88 |
|
| 119 |
Kevin |
89 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
90 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
91 |
|
|
|
92 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
93 |
|
| 121 |
Kevin |
94 |
while (1) {
|
|
|
95 |
|
|
|
96 |
buffer[0] = 0xBB;
|
|
|
97 |
|
|
|
98 |
I2C_Master_Send(0x30, 1, buffer);
|
|
|
99 |
result = I2C_Get_Status();
|
|
|
100 |
while (!result) {
|
|
|
101 |
result = I2C_Get_Status();
|
|
|
102 |
}
|
|
|
103 |
DBG_PRINT_MAIN("S:%X ", result);
|
|
|
104 |
|
|
|
105 |
I2C_Master_Recv(0x30, 2);
|
|
|
106 |
result = I2C_Get_Status();
|
|
|
107 |
while (!result) {
|
|
|
108 |
result = I2C_Get_Status();
|
|
|
109 |
}
|
|
|
110 |
DBG_PRINT_MAIN("S:%X ", result);
|
|
|
111 |
length = I2C_Read_Buffer(buffer);
|
|
|
112 |
DBG_PRINT_MAIN("L:%d D:", length);
|
|
|
113 |
for (i = 0; i < length; i++) {
|
|
|
114 |
DBG_PRINT_MAIN("%c ", buffer[i]);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
I2C_Master_Restart(0x30, 0xBB, 2);
|
|
|
118 |
result = I2C_Get_Status();
|
|
|
119 |
while (!result) {
|
|
|
120 |
result = I2C_Get_Status();
|
|
|
121 |
}
|
|
|
122 |
DBG_PRINT_MAIN("S:%X ", result);
|
|
|
123 |
length = I2C_Read_Buffer(buffer);
|
|
|
124 |
DBG_PRINT_MAIN("L:%d D:", length);
|
|
|
125 |
for (i = 0; i < length; i++) {
|
|
|
126 |
DBG_PRINT_MAIN("%c ", buffer[i]);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
DBG_PRINT_MAIN("\r\n");
|
|
|
130 |
|
|
|
131 |
Delay10KTCYx(255);
|
|
|
132 |
Delay10KTCYx(255);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
#endif
|
|
|
136 |
|
|
|
137 |
#ifdef _TEST_I2C_SLAVE
|
|
|
138 |
|
|
|
139 |
void main(void) {
|
|
|
140 |
unsigned char i = 0;
|
|
|
141 |
unsigned char length = 0;
|
|
|
142 |
unsigned char result = 0;
|
|
|
143 |
unsigned char buffer[100];
|
|
|
144 |
|
|
|
145 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
146 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
147 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
148 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
149 |
/* -------------------------------------------------------------------- */
|
|
|
150 |
|
|
|
151 |
// Set all ports as digial I/O
|
|
|
152 |
ANCON0 = 0xFF;
|
|
|
153 |
ANCON1 = 0x1F;
|
|
|
154 |
|
|
|
155 |
UART1_Init(); // Initialize the UART handler code
|
|
|
156 |
I2C_Init(); // Initialize the I2C handler code
|
|
|
157 |
|
| 120 |
Kevin |
158 |
I2C_Configure_Slave(0x30);
|
|
|
159 |
|
| 121 |
Kevin |
160 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
161 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
162 |
|
|
|
163 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
164 |
|
| 119 |
Kevin |
165 |
while (1) {
|
| 120 |
Kevin |
166 |
|
|
|
167 |
result = I2C_Get_Status();
|
|
|
168 |
while (!result) {
|
|
|
169 |
result = I2C_Get_Status();
|
| 119 |
Kevin |
170 |
}
|
| 120 |
Kevin |
171 |
DBG_PRINT_MAIN("%X ", result);
|
|
|
172 |
length = I2C_Read_Buffer(buffer);
|
|
|
173 |
DBG_PRINT_MAIN("%d ", length);
|
|
|
174 |
for (i = 0; i < length; i++) {
|
|
|
175 |
DBG_PRINT_MAIN("%X ", buffer[i]);
|
|
|
176 |
}
|
|
|
177 |
DBG_PRINT_MAIN("\r\n");
|
| 121 |
Kevin |
178 |
|
|
|
179 |
Delay10KTCYx(255);
|
|
|
180 |
Delay10KTCYx(255);
|
| 119 |
Kevin |
181 |
}
|
| 121 |
Kevin |
182 |
}
|
|
|
183 |
#endif
|
|
|
184 |
|
|
|
185 |
#ifdef _TEST_NFC
|
|
|
186 |
|
|
|
187 |
void main(void) {
|
| 126 |
Kevin |
188 |
unsigned char i, length = 0;
|
| 121 |
Kevin |
189 |
|
|
|
190 |
// NFC stuff
|
| 126 |
Kevin |
191 |
NFC_FIRMWARE_VERSION version;
|
|
|
192 |
NFC_TargetDataMiFare cardData[2];
|
|
|
193 |
NFC_TargetDataMiFare cardData_prev[2];
|
| 121 |
Kevin |
194 |
|
|
|
195 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
196 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
197 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
198 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
199 |
/* -------------------------------------------------------------------- */
|
|
|
200 |
|
|
|
201 |
// Set all ports as digial I/O
|
|
|
202 |
ANCON0 = 0xFF;
|
|
|
203 |
ANCON1 = 0x1F;
|
|
|
204 |
|
|
|
205 |
UART1_Init(); // Initialize the UART handler code
|
|
|
206 |
I2C_Init(); // Initialize the I2C handler code
|
|
|
207 |
NFC_Init(); // Initialize the NFC chip (uses I2C)
|
|
|
208 |
|
|
|
209 |
I2C_Configure_Master(I2C_400KHZ);
|
|
|
210 |
|
|
|
211 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
212 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
213 |
|
|
|
214 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
215 |
|
|
|
216 |
version = NFC_getFirmwareVersion();
|
|
|
217 |
while (!version.IC) {
|
|
|
218 |
DBG_PRINT_MAIN("Waiting for NFC board..\r\n");
|
|
|
219 |
Delay10KTCYx(3);
|
|
|
220 |
version = NFC_getFirmwareVersion();
|
|
|
221 |
}
|
|
|
222 |
DBG_PRINT_MAIN("Found chip PN5%X\r\n", version.IC);
|
|
|
223 |
DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
|
|
|
224 |
NFC_SAMConfig();
|
|
|
225 |
|
| 126 |
Kevin |
226 |
memset(cardData, 0, 24);
|
| 127 |
Kevin |
227 |
|
| 121 |
Kevin |
228 |
while (1) {
|
|
|
229 |
|
| 127 |
Kevin |
230 |
// // This query will hang until the NFC chip replies (card detected)
|
|
|
231 |
// length = NFC_readPassiveTargetID(cardData);
|
|
|
232 |
// if (length) {
|
|
|
233 |
// DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
|
|
|
234 |
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
|
|
|
235 |
// DBG_PRINT_MAIN("UID: ");
|
|
|
236 |
// for (i = 0; i < cardData[0].NFCID_LEN; i++) {
|
|
|
237 |
// DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
|
|
|
238 |
// }
|
|
|
239 |
// DBG_PRINT_MAIN("\r\n");
|
|
|
240 |
// if (length == 2) {
|
|
|
241 |
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
|
|
|
242 |
// DBG_PRINT_MAIN("UID: ");
|
|
|
243 |
// for (i = 0; i < cardData[1].NFCID_LEN; i++) {
|
|
|
244 |
// DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
|
|
|
245 |
// }
|
|
|
246 |
// DBG_PRINT_MAIN("\r\n");
|
|
|
247 |
// }
|
|
|
248 |
// }
|
|
|
249 |
|
| 129 |
Kevin |
250 |
// // This query will hang until the NFC chip replies (card detected)
|
|
|
251 |
// length = NFC_readPassiveTargetID(cardData);
|
|
|
252 |
// if (length) {
|
|
|
253 |
// DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
|
|
|
254 |
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
|
|
|
255 |
// DBG_PRINT_MAIN("UID: ");
|
|
|
256 |
// for (i = 0; i < cardData[0].NFCID_LEN; i++) {
|
|
|
257 |
// DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
|
|
|
258 |
// }
|
|
|
259 |
// DBG_PRINT_MAIN("\r\n");
|
|
|
260 |
// if (length == 2) {
|
|
|
261 |
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
|
|
|
262 |
// DBG_PRINT_MAIN("UID: ");
|
|
|
263 |
// for (i = 0; i < cardData[1].NFCID_LEN; i++) {
|
|
|
264 |
// DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
|
|
|
265 |
// }
|
|
|
266 |
// DBG_PRINT_MAIN("\r\n");
|
|
|
267 |
// }
|
|
|
268 |
// }
|
|
|
269 |
|
| 126 |
Kevin |
270 |
// This query will not wait for a detection before responding
|
|
|
271 |
length = NFC_pollTargets(1, 1, cardData);
|
|
|
272 |
if (!length) {
|
|
|
273 |
memset(cardData_prev, 0, 24);
|
|
|
274 |
} else if (length == 1) {
|
|
|
275 |
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
|
|
|
276 |
// Do nothing
|
|
|
277 |
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
|
| 127 |
Kevin |
278 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
| 126 |
Kevin |
279 |
} else {
|
|
|
280 |
DBG_PRINT_MAIN("UID: ");
|
|
|
281 |
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
|
|
|
282 |
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
|
|
|
283 |
}
|
|
|
284 |
DBG_PRINT_MAIN("\r\n");
|
| 127 |
Kevin |
285 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
| 121 |
Kevin |
286 |
}
|
| 126 |
Kevin |
287 |
memset(&cardData_prev[1], 0, 12);
|
|
|
288 |
} else if (length == 2) {
|
|
|
289 |
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
|
|
|
290 |
memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
|
|
|
291 |
// Do nothing
|
|
|
292 |
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
|
|
|
293 |
memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
|
| 127 |
Kevin |
294 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
|
|
295 |
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
|
| 126 |
Kevin |
296 |
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
|
|
|
297 |
// First card matched
|
|
|
298 |
DBG_PRINT_MAIN("UID2: ");
|
|
|
299 |
for (i = 0; i < cardData[1].NFCID_LEN; i++) {
|
|
|
300 |
DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
|
|
|
301 |
}
|
|
|
302 |
DBG_PRINT_MAIN("\r\n");
|
| 127 |
Kevin |
303 |
memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
|
| 126 |
Kevin |
304 |
} else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
|
|
|
305 |
// Second card matched
|
|
|
306 |
DBG_PRINT_MAIN("UID1: ");
|
|
|
307 |
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
|
|
|
308 |
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
|
|
|
309 |
}
|
|
|
310 |
DBG_PRINT_MAIN("\r\n");
|
| 127 |
Kevin |
311 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
| 126 |
Kevin |
312 |
} else {
|
|
|
313 |
// No match
|
|
|
314 |
DBG_PRINT_MAIN("UID1: ");
|
|
|
315 |
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
|
|
|
316 |
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
|
|
|
317 |
}
|
|
|
318 |
DBG_PRINT_MAIN("\r\n");
|
| 127 |
Kevin |
319 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
| 126 |
Kevin |
320 |
DBG_PRINT_MAIN("UID2: ");
|
|
|
321 |
for (i = 0; i < cardData[1].NFCID_LEN; i++) {
|
|
|
322 |
DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
|
|
|
323 |
}
|
|
|
324 |
DBG_PRINT_MAIN("\r\n");
|
| 127 |
Kevin |
325 |
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
|
| 126 |
Kevin |
326 |
}
|
| 121 |
Kevin |
327 |
}
|
|
|
328 |
}
|
|
|
329 |
}
|
|
|
330 |
#endif
|
|
|
331 |
|
|
|
332 |
#ifdef _TEST_LED_BACKPACK
|
|
|
333 |
|
|
|
334 |
void main(void) {
|
|
|
335 |
unsigned char i = 0;
|
|
|
336 |
unsigned char length = 0;
|
|
|
337 |
unsigned char buffer[100];
|
|
|
338 |
unsigned int counter = 0;
|
|
|
339 |
|
|
|
340 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
341 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
342 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
343 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
344 |
/* -------------------------------------------------------------------- */
|
|
|
345 |
|
|
|
346 |
// Set all ports as digial I/O
|
|
|
347 |
ANCON0 = 0xFF;
|
|
|
348 |
ANCON1 = 0x1F;
|
|
|
349 |
|
|
|
350 |
UART1_Init(); // Initialize the UART handler code
|
|
|
351 |
I2C_Init(); // Initialize the I2C handler code
|
|
|
352 |
LED_Init(); // Initialize the LED backpack (uses I2C);
|
|
|
353 |
|
|
|
354 |
I2C_Configure_Master(I2C_400KHZ);
|
|
|
355 |
|
|
|
356 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
357 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
358 |
|
|
|
359 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
360 |
|
|
|
361 |
LED_Start();
|
|
|
362 |
LED_writeDigitNum(0, 1, 1);
|
|
|
363 |
LED_writeDigitNum(1, 2, 0);
|
|
|
364 |
LED_writeDigitNum(2, 3, 0);
|
|
|
365 |
LED_writeDigitNum(3, 4, 0);
|
|
|
366 |
LED_writeDisplay();
|
|
|
367 |
for (i = 0; i < 15; i++) {
|
|
|
368 |
LED_setBrightness(15 - i);
|
|
|
369 |
Delay10KTCYx(100);
|
|
|
370 |
}
|
|
|
371 |
for (i = 0; i < 15; i++) {
|
|
|
372 |
LED_setBrightness(i);
|
|
|
373 |
Delay10KTCYx(100);
|
|
|
374 |
}
|
|
|
375 |
LED_blinkRate(HT16K33_BLINK_OFF);
|
|
|
376 |
|
|
|
377 |
while (1) {
|
|
|
378 |
LED_writeNum(counter);
|
|
|
379 |
counter++;
|
|
|
380 |
if (counter > 9999)
|
|
|
381 |
counter = 0;
|
|
|
382 |
|
|
|
383 |
// Delay10KTCYx(255);
|
|
|
384 |
}
|
|
|
385 |
}
|
|
|
386 |
#endif
|
|
|
387 |
|
|
|
388 |
#ifdef _TEST_SPI
|
|
|
389 |
|
|
|
390 |
void main(void) {
|
|
|
391 |
unsigned char i = 0;
|
|
|
392 |
unsigned char length = 0;
|
|
|
393 |
unsigned char result = 0;
|
|
|
394 |
unsigned char buffer[100];
|
|
|
395 |
unsigned char test[8] = "ASDF123";
|
|
|
396 |
|
|
|
397 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
398 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
399 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
400 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
401 |
/* -------------------------------------------------------------------- */
|
|
|
402 |
|
|
|
403 |
// Set all ports as digial I/O
|
|
|
404 |
ANCON0 = 0xFF;
|
|
|
405 |
ANCON1 = 0x1F;
|
|
|
406 |
|
|
|
407 |
UART1_Init(); // Initialize the UART handler code
|
|
|
408 |
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
|
|
|
409 |
|
|
|
410 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
411 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
412 |
|
|
|
413 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
414 |
|
|
|
415 |
while (1) {
|
|
|
416 |
|
|
|
417 |
SPI2_Write(test, 7);
|
|
|
418 |
while (result != 7) {
|
|
|
419 |
length = SPI2_Buffer_Read(buffer);
|
|
|
420 |
if (length) {
|
|
|
421 |
result += length;
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
result = 0;
|
|
|
425 |
|
|
|
426 |
for (i = 0; i < result; i++) {
|
|
|
427 |
DBG_PRINT_MAIN("%X ", buffer[i]);
|
|
|
428 |
}
|
|
|
429 |
DBG_PRINT_MAIN("\r\n");
|
|
|
430 |
|
|
|
431 |
Delay10KTCYx(255);
|
|
|
432 |
Delay10KTCYx(255);
|
|
|
433 |
}
|
|
|
434 |
}
|
|
|
435 |
#endif
|
|
|
436 |
|
|
|
437 |
#ifdef _TEST_SSD1306_OLED
|
|
|
438 |
|
|
|
439 |
void main(void) {
|
|
|
440 |
unsigned int i = 0;
|
|
|
441 |
unsigned long l = 0;
|
|
|
442 |
|
|
|
443 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
444 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
445 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
446 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
447 |
/* -------------------------------------------------------------------- */
|
|
|
448 |
|
|
|
449 |
// Set all ports as digial I/O
|
|
|
450 |
ANCON0 = 0xFF;
|
|
|
451 |
ANCON1 = 0x1F;
|
|
|
452 |
|
|
|
453 |
UART1_Init(); // Initialize the UART handler code
|
|
|
454 |
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
|
|
|
455 |
SSD1306_Init(); // Initialize the OLED code
|
|
|
456 |
|
|
|
457 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
458 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
459 |
|
|
|
460 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
461 |
|
|
|
462 |
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
|
|
|
463 |
|
|
|
464 |
SSD1306_Display(); // Show splashscreen
|
|
|
465 |
|
|
|
466 |
while (1) {
|
| 129 |
Kevin |
467 |
Delay10KTCYx(255);
|
|
|
468 |
Delay10KTCYx(255);
|
|
|
469 |
SSD1306_Clear_Display();
|
|
|
470 |
SSD1306_Test_DrawLine();
|
|
|
471 |
SSD1306_Display();
|
| 121 |
Kevin |
472 |
|
| 129 |
Kevin |
473 |
Delay10KTCYx(255);
|
|
|
474 |
Delay10KTCYx(255);
|
|
|
475 |
SSD1306_Clear_Display();
|
|
|
476 |
SSD1306_Test_DrawRect();
|
|
|
477 |
SSD1306_Display();
|
| 121 |
Kevin |
478 |
|
| 129 |
Kevin |
479 |
Delay10KTCYx(255);
|
|
|
480 |
Delay10KTCYx(255);
|
|
|
481 |
SSD1306_Clear_Display();
|
|
|
482 |
SSD1306_Test_FillRect();
|
|
|
483 |
SSD1306_Display();
|
| 121 |
Kevin |
484 |
|
| 129 |
Kevin |
485 |
Delay10KTCYx(255);
|
|
|
486 |
Delay10KTCYx(255);
|
|
|
487 |
SSD1306_Clear_Display();
|
|
|
488 |
SSD1306_Test_DrawCircle();
|
|
|
489 |
SSD1306_Display();
|
| 121 |
Kevin |
490 |
|
| 129 |
Kevin |
491 |
Delay10KTCYx(255);
|
|
|
492 |
Delay10KTCYx(255);
|
| 121 |
Kevin |
493 |
SSD1306_Clear_Display();
|
| 129 |
Kevin |
494 |
SSD1306_Fill_Circle(SSD1306_LCDWIDTH / 2, SSD1306_LCDHEIGHT / 2, 10, SSD1306_WHITE);
|
| 121 |
Kevin |
495 |
SSD1306_Display();
|
|
|
496 |
|
| 129 |
Kevin |
497 |
Delay10KTCYx(255);
|
|
|
498 |
Delay10KTCYx(255);
|
|
|
499 |
SSD1306_Clear_Display();
|
|
|
500 |
SSD1306_Test_DrawRoundRect();
|
|
|
501 |
SSD1306_Display();
|
|
|
502 |
|
|
|
503 |
Delay10KTCYx(255);
|
|
|
504 |
Delay10KTCYx(255);
|
|
|
505 |
SSD1306_Clear_Display();
|
|
|
506 |
SSD1306_Test_FillRoundRect();
|
|
|
507 |
SSD1306_Display();
|
|
|
508 |
|
|
|
509 |
Delay10KTCYx(255);
|
|
|
510 |
Delay10KTCYx(255);
|
|
|
511 |
SSD1306_Clear_Display();
|
|
|
512 |
SSD1306_Test_DrawTriangle();
|
|
|
513 |
SSD1306_Display();
|
|
|
514 |
|
|
|
515 |
Delay10KTCYx(255);
|
|
|
516 |
Delay10KTCYx(255);
|
|
|
517 |
SSD1306_Clear_Display();
|
|
|
518 |
SSD1306_Test_FillTriangle();
|
|
|
519 |
SSD1306_Display();
|
|
|
520 |
|
|
|
521 |
Delay10KTCYx(255);
|
|
|
522 |
Delay10KTCYx(255);
|
|
|
523 |
SSD1306_Clear_Display();
|
|
|
524 |
SSD1306_Test_DrawChar();
|
|
|
525 |
SSD1306_Display();
|
|
|
526 |
|
|
|
527 |
Delay10KTCYx(255);
|
|
|
528 |
Delay10KTCYx(255);
|
|
|
529 |
SSD1306_Clear_Display();
|
|
|
530 |
SSD1306_Set_Text_Size(1);
|
|
|
531 |
SSD1306_Set_Text_Color(SSD1306_WHITE);
|
|
|
532 |
SSD1306_Set_Cursor(0, 0);
|
|
|
533 |
SSD1306_Write_String("Hello World!\n");
|
|
|
534 |
// SSD1306_Set_Text_Color_BG(BLACK, WHITE);
|
|
|
535 |
i = 65535;
|
|
|
536 |
SSD1306_Write_String("%u %d\n", i, i);
|
|
|
537 |
// SSD1306_Set_Text_Size(2);
|
|
|
538 |
// SSD1306_Set_Text_Color(WHITE);
|
|
|
539 |
l = 0xDEADBEEF;
|
|
|
540 |
SSD1306_Write_String("0x%lX", (long) l);
|
|
|
541 |
SSD1306_Display();
|
|
|
542 |
|
|
|
543 |
// SSD1306_Clear_Display();
|
|
|
544 |
// SSD1306_Set_Rotation(0);
|
|
|
545 |
// SSD1306_Set_Text_Size(1);
|
|
|
546 |
// SSD1306_Set_Text_Color(SSD1306_WHITE);
|
|
|
547 |
// SSD1306_Set_Cursor(0, 0);
|
|
|
548 |
// SSD1306_Write_String("%u", i);
|
|
|
549 |
// i++;
|
|
|
550 |
// SSD1306_Display();
|
|
|
551 |
|
| 121 |
Kevin |
552 |
}
|
|
|
553 |
}
|
|
|
554 |
#endif
|
|
|
555 |
|
|
|
556 |
#ifdef _TEST_SSD1331_OLED
|
|
|
557 |
|
|
|
558 |
void main(void) {
|
|
|
559 |
unsigned int i = 0;
|
|
|
560 |
|
|
|
561 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
562 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
563 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
564 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
565 |
/* -------------------------------------------------------------------- */
|
|
|
566 |
|
|
|
567 |
// Set all ports as digial I/O
|
|
|
568 |
ANCON0 = 0xFF;
|
|
|
569 |
ANCON1 = 0x1F;
|
|
|
570 |
|
|
|
571 |
UART1_Init(); // Initialize the UART handler code
|
|
|
572 |
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
|
|
|
573 |
SSD1331_Init(); // Initialize the OLED code
|
|
|
574 |
|
|
|
575 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
576 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
577 |
|
|
|
578 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
579 |
|
|
|
580 |
SSD1331_Begin();
|
|
|
581 |
|
|
|
582 |
while (1) {
|
|
|
583 |
|
|
|
584 |
Delay10KTCYx(255);
|
|
|
585 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
586 |
SSD1331_Set_Rotation(0);
|
| 121 |
Kevin |
587 |
SSD1331_Test_Pattern();
|
|
|
588 |
|
|
|
589 |
Delay10KTCYx(255);
|
|
|
590 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
591 |
SSD1331_Clear_Display();
|
|
|
592 |
SSD1331_Set_Rotation(0);
|
|
|
593 |
SSD1331_Set_Cursor(0, 0);
|
|
|
594 |
SSD1331_Write_String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
|
| 121 |
Kevin |
595 |
|
|
|
596 |
Delay10KTCYx(255);
|
|
|
597 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
598 |
SSD1331_Clear_Display();
|
|
|
599 |
SSD1331_Set_Rotation(3);
|
|
|
600 |
SSD1331_Set_Cursor(0, 0);
|
|
|
601 |
SSD1331_Write_String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
|
| 121 |
Kevin |
602 |
|
|
|
603 |
Delay10KTCYx(255);
|
|
|
604 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
605 |
SSD1331_Set_Rotation(0);
|
| 121 |
Kevin |
606 |
SSD1331_Test_DrawLines(SSD1331_YELLOW);
|
|
|
607 |
|
|
|
608 |
Delay10KTCYx(255);
|
|
|
609 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
610 |
SSD1331_Set_Rotation(3);
|
| 121 |
Kevin |
611 |
SSD1331_Test_DrawLines(SSD1331_BLUE);
|
|
|
612 |
|
|
|
613 |
Delay10KTCYx(255);
|
|
|
614 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
615 |
SSD1331_Set_Rotation(0);
|
| 121 |
Kevin |
616 |
SSD1331_Test_DrawRect(SSD1331_GREEN);
|
|
|
617 |
|
|
|
618 |
Delay10KTCYx(255);
|
|
|
619 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
620 |
SSD1331_Set_Rotation(1);
|
| 121 |
Kevin |
621 |
SSD1331_Test_DrawRect(SSD1331_RED);
|
|
|
622 |
|
|
|
623 |
Delay10KTCYx(255);
|
|
|
624 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
625 |
SSD1331_Set_Rotation(2);
|
| 121 |
Kevin |
626 |
SSD1331_Test_DrawRect(SSD1331_BLUE);
|
|
|
627 |
|
|
|
628 |
Delay10KTCYx(255);
|
|
|
629 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
630 |
SSD1331_Set_Rotation(3);
|
| 121 |
Kevin |
631 |
SSD1331_Test_DrawRect(SSD1331_YELLOW);
|
|
|
632 |
|
|
|
633 |
Delay10KTCYx(255);
|
|
|
634 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
635 |
SSD1331_Set_Rotation(0);
|
| 121 |
Kevin |
636 |
SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
|
|
|
637 |
|
|
|
638 |
Delay10KTCYx(255);
|
|
|
639 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
640 |
SSD1331_Set_Rotation(3);
|
| 121 |
Kevin |
641 |
SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
|
|
|
642 |
|
|
|
643 |
Delay10KTCYx(255);
|
|
|
644 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
645 |
SSD1331_Set_Rotation(0);
|
|
|
646 |
SSD1331_Clear_Display();
|
| 121 |
Kevin |
647 |
SSD1331_Test_FillCircle(10, SSD1331_BLUE);
|
|
|
648 |
SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
|
|
|
649 |
|
|
|
650 |
Delay10KTCYx(255);
|
|
|
651 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
652 |
SSD1331_Set_Rotation(3);
|
|
|
653 |
SSD1331_Clear_Display();
|
| 121 |
Kevin |
654 |
SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
|
|
|
655 |
SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
|
|
|
656 |
|
|
|
657 |
Delay10KTCYx(255);
|
|
|
658 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
659 |
SSD1331_Set_Rotation(0);
|
| 121 |
Kevin |
660 |
SSD1331_Test_DrawTria();
|
|
|
661 |
|
|
|
662 |
Delay10KTCYx(255);
|
|
|
663 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
664 |
SSD1331_Set_Rotation(3);
|
| 121 |
Kevin |
665 |
SSD1331_Test_DrawTria();
|
|
|
666 |
|
|
|
667 |
Delay10KTCYx(255);
|
|
|
668 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
669 |
SSD1331_Set_Rotation(0);
|
| 121 |
Kevin |
670 |
SSD1331_Test_DrawRoundRect();
|
|
|
671 |
|
|
|
672 |
Delay10KTCYx(255);
|
|
|
673 |
Delay10KTCYx(255);
|
| 129 |
Kevin |
674 |
SSD1331_Set_Rotation(3);
|
| 121 |
Kevin |
675 |
SSD1331_Test_DrawRoundRect();
|
|
|
676 |
|
| 129 |
Kevin |
677 |
// SSD1331_Clear_Display();
|
|
|
678 |
// SSD1331_Set_Rotation(3);
|
|
|
679 |
// SSD1331_Set_Cursor(0,0);
|
|
|
680 |
// SSD1331_Set_Text_Color_BG(SSD1331_WHITE, SSD1331_BLACK);
|
|
|
681 |
// SSD1331_Write_String("%u", i);
|
| 121 |
Kevin |
682 |
// i++;
|
|
|
683 |
}
|
|
|
684 |
}
|
|
|
685 |
#endif
|
|
|
686 |
|
| 123 |
Kevin |
687 |
#ifdef _TEST_ADC
|
|
|
688 |
|
|
|
689 |
void main(void) {
|
| 126 |
Kevin |
690 |
unsigned int x, y, z;
|
| 123 |
Kevin |
691 |
unsigned char buffer[60];
|
|
|
692 |
|
|
|
693 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
694 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
695 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
696 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
697 |
/* -------------------------------------------------------------------- */
|
|
|
698 |
|
|
|
699 |
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
|
|
|
700 |
ANCON0 = 0xF8;
|
|
|
701 |
ANCON1 = 0x1F;
|
|
|
702 |
|
|
|
703 |
UART1_Init(); // Initialize the UART handler code
|
|
|
704 |
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
|
| 129 |
Kevin |
705 |
SSD1306_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
|
| 126 |
Kevin |
706 |
ADC_Init(ADC_TAD_20, ADC_FOSC_64);
|
| 123 |
Kevin |
707 |
|
|
|
708 |
// I2C_Configure_Master(I2C_400KHZ);
|
| 129 |
Kevin |
709 |
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
|
| 123 |
Kevin |
710 |
|
|
|
711 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
712 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
713 |
|
|
|
714 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
715 |
|
|
|
716 |
memset(buffer, 0, 60);
|
| 129 |
Kevin |
717 |
SSD1306_Clear_Display();
|
|
|
718 |
SSD1306_Display();
|
| 123 |
Kevin |
719 |
|
|
|
720 |
while (1) {
|
|
|
721 |
// ADC read from AN0-AN2 and prints to display
|
| 127 |
Kevin |
722 |
ADC_Start(ADC_CHANNEL_AN2);
|
| 129 |
Kevin |
723 |
// SSD1306_Fill_Rect(0, 0, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
|
|
|
724 |
SSD1306_Set_Cursor(0, 0);
|
| 123 |
Kevin |
725 |
while (!ADC_Get_Result(&x));
|
| 129 |
Kevin |
726 |
SSD1306_Write_String("X: %u", x);
|
|
|
727 |
SSD1306_Display();
|
| 123 |
Kevin |
728 |
|
|
|
729 |
ADC_Start(ADC_CHANNEL_AN1);
|
| 129 |
Kevin |
730 |
// SSD1306_Fill_Rect(0, 8, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
|
|
|
731 |
SSD1306_Set_Cursor(0, 8);
|
| 123 |
Kevin |
732 |
while (!ADC_Get_Result(&y));
|
| 129 |
Kevin |
733 |
SSD1306_Write_String("Y: %u", y);
|
|
|
734 |
SSD1306_Display();
|
| 123 |
Kevin |
735 |
|
| 127 |
Kevin |
736 |
ADC_Start(ADC_CHANNEL_AN0);
|
| 129 |
Kevin |
737 |
// SSD1306_Fill_Rect(0, 16, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
|
|
|
738 |
SSD1306_Set_Cursor(0, 16);
|
| 123 |
Kevin |
739 |
while (!ADC_Get_Result(&z));
|
| 129 |
Kevin |
740 |
SSD1306_Write_String("Z: %u", z);
|
|
|
741 |
SSD1306_Display();
|
| 123 |
Kevin |
742 |
}
|
|
|
743 |
}
|
|
|
744 |
|
|
|
745 |
#endif
|
|
|
746 |
|
| 127 |
Kevin |
747 |
#ifdef _TEST_XBEE
|
|
|
748 |
|
|
|
749 |
void main(void) {
|
|
|
750 |
unsigned int i, length = 0;
|
|
|
751 |
unsigned char buffer[100];
|
|
|
752 |
|
| 129 |
Kevin |
753 |
XBEE_RX_AT_COMMAND_RESPONSE_FRAME *rx_at_cmd_response_frame;
|
| 127 |
Kevin |
754 |
XBEE_RX_DATA_PACKET_FRAME *rx_data_frame;
|
| 129 |
Kevin |
755 |
XBEE_RX_DATA_TX_STATUS_FRAME *rx_tx_status_frame;
|
|
|
756 |
XBEE_RX_REMOTE_AT_COMMAND_FRAME *rx_remote_at_cmd_frame;
|
|
|
757 |
XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *rx_node_ident_frame;
|
|
|
758 |
XBEE_RX_MODEM_STATUS_FRAME *rx_modem_status_frame;
|
| 127 |
Kevin |
759 |
|
|
|
760 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
761 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
762 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
763 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
764 |
/* -------------------------------------------------------------------- */
|
|
|
765 |
|
|
|
766 |
// Set all ports as digial I/O
|
|
|
767 |
ANCON0 = 0xFF;
|
|
|
768 |
ANCON1 = 0x1F;
|
|
|
769 |
|
|
|
770 |
UART1_Init(); // Initialize the UART handler code
|
|
|
771 |
XBee_Init();
|
|
|
772 |
|
|
|
773 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
774 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
775 |
|
|
|
776 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
777 |
|
|
|
778 |
while (1) {
|
|
|
779 |
|
|
|
780 |
//#define _ROUTER
|
|
|
781 |
#define _COORDINATOR
|
|
|
782 |
|
|
|
783 |
#ifdef _ROUTER
|
|
|
784 |
XBEE_TX_DATA_PACKET_FRAME *tx_data_frame;
|
|
|
785 |
tx_data_frame = (void *) buffer;
|
|
|
786 |
tx_data_frame->frame_type = XBEE_TX_DATA_PACKET;
|
|
|
787 |
tx_data_frame->frame_id = 1;
|
|
|
788 |
tx_data_frame->destination_64.UPPER_32.long_value = 0x00000000;
|
|
|
789 |
tx_data_frame->destination_64.LOWER_32.long_value = 0x00000000;
|
|
|
790 |
tx_data_frame->destination_16.INT_16.int_value = 0xFEFF;
|
|
|
791 |
tx_data_frame->broadcast_radius = 0;
|
|
|
792 |
tx_data_frame->options = 0;
|
|
|
793 |
tx_data_frame->data[0] = 0x54;
|
|
|
794 |
tx_data_frame->data[1] = 0x78;
|
|
|
795 |
tx_data_frame->data[2] = 0x32;
|
|
|
796 |
tx_data_frame->data[3] = 0x43;
|
|
|
797 |
tx_data_frame->data[4] = 0x6F;
|
|
|
798 |
tx_data_frame->data[5] = 0x6F;
|
|
|
799 |
tx_data_frame->data[6] = 0x72;
|
|
|
800 |
tx_data_frame->data[7] = 0x11;
|
|
|
801 |
XBee_Process_Transmit_Frame(buffer, XBEE_TX_DATA_PACKET_FRAME_SIZE + 8);
|
| 129 |
Kevin |
802 |
|
| 127 |
Kevin |
803 |
Delay10KTCYx(255);
|
|
|
804 |
Delay10KTCYx(255);
|
|
|
805 |
Delay10KTCYx(255);
|
|
|
806 |
Delay10KTCYx(255);
|
|
|
807 |
Delay10KTCYx(255);
|
|
|
808 |
Delay10KTCYx(255);
|
|
|
809 |
Delay10KTCYx(255);
|
|
|
810 |
Delay10KTCYx(255);
|
|
|
811 |
#endif
|
|
|
812 |
|
|
|
813 |
#ifdef _COORDINATOR
|
|
|
814 |
length = XBee_Get_Received_Frame(buffer);
|
|
|
815 |
if (length != 0) {
|
|
|
816 |
switch (*(unsigned char *) buffer) {
|
|
|
817 |
case XBEE_RX_AT_COMMAND_RESPONSE:
|
|
|
818 |
DBG_PRINT_MAIN("XBEE: parsing recieved AT command response frame\r\n");
|
| 129 |
Kevin |
819 |
rx_at_cmd_response_frame = (void *) buffer;
|
|
|
820 |
DBG_PRINT_MAIN("Frame ID: %u\r\n", rx_at_cmd_response_frame->frame_id);
|
|
|
821 |
DBG_PRINT_MAIN("AT Command: %c%c Status: %02X\r\n", rx_at_cmd_response_frame->command[0], \\
|
|
|
822 |
rx_at_cmd_response_frame->command[1], rx_at_cmd_response_frame->command_status);
|
|
|
823 |
if (length > XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE) {
|
|
|
824 |
DBG_PRINT_MAIN("Command Data: ");
|
|
|
825 |
for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
|
|
|
826 |
DBG_PRINT_MAIN("%02X ", rx_at_cmd_response_frame->data[i]);
|
|
|
827 |
}
|
|
|
828 |
DBG_PRINT_MAIN("\r\n");
|
|
|
829 |
}
|
| 127 |
Kevin |
830 |
break;
|
|
|
831 |
case XBEE_RX_DATA_PACKET:
|
|
|
832 |
DBG_PRINT_MAIN("XBEE: parsing recieved data recieved frame\r\n");
|
| 129 |
Kevin |
833 |
rx_data_frame = (void *) buffer;
|
| 127 |
Kevin |
834 |
XBee_ConvertEndian64(&(rx_data_frame->source_64));
|
|
|
835 |
XBee_ConvertEndian16(&(rx_data_frame->source_16));
|
| 129 |
Kevin |
836 |
DBG_PRINT_MAIN("Source 64: %08lX %08lX Source 16: %04X Options: %02X\r\n", \\
|
| 127 |
Kevin |
837 |
rx_data_frame->source_64.UPPER_32.long_value, \\
|
|
|
838 |
rx_data_frame->source_64.LOWER_32.long_value, \\
|
|
|
839 |
rx_data_frame->source_16.INT_16.int_value, \\
|
|
|
840 |
rx_data_frame->recieve_options);
|
|
|
841 |
DBG_PRINT_MAIN("Data: ");
|
|
|
842 |
for (i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
|
|
|
843 |
DBG_PRINT_MAIN("%02X ", rx_data_frame->data[i]);
|
|
|
844 |
}
|
|
|
845 |
DBG_PRINT_MAIN("\r\n");
|
|
|
846 |
break;
|
|
|
847 |
case XBEE_RX_DATA_TX_STATUS:
|
|
|
848 |
DBG_PRINT_MAIN("XBEE: parsing recieved TX status frame\r\n");
|
| 129 |
Kevin |
849 |
rx_tx_status_frame = (void *) buffer;
|
|
|
850 |
XBee_ConvertEndian16(&(rx_tx_status_frame->destination_16));
|
|
|
851 |
DBG_PRINT_MAIN("Frame ID: %u Destination 16: %04X\r\n", \\
|
|
|
852 |
rx_tx_status_frame->frame_id, rx_tx_status_frame->destination_16.INT_16.int_value);
|
|
|
853 |
DBG_PRINT_MAIN("Transmit Retry Count: %02X Delivery Status: %02X Discovery Status: %02X\r\n", \\
|
|
|
854 |
rx_tx_status_frame->transmit_retry_count, rx_tx_status_frame->delivery_status, \\
|
|
|
855 |
rx_tx_status_frame->discovery_status);
|
| 127 |
Kevin |
856 |
break;
|
|
|
857 |
case XBEE_RX_IO_DATA_SAMPLE:
|
|
|
858 |
DBG_PRINT_MAIN("XBEE: parsing recieved IO data sample frame\r\n");
|
|
|
859 |
break;
|
|
|
860 |
case XBEE_RX_EXPLICIT_COMMAND:
|
|
|
861 |
DBG_PRINT_MAIN("XBEE: parsing recieved explicit command frame\r\n");
|
|
|
862 |
break;
|
|
|
863 |
case XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
|
|
|
864 |
DBG_PRINT_MAIN("XBEE: parsing recieved remote AT command frame\r\n");
|
| 129 |
Kevin |
865 |
rx_remote_at_cmd_frame = (void *) buffer;
|
| 127 |
Kevin |
866 |
break;
|
|
|
867 |
case XBEE_RX_ROUTE_RECORD:
|
|
|
868 |
DBG_PRINT_MAIN("XBEE: parsing recieved route record frame\r\n");
|
|
|
869 |
break;
|
|
|
870 |
case XBEE_RX_NODE_IDENTIFICATION:
|
|
|
871 |
DBG_PRINT_MAIN("XBEE: parsing recieved node identification frame\r\n");
|
| 129 |
Kevin |
872 |
rx_node_ident_frame = (void *) buffer;
|
|
|
873 |
XBee_ConvertEndian64(&(rx_node_ident_frame->source_64));
|
|
|
874 |
XBee_ConvertEndian16(&(rx_node_ident_frame->source_16));
|
|
|
875 |
XBee_ConvertEndian64(&(rx_node_ident_frame->remote_64));
|
|
|
876 |
XBee_ConvertEndian16(&(rx_node_ident_frame->remote_16));
|
|
|
877 |
XBee_ConvertEndian16(&(rx_node_ident_frame->parent_16));
|
|
|
878 |
DBG_PRINT_MAIN("Source 64: %08lX %08lX Source 16: %04X Options: %02X\r\n", \\
|
|
|
879 |
rx_node_ident_frame->source_64.UPPER_32.long_value, \\
|
|
|
880 |
rx_node_ident_frame->source_64.LOWER_32.long_value, \\
|
|
|
881 |
rx_node_ident_frame->source_16.INT_16.int_value, \\
|
|
|
882 |
rx_node_ident_frame->recieve_options);
|
|
|
883 |
DBG_PRINT_MAIN("Remote 64: %08lX %08lX Remote 16: %04X Parent 16: %04X\r\n", \\
|
|
|
884 |
rx_node_ident_frame->remote_64.UPPER_32.long_value, \\
|
|
|
885 |
rx_node_ident_frame->remote_64.LOWER_32.long_value, \\
|
|
|
886 |
rx_node_ident_frame->remote_16.INT_16.int_value, \\
|
|
|
887 |
rx_node_ident_frame->parent_16.INT_16.int_value);
|
|
|
888 |
DBG_PRINT_MAIN("Device Type: %02X Source Event: %02X\r\n", \\
|
|
|
889 |
rx_node_ident_frame->device_type, rx_node_ident_frame->source_event);
|
| 127 |
Kevin |
890 |
break;
|
|
|
891 |
case XBEE_RX_FRAME_MODEM_STATUS:
|
|
|
892 |
DBG_PRINT_MAIN("XBEE: parsing recieved modem status frame\r\n");
|
| 129 |
Kevin |
893 |
rx_modem_status_frame = (void *) buffer;
|
|
|
894 |
DBG_PRINT_MAIN("Status: %02X\r\n", rx_modem_status_frame->status);
|
| 127 |
Kevin |
895 |
break;
|
|
|
896 |
default:
|
|
|
897 |
DBG_PRINT_MAIN("??\r\n");
|
|
|
898 |
break;
|
|
|
899 |
}
|
|
|
900 |
}
|
|
|
901 |
#endif
|
|
|
902 |
|
|
|
903 |
}
|
|
|
904 |
}
|
|
|
905 |
#endif
|
|
|
906 |
|
| 121 |
Kevin |
907 |
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
|
|
|
908 |
!defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
|
|
|
909 |
!defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
|
| 123 |
Kevin |
910 |
!defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
|
| 127 |
Kevin |
911 |
!defined(_TEST_ADC) && !defined(_TEST_XBEE)
|
| 121 |
Kevin |
912 |
|
|
|
913 |
void main(void) {
|
|
|
914 |
unsigned char length = 0;
|
|
|
915 |
|
| 127 |
Kevin |
916 |
// NFC stuff
|
|
|
917 |
NFC_FIRMWARE_VERSION version;
|
|
|
918 |
NFC_TargetDataMiFare cardData[2];
|
|
|
919 |
NFC_TargetDataMiFare cardData_prev[2];
|
|
|
920 |
|
| 121 |
Kevin |
921 |
/* --------------------- Oscillator Configuration --------------------- */
|
|
|
922 |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
|
|
|
923 |
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
|
|
|
924 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
|
|
|
925 |
/* -------------------------------------------------------------------- */
|
|
|
926 |
|
| 123 |
Kevin |
927 |
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
|
|
|
928 |
ANCON0 = 0xF8;
|
| 121 |
Kevin |
929 |
ANCON1 = 0x1F;
|
|
|
930 |
|
| 127 |
Kevin |
931 |
UART1_Init();
|
|
|
932 |
I2C_Init();
|
|
|
933 |
NFC_Init();
|
|
|
934 |
SPI2_Init(SPI2_FOSC_8);
|
| 129 |
Kevin |
935 |
SSD1306_Init();
|
| 121 |
Kevin |
936 |
|
| 126 |
Kevin |
937 |
I2C_Configure_Master(I2C_400KHZ);
|
| 127 |
Kevin |
938 |
|
| 121 |
Kevin |
939 |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
|
|
|
940 |
interrupt_init(); // Initialize the interrupt priorities
|
|
|
941 |
|
|
|
942 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
|
|
|
943 |
|
| 129 |
Kevin |
944 |
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
|
| 127 |
Kevin |
945 |
memset(cardData, 0, 24);
|
|
|
946 |
memset(cardData_prev, 0, 24);
|
| 129 |
Kevin |
947 |
SSD1306_Clear_Display();
|
|
|
948 |
SSD1306_Set_Rotation(0);
|
|
|
949 |
SSD1306_Set_Cursor(0, 0);
|
| 122 |
Kevin |
950 |
|
| 127 |
Kevin |
951 |
version = NFC_getFirmwareVersion();
|
|
|
952 |
while (!version.IC) {
|
| 129 |
Kevin |
953 |
SSD1306_Write_String("Waiting for NFC board..\r");
|
|
|
954 |
SSD1306_Display();
|
| 127 |
Kevin |
955 |
Delay10KTCYx(3);
|
|
|
956 |
version = NFC_getFirmwareVersion();
|
|
|
957 |
}
|
| 129 |
Kevin |
958 |
SSD1306_Write_String("PN5%X Ver. %d.%d\r", version.IC, version.Ver, version.Rev);
|
|
|
959 |
SSD1306_Display();
|
| 127 |
Kevin |
960 |
NFC_SAMConfig();
|
|
|
961 |
|
| 121 |
Kevin |
962 |
while (1) {
|
|
|
963 |
|
| 127 |
Kevin |
964 |
// This query will not wait for a detection before responding
|
|
|
965 |
length = NFC_pollTargets(1, 1, cardData);
|
|
|
966 |
if (!length) {
|
|
|
967 |
memset(cardData_prev, 0, 24);
|
|
|
968 |
} else if (length == 1) {
|
|
|
969 |
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
|
|
|
970 |
// Do nothing
|
|
|
971 |
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
|
|
|
972 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
|
|
973 |
} else {
|
| 129 |
Kevin |
974 |
SSD1306_Write_String("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
|
|
|
975 |
SSD1306_Display();
|
| 127 |
Kevin |
976 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
|
|
977 |
}
|
|
|
978 |
memset(&cardData_prev[1], 0, 12);
|
|
|
979 |
} else if (length == 2) {
|
|
|
980 |
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
|
|
|
981 |
memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
|
|
|
982 |
// Do nothing
|
|
|
983 |
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
|
|
|
984 |
memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
|
|
|
985 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
|
|
986 |
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
|
|
|
987 |
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
|
|
|
988 |
// First card matched
|
| 129 |
Kevin |
989 |
SSD1306_Write_String("UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
|
|
|
990 |
SSD1306_Display();
|
| 127 |
Kevin |
991 |
memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
|
|
|
992 |
} else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
|
|
|
993 |
// Second card matched
|
| 129 |
Kevin |
994 |
SSD1306_Write_String("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
|
|
|
995 |
SSD1306_Display();
|
| 127 |
Kevin |
996 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
|
|
997 |
} else {
|
|
|
998 |
// No match
|
| 129 |
Kevin |
999 |
SSD1306_Write_String("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
|
|
|
1000 |
SSD1306_Display();
|
| 127 |
Kevin |
1001 |
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
|
| 129 |
Kevin |
1002 |
SSD1306_Write_String("UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
|
|
|
1003 |
SSD1306_Display();
|
| 127 |
Kevin |
1004 |
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
|
|
|
1005 |
}
|
| 126 |
Kevin |
1006 |
}
|
| 121 |
Kevin |
1007 |
}
|
|
|
1008 |
}
|
|
|
1009 |
#endif
|