Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
152 Kevin 1
#include <xc.h>
2
#include <delays.h>
3
#include <stdio.h>
155 Kevin 4
#include <string.h>
152 Kevin 5
#include "defines.h"
155 Kevin 6
#include "interrupts.h"
152 Kevin 7
#include "uart.h"
155 Kevin 8
#include "i2c.h"
9
#include "spi.h"
157 Kevin 10
#include "timers.h"
155 Kevin 11
#include "nfc_PN532.h"
12
#include "led_HT16K33.h"
157 Kevin 13
#include "lux_TSL2561.h"
14
#include "temp_BMP085.h"
155 Kevin 15
#include "oled_ssd1306.h"
16
#include "oled_ssd1331.h"
17
#include "oled_NHD-0216KZW-AB5.h"
18
#include "adc.h"
157 Kevin 19
#include "xbee.h"
152 Kevin 20
 
21
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
22
/* --------------------------- Configuration Bits --------------------------- */
23
/* CONFIG1L @ 0x1FFF8 */
24
#pragma config CFGPLLEN = ON        // Enable PLL on startup
25
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
26
#pragma config WDTEN = OFF          // Turn off watchdog timer
27
#pragma config STVREN = OFF         // Stack overflow/underflow reset disabled
28
#pragma config XINST = OFF          // Turn off extended instruction set
29
 
30
/* CONFIG1H @ 0x1FFF9 */
31
#pragma config CP0 = OFF            // Program memory is not code-protected
32
 
33
/* CONFIG2L @ 0x1FFFA */
34
#pragma config CLKOEC = OFF         // CLKO output disabled on RA6 pin
35
#pragma config SOSCSEL = LOW        // Low Power T1OSC/SOSC circuit selected
36
#pragma config IESO = ON            // Internal external oscillator switch over disabled
37
#pragma config OSC = HSPLL          // Use external oscillator (101)
38
#pragma config FCMEN = OFF          // Fail-safe clock monitor disabled
39
 
40
/* CONFIG2H @ 0x1FFFB */
41
#pragma config WDTPS = 1            // Watchdog postscaler of 1:1
42
 
43
/* CONFIG3L @ 0x1FFFC */
44
#pragma config RTCOSC = T1OSCREF    // RTCC uses T1OSC/T1CKI
45
#pragma config DSBOREN = ON         // Deep sleep BOR enabled
46
#pragma config DSWDTPS = M2         // Deep sleep watchdog postscaler of 1:2 (36m)
47
#pragma config DSWDTEN = OFF        // Deep sleep watchdog timer disabled
48
#pragma config DSWDTOSC = INTOSCREF  // DSWDT clock select uses INTRC
49
 
50
/* CONFIG3H @ 0x1FFFD */
51
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
52
#pragma config ADCSEL = BIT12       // 12-bit ADC
53
#pragma config MSSP7B_EN = MSK7     // 7-bit address masking mode
54
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
55
 
56
/* CONFIG4L @ 0x1FFFE */
57
#pragma config WPCFG = ON           // Configuration words page protected
58
 
59
/* CONFIG4H @ 0x1FFFF */
60
#pragma config WPEND = PAGE_WPFP    // Pages WPFP<6:0> through Configuration Words erase/write protected
61
#pragma config WPDIS = OFF          // WPFP<6:0>/WPEND region ignored
62
/* -------------------------------------------------------------------------- */
63
// </editor-fold>
64
 
65
#if  defined(_TEST_UART)
66
int main() {
67
    char buffer[100];
155 Kevin 68
 
69
    // Set all ports as digial I/O
70
    ANCON0 = 0xFF;
71
    ANCON1 = 0x1F;
72
 
73
    UART_DATA uart_data;
74
    UART1_Init(&uart_data); // Initialize the UART handler code
75
 
76
    Interrupt_Init(); // Initialize the interrupt priorities
77
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
78
 
79
    char output[] = "\r\nBegin Program\r\n";
80
    DBG_PRINT_MAIN(output, strlen(output));
81
 
82
    while (1) {
83
        char length = UART1_Read_Buffer((char *) buffer);
84
        if (length != 0) {
85
            UART1_WriteS(buffer, length);
86
        }
87
 
88
        Delay10KTCYx(255);
89
        Delay10KTCYx(255);
90
    }
91
}
92
#elif defined(_TEST_I2C_MASTER)
93
void main(void) {
94
    char length = 0;
95
    char result = 0;
96
    char buffer[100];
97
    char output[64];
98
 
99
    // Set all ports as digial I/O
100
    ANCON0 = 0xFF;
101
    ANCON1 = 0x1F;
102
 
103
    UART_DATA uart_data;
104
    UART1_Init(&uart_data); // Initialize the UART handler code
105
    I2C_DATA i2c_data;
106
    I2C_Init(&i2c_data); // Initialize the I2C handler code
107
 
108
    I2C_Configure_Master(I2C_100KHZ);
109
 
110
    Interrupt_Init(); // Initialize the interrupt priorities
111
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
112
 
113
    sprintf(output, "\r\nBegin Program\r\n");
114
    DBG_PRINT_MAIN(output, strlen(output));
115
 
116
    while (1) {
117
        buffer[0] = 0x8;
118
 
119
        I2C_Master_Send(0x24, 1, buffer);
120
        do {
121
            result = I2C_Get_Status();
122
        } while (!result);
123
        sprintf(output, "S: %X ", result);
124
        DBG_PRINT_MAIN(output, strlen(output));
125
 
126
        I2C_Master_Recv(0x24, 2);
127
        do {
128
            result = I2C_Get_Status();
129
        } while (!result);
130
        sprintf(output, "S: %X ", result);
131
        DBG_PRINT_MAIN(output, strlen(output));
132
        length = I2C_Read_Buffer(buffer);
133
        sprintf(output, "L: %d D: ", length);
134
        DBG_PRINT_MAIN(output, strlen(output));
135
        for (char i = 0; i < length; i++) {
136
            sprintf(output, "%c ", buffer[i]);
137
            DBG_PRINT_MAIN(output, strlen(output));
138
        }
139
        sprintf(output, "\r\n");
140
        DBG_PRINT_MAIN(output, strlen(output));
141
 
142
        I2C_Master_Restart(0x30, 0xBB, 2);
143
        result = I2C_Get_Status();
144
        while (!result) {
145
            result = I2C_Get_Status();
146
        }
147
        sprintf(output, "S: %X ", result);
148
        DBG_PRINT_MAIN(output, strlen(output));
149
        length = I2C_Read_Buffer(buffer);
150
        sprintf(output, "L: %d D: ", length);
151
        DBG_PRINT_MAIN(output, strlen(output));
152
        for (char i = 0; i < length; i++) {
153
            sprintf(output, "%c ", buffer[i]);
154
            DBG_PRINT_MAIN(output, strlen(output));
155
        }
156
        sprintf(output, "\r\n");
157
        DBG_PRINT_MAIN(output, strlen(output));
158
 
159
        Delay10KTCYx(255);
160
        Delay10KTCYx(255);
161
    }
162
}
163
#elif defined(_TEST_I2C_SLAVE)
164
void main(void) {
165
    char length = 0;
166
    char result = 0;
167
    char buffer[100];
168
    char output[64];
169
 
170
    // Set all ports as digial I/O
171
    ANCON0 = 0xFF;
172
    ANCON1 = 0x1F;
173
 
174
    UART_DATA uart_data;
175
    UART1_Init(&uart_data); // Initialize the UART handler code
176
    I2C_DATA i2c_data;
177
    I2C_Init(&i2c_data); // Initialize the I2C handler code
178
 
179
    I2C_Configure_Slave(0x24);
180
 
181
    Interrupt_Init(); // Initialize the interrupt priorities
182
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
183
 
184
    sprintf(output, "\r\nBegin Program\r\n");
185
    DBG_PRINT_MAIN(output, strlen(output));
186
 
187
    while (1) {
188
 
189
        result = I2C_Get_Status();
190
        while (!result) {
191
            result = I2C_Get_Status();
192
        }
193
        sprintf(output, "S: %X ", result);
194
        DBG_PRINT_MAIN(output, strlen(output));
195
        length = I2C_Read_Buffer(buffer);
196
        sprintf(output, "L: %d D: ", length);
197
        DBG_PRINT_MAIN(output, strlen(output));
198
        for (char i = 0; i < length; i++) {
199
            sprintf(output, "%X ", buffer[i]);
200
            DBG_PRINT_MAIN(output, strlen(output));
201
        }
202
        sprintf(output, "\r\n");
203
        DBG_PRINT_MAIN(output, strlen(output));
204
 
205
        Delay10KTCYx(255);
206
        Delay10KTCYx(255);
207
    }
208
}
209
#elif defined(_TEST_SPI)
210
void main(void) {
211
    char length = 0;
212
    char result = 0;
213
    char buffer[100];
214
    char output[64];
215
    char test[8] = "ASDF123";
216
 
217
    // Set all ports as digial I/O
218
    ANCON0 = 0xFF;
219
    ANCON1 = 0x1F;
220
 
221
    UART_DATA uart_data;
222
    UART1_Init(&uart_data); // Initialize the UART handler code
223
    SPI_DATA spi_data;
224
    SPI2_Init(&spi_data, SPI2_FOSC_8); // Initialize the SPI module
225
 
226
    Interrupt_Init(); // Initialize the interrupt priorities
227
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
228
 
229
    sprintf(output, "\r\nBegin Program\r\n");
230
    DBG_PRINT_MAIN(output, strlen(output));
231
 
232
    while (1) {
233
 
234
        SPI2_Write(test, 7);
235
        while (result != 7) {
236
            length = SPI2_Read_Buffer(buffer);
237
            if (length) {
238
                result += length;
239
            }
240
        }
241
        result = 0;
242
 
243
        for (char i = 0; i < result; i++) {
244
            sprintf(output, "%X ", buffer[i]);
245
            DBG_PRINT_MAIN(output, strlen(output));
246
        }
247
        sprintf(output, "\r\n");
248
        DBG_PRINT_MAIN(output, strlen(output));
249
 
250
        Delay10KTCYx(255);
251
        Delay10KTCYx(255);
252
    }
253
}
254
#elif defined(_TEST_NFC)
255
void main(void) {
256
    char length = 0;
257
    char output[64];
258
 
259
    // NFC stuff
260
    NFC_FIRMWARE_VERSION version;
261
    NFC_TargetDataMiFare cardData[2];
262
    NFC_TargetDataMiFare cardData_prev[2];
263
 
264
    // Set all ports as digial I/O
265
    ANCON0 = 0xFF;
266
    ANCON1 = 0x1F;
267
 
268
    UART_DATA uart_data;
269
    UART1_Init(&uart_data); // Initialize the UART handler code
270
    I2C_DATA i2c_data;
271
    I2C_Init(&i2c_data); // Initialize the I2C handler code
272
    NFC_DATA nfc_data;
273
    NFC_Init(&nfc_data); // Initialize the NFC chip (uses I2C)
274
 
275
    I2C_Configure_Master(I2C_400KHZ);
276
 
277
    Interrupt_Init(); // Initialize the interrupt priorities
278
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
279
 
280
    sprintf(output, "\r\nBegin Program\r\n");
281
    DBG_PRINT_MAIN(output, strlen(output));
282
 
283
    version = NFC_Get_Firmware_Version();
284
    while (!version.IC) {
285
        sprintf(output, "Waiting for NFC board..\r\n");
286
        DBG_PRINT_MAIN(output, strlen(output));
287
        Delay10KTCYx(3);
288
        version = NFC_Get_Firmware_Version();
289
    }
290
    sprintf(output, "Found chip PN5%X\r\n", version.IC);
291
    DBG_PRINT_MAIN(output, strlen(output));
292
    sprintf(output, "Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
293
    DBG_PRINT_MAIN(output, strlen(output));
294
    NFC_SAMConfig();
295
 
296
    memset(cardData, 0, 24);
297
 
298
    while (1) {
299
 
300
        //        // This query will hang until the NFC chip replies (card detected)
301
        //        length = NFC_readPassiveTargetID(cardData);
302
        //        if (length) {
303
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
304
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
305
        //            DBG_PRINT_MAIN("UID: ");
306
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
307
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
308
        //            }
309
        //            DBG_PRINT_MAIN("\r\n");
310
        //            if (length == 2) {
311
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
312
        //                DBG_PRINT_MAIN("UID: ");
313
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
314
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
315
        //                }
316
        //                DBG_PRINT_MAIN("\r\n");
317
        //            }
318
        //        }
319
 
320
        //        // This query will hang until the NFC chip replies (card detected)
321
        //        length = NFC_readPassiveTargetID(cardData);
322
        //        if (length) {
323
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
324
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
325
        //            DBG_PRINT_MAIN("UID: ");
326
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
327
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
328
        //            }
329
        //            DBG_PRINT_MAIN("\r\n");
330
        //            if (length == 2) {
331
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
332
        //                DBG_PRINT_MAIN("UID: ");
333
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
334
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
335
        //                }
336
        //                DBG_PRINT_MAIN("\r\n");
337
        //            }
338
        //        }
339
 
340
        // This query will not wait for a detection before responding
341
        length = NFC_Poll_Targets(1, 1, cardData);
342
        if (!length) {
343
            memset(cardData_prev, 0, 24);
344
        } else if (length == 1) {
345
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
346
                // Do nothing
347
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
348
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
349
            } else {
350
                sprintf(output, "UID: ");
351
                DBG_PRINT_MAIN(output, strlen(output));
352
                for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
353
                    sprintf(output, "%02X ", cardData[0].NFCID[i]);
354
                    DBG_PRINT_MAIN(output, strlen(output));
355
                }
356
                sprintf(output, "\r\n");
357
                DBG_PRINT_MAIN(output, strlen(output));
358
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
359
            }
360
            memset(&cardData_prev[1], 0, 12);
361
        } else if (length == 2) {
362
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
363
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
364
                // Do nothing
365
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
366
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
367
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
368
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
369
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
370
                // First card matched
371
                sprintf(output, "UID2: ");
372
                DBG_PRINT_MAIN(output, strlen(output));
373
                for (char i = 0; i < cardData[1].NFCID_LEN; i++) {
374
                    sprintf(output, "%02X ", cardData[1].NFCID[i]);
375
                    DBG_PRINT_MAIN(output, strlen(output));
376
                }
377
                sprintf(output, "\r\n");
378
                DBG_PRINT_MAIN(output, strlen(output));
379
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
380
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
381
                // Second card matched
382
                sprintf(output, "UID1: ");
383
                DBG_PRINT_MAIN(output, strlen(output));
384
                for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
385
                    sprintf(output, "%02X ", cardData[0].NFCID[i]);
386
                    DBG_PRINT_MAIN(output, strlen(output));
387
                }
388
                sprintf(output, "\r\n");
389
                DBG_PRINT_MAIN(output, strlen(output));
390
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
391
            } else {
392
                // No match
393
                sprintf(output, "UID1: ");
394
                DBG_PRINT_MAIN(output, strlen(output));
395
                for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
396
                    sprintf(output, "%02X ", cardData[0].NFCID[i]);
397
                    DBG_PRINT_MAIN(output, strlen(output));
398
                }
399
                sprintf(output, "\r\n");
400
                DBG_PRINT_MAIN(output, strlen(output));
401
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
402
                sprintf(output, "UID2: ");
403
                DBG_PRINT_MAIN(output, strlen(output));
404
                for (char i = 0; i < cardData[1].NFCID_LEN; i++) {
405
                    sprintf(output, "%02X ", cardData[1].NFCID[i]);
406
                    DBG_PRINT_MAIN(output, strlen(output));
407
                }
408
                sprintf(output, "\r\n");
409
                DBG_PRINT_MAIN(output, strlen(output));
410
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
411
            }
412
        }
413
    }
414
}
415
#elif defined(_TEST_LED_BACKPACK)
416
void main(void) {
417
    unsigned int counter = 0;
418
    char output[64];
419
 
420
    // Set all ports as digial I/O
421
    ANCON0 = 0xFF;
422
    ANCON1 = 0x1F;
423
 
424
    UART_DATA uart_data;
425
    UART1_Init(&uart_data); // Initialize the UART handler code
426
    I2C_DATA i2c_data;
427
    I2C_Init(&i2c_data); // Initialize the I2C handler code
428
    LED_DATA led_data;
429
    LED_Init(&led_data); // Initialize the LED backpack (uses I2C);
430
 
431
    I2C_Configure_Master(I2C_400KHZ);
432
 
433
    Interrupt_Init(); // Initialize the interrupt priorities
434
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
435
 
436
    sprintf(output, "\r\nBegin Program\r\n");
437
    DBG_PRINT_MAIN(output, strlen(output));
438
 
439
    LED_Start();
440
    LED_Write_Digit_Num(0, 1, 1);
441
    LED_Write_Digit_Num(1, 2, 0);
442
    LED_Write_Digit_Num(2, 3, 0);
443
    LED_Write_Digit_Num(3, 4, 0);
444
    LED_Write_Display();
445
    for (char i = 0; i < 15; i++) {
446
        LED_Set_Brightness(15 - i);
447
        Delay10KTCYx(100);
448
    }
449
    for (char i = 0; i < 15; i++) {
450
        LED_Set_Brightness(i);
451
        Delay10KTCYx(100);
452
    }
453
    LED_Blink_Rate(HT16K33_BLINK_OFF);
454
 
455
    while (1) {
456
        LED_Write_Num(counter);
457
        counter++;
458
        if (counter > 9999)
459
            counter = 0;
460
 
461
        //        Delay10KTCYx(255);
462
    }
463
}
464
#elif defined(_TEST_SSD1306_OLED)
465
void main(void) {
466
    char output[64];
467
 
468
    // Set all ports as digial I/O
469
    ANCON0 = 0xFF;
470
    ANCON1 = 0x1F;
471
 
472
    UART_DATA uart_data;
473
    UART1_Init(&uart_data); // Initialize the UART handler code
474
    SPI_DATA spi_data;
475
    SPI2_Init(&spi_data, SPI2_FOSC_4); // Initialize the SPI module
476
    SSD1306_DATA ssd1306_data;
477
    SSD1306_Init(&ssd1306_data); // Initialize the OLED code
478
 
479
    Interrupt_Init(); // Initialize the interrupt priorities
480
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
481
 
482
    sprintf(output, "\r\nBegin Program\r\n");
483
    DBG_PRINT_MAIN(output, strlen(output));
484
 
485
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
486
 
487
    SSD1306_Display(); // Show splashscreen
488
 
489
    while (1) {
490
        Delay10KTCYx(255);
491
        Delay10KTCYx(255);
492
        SSD1306_Clear_Display();
493
        SSD1306_Test_DrawLine();
494
        SSD1306_Display();
495
 
496
        Delay10KTCYx(255);
497
        Delay10KTCYx(255);
498
        SSD1306_Clear_Display();
499
        SSD1306_Test_DrawRect();
500
        SSD1306_Display();
501
 
502
        Delay10KTCYx(255);
503
        Delay10KTCYx(255);
504
        SSD1306_Clear_Display();
505
        SSD1306_Test_FillRect();
506
        SSD1306_Display();
507
 
508
        Delay10KTCYx(255);
509
        Delay10KTCYx(255);
510
        SSD1306_Clear_Display();
511
        SSD1306_Test_DrawCircle();
512
        SSD1306_Display();
513
 
514
        Delay10KTCYx(255);
515
        Delay10KTCYx(255);
516
        SSD1306_Clear_Display();
517
        SSD1306_Fill_Circle(SSD1306_LCDWIDTH / 2, SSD1306_LCDHEIGHT / 2, 10, SSD1306_WHITE);
518
        SSD1306_Display();
519
 
520
        Delay10KTCYx(255);
521
        Delay10KTCYx(255);
522
        SSD1306_Clear_Display();
523
        SSD1306_Test_DrawRoundRect();
524
        SSD1306_Display();
525
 
526
        Delay10KTCYx(255);
527
        Delay10KTCYx(255);
528
        SSD1306_Clear_Display();
529
        SSD1306_Test_FillRoundRect();
530
        SSD1306_Display();
531
 
532
        Delay10KTCYx(255);
533
        Delay10KTCYx(255);
534
        SSD1306_Clear_Display();
535
        SSD1306_Test_DrawTriangle();
536
        SSD1306_Display();
537
 
538
        Delay10KTCYx(255);
539
        Delay10KTCYx(255);
540
        SSD1306_Clear_Display();
541
        SSD1306_Test_FillTriangle();
542
        SSD1306_Display();
543
 
544
        Delay10KTCYx(255);
545
        Delay10KTCYx(255);
546
        SSD1306_Clear_Display();
547
        SSD1306_Test_DrawChar();
548
        SSD1306_Display();
549
 
550
        Delay10KTCYx(255);
551
        Delay10KTCYx(255);
552
        SSD1306_Clear_Display();
553
        SSD1306_Set_Text_Size(1);
554
        SSD1306_Set_Text_Color(SSD1306_WHITE);
555
        SSD1306_Set_Cursor(0, 0);
556
        sprintf(output, "Hello World!\n");
557
        SSD1306_Write_String(output, strlen(output));
558
        //        SSD1306_Set_Text_Color_BG(BLACK, WHITE);
559
        unsigned int i = 65535;
560
        sprintf(output, "%u %d\n", i, i);
561
        SSD1306_Write_String(output, strlen(output));
562
        //        SSD1306_Set_Text_Size(2);
563
        //        SSD1306_Set_Text_Color(WHITE);
564
        unsigned long l = 0xDEADBEEF;
565
        sprintf(output, "0x%lX", (long) l);
566
        SSD1306_Write_String(output, strlen(output));
567
        SSD1306_Display();
568
 
569
        //        SSD1306_Clear_Display();
570
        //        SSD1306_Set_Rotation(0);
571
        //        SSD1306_Set_Text_Size(1);
572
        //        SSD1306_Set_Text_Color(SSD1306_WHITE);
573
        //        SSD1306_Set_Cursor(0, 0);
574
        //        SSD1306_Write_String("%u", i);
575
        //        i++;
576
        //        SSD1306_Display();
577
 
578
    }
579
}
580
#elif defined(_TEST_SSD1331_OLED)
581
void main(void) {
582
    char output[128];
583
 
584
    // Set all ports as digial I/O
585
    ANCON0 = 0xFF;
586
    ANCON1 = 0x1F;
587
 
588
    UART_DATA uart_data;
589
    UART1_Init(&uart_data); // Initialize the UART handler code
590
    SPI_DATA spi_data;
591
    SPI2_Init(&spi_data, SPI2_FOSC_64); // Initialize the SPI module
592
    SSD1331_DATA ssd1331_data;
593
    SSD1331_Init(&ssd1331_data); // Initialize the OLED code
594
 
595
    Interrupt_Init(); // Initialize the interrupt priorities
596
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
597
 
598
    sprintf(output, "\r\nBegin Program\r\n");
599
    DBG_PRINT_MAIN(output, strlen(output));
600
 
601
    SSD1331_Begin();
602
 
603
    while (1) {
604
 
605
        Delay10KTCYx(255);
606
        Delay10KTCYx(255);
607
        SSD1331_Set_Rotation(0);
608
        SSD1331_Test_Pattern();
609
 
610
        Delay10KTCYx(255);
611
        Delay10KTCYx(255);
612
        SSD1331_Clear_Display();
613
        SSD1331_Set_Rotation(0);
614
        SSD1331_Set_Cursor(0, 0);
615
        sprintf(output, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabit adipiscing ante sed nibh tincidunt feugiat.");
157 Kevin 616
        SSD1331_Write_String(output, strlen(output));
155 Kevin 617
 
618
//        Delay10KTCYx(255);
619
//        Delay10KTCYx(255);
620
//        SSD1331_Clear_Display();
621
//        SSD1331_Set_Rotation(3);
622
//        SSD1331_Set_Cursor(0, 0);
623
//        SSD1331_Write_String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
624
 
625
        Delay10KTCYx(255);
626
        Delay10KTCYx(255);
627
        SSD1331_Set_Rotation(0);
628
        SSD1331_Test_DrawLines(SSD1331_YELLOW);
629
 
630
        Delay10KTCYx(255);
631
        Delay10KTCYx(255);
632
        SSD1331_Set_Rotation(3);
633
        SSD1331_Test_DrawLines(SSD1331_BLUE);
634
 
635
                Delay10KTCYx(255);
636
                Delay10KTCYx(255);
637
                SSD1331_Set_Rotation(0);
638
                SSD1331_Test_DrawRect(SSD1331_GREEN);
639
 
640
                Delay10KTCYx(255);
641
                Delay10KTCYx(255);
642
                SSD1331_Set_Rotation(1);
643
                SSD1331_Test_DrawRect(SSD1331_RED);
644
 
645
                Delay10KTCYx(255);
646
                Delay10KTCYx(255);
647
                SSD1331_Set_Rotation(2);
648
                SSD1331_Test_DrawRect(SSD1331_BLUE);
649
 
650
                Delay10KTCYx(255);
651
                Delay10KTCYx(255);
652
                SSD1331_Set_Rotation(3);
653
                SSD1331_Test_DrawRect(SSD1331_YELLOW);
654
 
655
                Delay10KTCYx(255);
656
                Delay10KTCYx(255);
657
                SSD1331_Set_Rotation(0);
658
                SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
659
 
660
                Delay10KTCYx(255);
661
                Delay10KTCYx(255);
662
                SSD1331_Set_Rotation(3);
663
                SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
664
 
665
                Delay10KTCYx(255);
666
                Delay10KTCYx(255);
667
                SSD1331_Set_Rotation(0);
668
                SSD1331_Clear_Display();
669
                SSD1331_Test_FillCircle(10, SSD1331_BLUE);
670
                SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
671
 
672
                Delay10KTCYx(255);
673
                Delay10KTCYx(255);
674
                SSD1331_Set_Rotation(3);
675
                SSD1331_Clear_Display();
676
                SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
677
                SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
678
 
679
                Delay10KTCYx(255);
680
                Delay10KTCYx(255);
681
                SSD1331_Set_Rotation(0);
682
                SSD1331_Test_DrawTria();
683
 
684
                Delay10KTCYx(255);
685
                Delay10KTCYx(255);
686
                SSD1331_Set_Rotation(3);
687
                SSD1331_Test_DrawTria();
688
 
689
                Delay10KTCYx(255);
690
                Delay10KTCYx(255);
691
                SSD1331_Set_Rotation(0);
692
                SSD1331_Test_DrawRoundRect();
693
 
694
                Delay10KTCYx(255);
695
                Delay10KTCYx(255);
696
                SSD1331_Set_Rotation(3);
697
                SSD1331_Test_DrawRoundRect();
698
 
699
        //        SSD1331_Clear_Display();
700
        //        SSD1331_Set_Rotation(3);
701
        //        SSD1331_Set_Cursor(0,0);
702
        //        SSD1331_Set_Text_Color_BG(SSD1331_WHITE, SSD1331_BLACK);
703
        //        SSD1331_Write_String("%u", i);
704
        //        i++;
705
    }
706
}
707
#elif defined(_TEST_TIMER1_RTC)
708
void main(void) {
709
 
710
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
711
    ANCON0 = 0xF8;
712
    ANCON1 = 0x1F;
713
 
714
    Timer1_Init();
715
 
716
    Interrupt_Init(); // Initialize the interrupt priorities
717
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
718
 
719
    LED_BLUE_TRIS = 0;
720
    LED_RED_TRIS = 0;
721
 
722
    Timer1_Enable();
723
 
724
    while (1) {
725
 
726
    }
727
}
728
#elif defined(_TEST_LUX)
729
void main(void) {
730
    char output[64];
731
 
732
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
733
    ANCON0 = 0xF8;
734
    ANCON1 = 0x1F;
735
 
736
    UART_DATA uart_data;
737
    UART1_Init(&uart_data);
738
    I2C_DATA i2c_data;
739
    I2C_Init(&i2c_data);
740
    TSL2561_DATA lux_data;
741
    LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
742
 
743
    I2C_Configure_Master(I2C_100KHZ);
744
 
745
    Interrupt_Init(); // Initialize the interrupt priorities
746
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
747
 
748
    LUX_Begin();
749
 
750
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
751
//    LUX_Set_Gain(TSL2561_GAIN_0X);   // set no gain (for bright situtations)
752
    LUX_Set_Gain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
753
 
754
    // Changing the integration time gives you a longer time over which to sense light
755
    // longer timelines are slower, but are good in very low light situtations!
756
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)
757
    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
758
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
759
 
760
    sprintf(output, "\r\nBegin Program\r\n");
761
    DBG_PRINT_MAIN(output, strlen(output));
762
 
763
    while (1) {
764
        unsigned long lum = LUX_Get_Full_Luminosity();
765
        unsigned int ir = lum >> 16;
766
        unsigned int full = lum & 0xFFFF;
767
        sprintf(output, "IR: %d\r\n", ir);
768
        DBG_PRINT_MAIN(output, strlen(output));
769
        sprintf(output, "Visible: %d\r\n", full - ir);
770
        DBG_PRINT_MAIN(output, strlen(output));
771
        sprintf(output, "Full: %d\r\n", full);
772
        DBG_PRINT_MAIN(output, strlen(output));
773
        sprintf(output, "Lux: %ld\r\n\r\n", LUX_Calculate_Lux(full, ir));
774
        DBG_PRINT_MAIN(output, strlen(output));
775
 
776
        Delay10KTCYx(255);
777
        Delay10KTCYx(255);
778
        Delay10KTCYx(255);
779
        Delay10KTCYx(255);
780
    }
781
}
782
#elif defined(_TEST_OLED_CHAR)
783
void main(void) {
784
    char output[64];
785
 
786
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
787
    ANCON0 = 0xFF;
788
    ANCON1 = 0x1F;
789
 
790
//    UART1_Init();
791
    OLED_CHAR_DATA oled_data;
792
    NHD_Init(&oled_data);
793
 
794
    Interrupt_Init(); // Initialize the interrupt priorities
795
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
796
 
797
    NHD_Begin(16, 2);
798
 
799
    sprintf(output, "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do");
800
    NHD_Write_String(output, strlen(output));
801
    NHD_Set_Cursor(0,1);
802
    sprintf(output, "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e");
803
    NHD_Write_String(output, strlen(output));
804
 
805
    while (1) {
806
        Delay10KTCYx(150);
807
        NHD_Scroll_Display_Left();
808
    }
809
}
810
#elif defined(_TEST_NFC_TO_SSD1306_OLED)
811
void main(void) {
812
    char output[64];
813
 
814
    // NFC stuff
815
    NFC_FIRMWARE_VERSION version;
816
    NFC_TargetDataMiFare cardData[2];
817
    NFC_TargetDataMiFare cardData_prev[2];
818
 
819
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
820
    ANCON0 = 0xF8;
821
    ANCON1 = 0x1F;
822
 
823
    UART_DATA uart_data;
824
    UART1_Init(&uart_data);
825
    I2C_DATA i2c_data;
826
    I2C_Init(&i2c_data);
827
    NFC_DATA nfc_data;
828
    NFC_Init(&nfc_data);
829
    SPI_DATA spi_data;
830
    SPI2_Init(&spi_data, SPI2_FOSC_4);
831
    SSD1306_DATA ssd1306_data;
832
    SSD1306_Init(&ssd1306_data);
833
 
834
    I2C_Configure_Master(I2C_400KHZ);
835
 
836
    Interrupt_Init(); // Initialize the interrupt priorities
837
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
838
 
839
    sprintf(output, "\r\nBegin Program\r\n");
840
    DBG_PRINT_MAIN(output, strlen(output));
841
 
842
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
843
    memset(cardData, 0, 24);
844
    memset(cardData_prev, 0, 24);
845
    SSD1306_Clear_Display();
846
    SSD1306_Set_Rotation(0);
847
    SSD1306_Set_Cursor(0, 0);
848
 
849
    version = NFC_Get_Firmware_Version();
850
    while (!version.IC) {
851
        sprintf(output, "Waiting for NFC board..\n");
852
        SSD1306_Write_String(output, strlen(output));
853
        SSD1306_Display();
854
        Delay10KTCYx(255);
855
        version = NFC_Get_Firmware_Version();
856
    }
857
    sprintf(output, "PN5%X Ver. %d.%d\n", version.IC, version.Ver, version.Rev);
858
    SSD1306_Write_String(output, strlen(output));
859
    SSD1306_Display();
860
    NFC_SAMConfig();
861
 
862
    while (1) {
863
 
864
        // This query will not wait for a detection before responding
865
        char length = NFC_Poll_Targets(1, 1, cardData);
866
        if (!length) {
867
            memset(cardData_prev, 0, 24);
868
        } else if (length == 1) {
869
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
870
                // Do nothing
871
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
872
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
873
            } else {
874
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
875
                SSD1306_Write_String(output, strlen(output));
876
                SSD1306_Display();
877
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
878
            }
879
            memset(&cardData_prev[1], 0, 12);
880
        } else if (length == 2) {
881
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
882
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
883
                // Do nothing
884
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
885
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
886
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
887
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
888
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
889
                // First card matched
890
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
891
                SSD1306_Write_String(output, strlen(output));
892
                SSD1306_Display();
893
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
894
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
895
                // Second card matched
896
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
897
                SSD1306_Write_String(output, strlen(output));
898
                SSD1306_Display();
899
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
900
            } else {
901
                // No match
902
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
903
                SSD1306_Write_String(output, strlen(output));
904
                SSD1306_Display();
905
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
906
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
907
                SSD1306_Write_String(output, strlen(output));
908
                SSD1306_Display();
909
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
910
            }
911
        }
912
    }
913
}
914
#elif defined(_TEST_LUX_TO_CHAR_OLED)
915
void main(void) {
916
    char output[64];
917
 
918
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
919
    ANCON0 = 0xF8;
920
    ANCON1 = 0x1F;
921
 
922
    I2C_DATA i2c_data;
923
    I2C_Init(&i2c_data);
924
    OLED_CHAR_DATA oled_data;
925
    NHD_Init(&oled_data);
926
    TSL2561_DATA lux_data;
927
    LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
928
 
929
    I2C_Configure_Master(I2C_400KHZ);
930
 
931
    Interrupt_Init(); // Initialize the interrupt priorities
932
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
933
 
934
    NHD_Begin(16, 2);
935
 
936
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
937
//    LUX_SetGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
938
    LUX_Set_Gain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
939
 
940
    // Changing the integration time gives you a longer time over which to sense light
941
    // longer timelines are slower, but are good in very low light situtations!
942
//    LUX_SetTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
943
    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
944
//    LUX_SetTiming(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
945
 
946
    while (1) {
947
        unsigned long lum = LUX_Get_Full_Luminosity();
948
        unsigned int ir = lum >> 16;
949
        unsigned int full = lum & 0xFFFF;
950
        NHD_Set_Cursor(0, 0);
951
        sprintf(output, "I: %d ", ir);
952
        NHD_Write_String(output, strlen(output));
953
        sprintf(output, "V: %d        ", full - ir);
954
        NHD_Write_String(output, strlen(output));
955
        NHD_Set_Cursor(0, 1);
956
        sprintf(output, "Lux: %ld        ", LUX_Calculate_Lux(full, ir));
957
        NHD_Write_String(output, strlen(output));
958
 
959
        Delay10KTCYx(100);
960
    }
961
}
962
#elif defined(_TEST_ADC)
963
void main(void) {
964
    unsigned int x, y, z;
965
    char buffer[60];
966
 
967
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
968
    ANCON0 = 0xF8;
969
    ANCON1 = 0x1F;
970
 
971
    UART_DATA uart_data;
972
    UART1_Init(&uart_data); // Initialize the UART handler code
973
    SPI_DATA spi_data;
974
    SPI2_Init(&spi_data, SPI2_FOSC_8); // Initialize the SPI module
975
    SSD1306_DATA ssd1306_data;
976
    SSD1306_Init(&ssd1306_data); // Initialize the SSD1331 OLED display (uses SPI2)
977
    ADC_DATA adc_data;
978
    ADC_Init(&adc_data, ADC_TAD_20, ADC_FOSC_64_);
979
 
980
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
981
 
982
    Interrupt_Init(); // Initialize the interrupt priorities
983
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
984
 
985
    sprintf(buffer, "\r\nBegin Program\r\n");
986
    SSD1306_Write_String(buffer, strlen(buffer));
987
 
988
    memset(buffer, 0, 60);
989
    SSD1306_Clear_Display();
990
    SSD1306_Display();
991
 
992
    while (1) {
993
        // ADC read from AN0-AN2 and prints to display
994
        ADC_Start(ADC_CHANNEL_AN2);
995
//        SSD1306_Fill_Rect(0, 0, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
996
        SSD1306_Set_Cursor(0, 0);
997
        while (!ADC_Get_Result(&x));
998
        sprintf(buffer, "X: %u", x);
999
        SSD1306_Write_String(buffer, strlen(buffer));
1000
        SSD1306_Display();
1001
 
1002
        ADC_Start(ADC_CHANNEL_AN1);
1003
//        SSD1306_Fill_Rect(0, 8, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
1004
        SSD1306_Set_Cursor(0, 8);
1005
        while (!ADC_Get_Result(&y));
1006
        sprintf(buffer, "Y: %u", y);
1007
        SSD1306_Write_String(buffer, strlen(buffer));
1008
        SSD1306_Display();
1009
 
1010
        ADC_Start(ADC_CHANNEL_AN0);
1011
//        SSD1306_Fill_Rect(0, 16, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
1012
        SSD1306_Set_Cursor(0, 16);
1013
        while (!ADC_Get_Result(&z));
1014
        sprintf(buffer, "Z: %u", z);
1015
        SSD1306_Write_String(buffer, strlen(buffer));
1016
        SSD1306_Display();
1017
    }
1018
}
1019
#elif defined(_TEST_BMP)
1020
void main(void) {
1021
    char output[64];
1022
 
1023
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1024
    ANCON0 = 0xF8;
1025
    ANCON1 = 0x1F;
1026
 
1027
    UART_DATA uart_data;
1028
    UART1_Init(&uart_data);
1029
    I2C_DATA i2c_data;
1030
    I2C_Init(&i2c_data);
1031
    BMP085_DATA bmp_data;
1032
    BMP_Init(&bmp_data);
1033
 
157 Kevin 1034
    I2C_Configure_Master(I2C_400KHZ);
155 Kevin 1035
 
1036
    Interrupt_Init(); // Initialize the interrupt priorities
1037
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1038
 
1039
    BMP_Begin(BMP085_ULTRAHIGHRES);
1040
 
157 Kevin 1041
    BMP_Read_Temperature();
1042
    BMP_Read_Pressure();
1043
    BMP_Read_Altitude(101592);
1044
 
155 Kevin 1045
    while (1) {
1046
        sprintf(output, "Temp: %f *C\r\n", BMP_Read_Temperature());
1047
        DBG_PRINT_MAIN(output, strlen(output));
1048
        sprintf(output, "Pressure: %ld Pa\r\n", BMP_Read_Pressure());
1049
        DBG_PRINT_MAIN(output, strlen(output));
1050
        sprintf(output, "Altitude: %f meters\r\n", BMP_Read_Altitude(101592));
1051
        DBG_PRINT_MAIN(output, strlen(output));
1052
 
1053
        Delay10KTCYx(255);
1054
        Delay10KTCYx(255);
1055
        Delay10KTCYx(255);
1056
        Delay10KTCYx(255);
1057
    }
1058
}
1059
#elif defined(_TEST_XBEE)
1060
void main(void) {
157 Kevin 1061
    char buffer[100];
152 Kevin 1062
 
155 Kevin 1063
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *rx_at_cmd_response_frame;
1064
    XBEE_RX_DATA_PACKET_FRAME *rx_data_frame;
1065
    XBEE_RX_DATA_TX_STATUS_FRAME *rx_tx_status_frame;
1066
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *rx_remote_at_cmd_frame;
1067
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *rx_node_ident_frame;
1068
    XBEE_RX_MODEM_STATUS_FRAME *rx_modem_status_frame;
1069
 
152 Kevin 1070
    /* --------------------- Oscillator Configuration --------------------- */
1071
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
1072
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
1073
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
1074
    /* -------------------------------------------------------------------- */
1075
 
1076
    // Set all ports as digial I/O
1077
    ANCON0 = 0xFF;
1078
    ANCON1 = 0x1F;
1079
 
157 Kevin 1080
    UART_DATA uart_data;
1081
    UART1_Init(&uart_data); // Initialize the UART handler code
1082
    XBEE_DATA xbee_data;
1083
    XBee_Init(&xbee_data);
152 Kevin 1084
 
157 Kevin 1085
    Interrupt_Init(); // Initialize the interrupt priorities
155 Kevin 1086
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
152 Kevin 1087
 
1088
 
157 Kevin 1089
    sprintf(buffer, "\r\nBegin Program\r\n");
1090
    DBG_PRINT_MAIN(buffer, strlen(buffer));
1091
 
152 Kevin 1092
    while (1) {
1093
 
157 Kevin 1094
//#define _ROUTER
155 Kevin 1095
#define _COORDINATOR
1096
 
1097
#ifdef _ROUTER
1098
        XBEE_TX_DATA_PACKET_FRAME *tx_data_frame;
1099
        tx_data_frame = (void *) buffer;
1100
        tx_data_frame->frame_type = XBEE_TX_DATA_PACKET;
1101
        tx_data_frame->frame_id = 1;
1102
        tx_data_frame->destination_64.UPPER_32.long_value = 0x00000000;
1103
        tx_data_frame->destination_64.LOWER_32.long_value = 0x00000000;
1104
        tx_data_frame->destination_16.INT_16.int_value = 0xFEFF;
1105
        tx_data_frame->broadcast_radius = 0;
1106
        tx_data_frame->options = 0;
1107
        tx_data_frame->data[0] = 0x54;
1108
        tx_data_frame->data[1] = 0x78;
1109
        tx_data_frame->data[2] = 0x32;
1110
        tx_data_frame->data[3] = 0x43;
1111
        tx_data_frame->data[4] = 0x6F;
1112
        tx_data_frame->data[5] = 0x6F;
1113
        tx_data_frame->data[6] = 0x72;
1114
        tx_data_frame->data[7] = 0x11;
1115
        XBee_Process_Transmit_Frame(buffer, XBEE_TX_DATA_PACKET_FRAME_SIZE + 8);
1116
 
1117
        Delay10KTCYx(255);
1118
        Delay10KTCYx(255);
1119
        Delay10KTCYx(255);
1120
        Delay10KTCYx(255);
1121
        Delay10KTCYx(255);
1122
        Delay10KTCYx(255);
1123
        Delay10KTCYx(255);
1124
        Delay10KTCYx(255);
1125
#endif
1126
 
1127
#ifdef _COORDINATOR
157 Kevin 1128
        int length = XBee_Get_Received_Frame(buffer);
155 Kevin 1129
        if (length != 0) {
157 Kevin 1130
            switch (*(char *) buffer) {
155 Kevin 1131
                case XBEE_RX_AT_COMMAND_RESPONSE:
157 Kevin 1132
                    sprintf(buffer, "XBEE: parsing recieved AT command response frame\r\n");
1133
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1134
                    rx_at_cmd_response_frame = (void *) buffer;
157 Kevin 1135
//                    DBG_PRINT_MAIN("Frame ID: %u\r\n", rx_at_cmd_response_frame->frame_id);
1136
//                    DBG_PRINT_MAIN("AT Command: %c%c  Status: %02X\r\n", rx_at_cmd_response_frame->command[0], \\
1137
//                            rx_at_cmd_response_frame->command[1], rx_at_cmd_response_frame->command_status);
155 Kevin 1138
                    if (length > XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE) {
157 Kevin 1139
//                        DBG_PRINT_MAIN("Command Data: ");
1140
                        for (int i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
1141
//                            DBG_PRINT_MAIN("%02X ", rx_at_cmd_response_frame->data[i]);
155 Kevin 1142
                        }
157 Kevin 1143
//                        DBG_PRINT_MAIN("\r\n");
155 Kevin 1144
                    }
1145
                    break;
1146
                case XBEE_RX_DATA_PACKET:
157 Kevin 1147
                    sprintf(buffer, "XBEE: parsing recieved data recieved frame\r\n");
1148
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1149
                    rx_data_frame = (void *) buffer;
1150
                    XBee_Convert_Endian_64(&(rx_data_frame->source_64));
1151
                    XBee_Convert_Endian_16(&(rx_data_frame->source_16));
157 Kevin 1152
//                    DBG_PRINT_MAIN("Source 64: %08lX %08lX  Source 16: %04X  Options: %02X\r\n", \\
1153
//                            rx_data_frame->source_64.UPPER_32.long_value, \\
1154
//                            rx_data_frame->source_64.LOWER_32.long_value, \\
1155
//                            rx_data_frame->source_16.INT_16.int_value, \\
1156
//                            rx_data_frame->recieve_options);
1157
//                    DBG_PRINT_MAIN("Data: ");
1158
                    for (int i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
1159
//                        DBG_PRINT_MAIN("%02X ", rx_data_frame->data[i]);
155 Kevin 1160
                    }
157 Kevin 1161
//                    DBG_PRINT_MAIN("\r\n");
155 Kevin 1162
                    break;
1163
                case XBEE_RX_DATA_TX_STATUS:
157 Kevin 1164
                    sprintf(buffer, "XBEE: parsing recieved TX status frame\r\n");
1165
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1166
                    rx_tx_status_frame = (void *) buffer;
1167
                    XBee_Convert_Endian_16(&(rx_tx_status_frame->destination_16));
157 Kevin 1168
//                    DBG_PRINT_MAIN("Frame ID: %u  Destination 16: %04X\r\n", \\
1169
//                            rx_tx_status_frame->frame_id, rx_tx_status_frame->destination_16.INT_16.int_value);
1170
//                    DBG_PRINT_MAIN("Transmit Retry Count: %02X  Delivery Status: %02X  Discovery Status: %02X\r\n", \\
1171
//                            rx_tx_status_frame->transmit_retry_count, rx_tx_status_frame->delivery_status, \\
1172
//                            rx_tx_status_frame->discovery_status);
155 Kevin 1173
                    break;
1174
                case XBEE_RX_IO_DATA_SAMPLE:
157 Kevin 1175
                    sprintf(buffer, "XBEE: parsing recieved IO data sample frame\r\n");
1176
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1177
                    break;
1178
                case XBEE_RX_EXPLICIT_COMMAND:
157 Kevin 1179
                    sprintf(buffer, "XBEE: parsing recieved explicit command frame\r\n");
1180
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1181
                    break;
1182
                case XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
157 Kevin 1183
                    sprintf(buffer, "XBEE: parsing recieved remote AT command frame\r\n");
1184
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1185
                    rx_remote_at_cmd_frame = (void *) buffer;
1186
                    break;
1187
                case XBEE_RX_ROUTE_RECORD:
157 Kevin 1188
                    sprintf(buffer, "XBEE: parsing recieved route record frame\r\n");
1189
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1190
                    break;
1191
                case XBEE_RX_NODE_IDENTIFICATION:
157 Kevin 1192
                    sprintf(buffer, "XBEE: parsing recieved node identification frame\r\n");
1193
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1194
                    rx_node_ident_frame = (void *) buffer;
1195
                    XBee_Convert_Endian_64(&(rx_node_ident_frame->source_64));
1196
                    XBee_Convert_Endian_16(&(rx_node_ident_frame->source_16));
1197
                    XBee_Convert_Endian_64(&(rx_node_ident_frame->remote_64));
1198
                    XBee_Convert_Endian_16(&(rx_node_ident_frame->remote_16));
1199
                    XBee_Convert_Endian_16(&(rx_node_ident_frame->parent_16));
157 Kevin 1200
//                    DBG_PRINT_MAIN("Source 64: %08lX %08lX  Source 16: %04X  Options: %02X\r\n", \\
1201
//                            rx_node_ident_frame->source_64.UPPER_32.long_value, \\
1202
//                            rx_node_ident_frame->source_64.LOWER_32.long_value, \\
1203
//                            rx_node_ident_frame->source_16.INT_16.int_value, \\
1204
//                            rx_node_ident_frame->recieve_options);
1205
//                    DBG_PRINT_MAIN("Remote 64: %08lX %08lX  Remote 16: %04X  Parent 16: %04X\r\n", \\
1206
//                            rx_node_ident_frame->remote_64.UPPER_32.long_value, \\
1207
//                            rx_node_ident_frame->remote_64.LOWER_32.long_value, \\
1208
//                            rx_node_ident_frame->remote_16.INT_16.int_value, \\
1209
//                            rx_node_ident_frame->parent_16.INT_16.int_value);
1210
//                    DBG_PRINT_MAIN("Device Type: %02X  Source Event: %02X\r\n", \\
1211
//                            rx_node_ident_frame->device_type, rx_node_ident_frame->source_event);
155 Kevin 1212
                    break;
1213
                case XBEE_RX_FRAME_MODEM_STATUS:
157 Kevin 1214
                    sprintf(buffer, "XBEE: parsing recieved modem status frame\r\n");
1215
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1216
                    rx_modem_status_frame = (void *) buffer;
157 Kevin 1217
//                    DBG_PRINT_MAIN("Status: %02X\r\n", rx_modem_status_frame->status);
155 Kevin 1218
                    break;
1219
                default:
157 Kevin 1220
                    sprintf(buffer, "??\r\n");
1221
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1222
                    break;
1223
            }
1224
        }
1225
#endif
1226
 
152 Kevin 1227
    }
1228
}
1229
#else
1230
int main() {
157 Kevin 1231
 
152 Kevin 1232
}
157 Kevin 1233
#endif