Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
147 Kevin 1
#include "defines.h"
119 Kevin 2
#include "interrupts.h"
3
#include "uart.h"
120 Kevin 4
#include "i2c.h"
121 Kevin 5
#include "spi.h"
148 Kevin 6
#include "nfc_PN532.h"
147 Kevin 7
#include "led_HT16K33.h"
121 Kevin 8
#include "oled_ssd1306.h"
9
#include "oled_ssd1331.h"
123 Kevin 10
#include "adc.h"
127 Kevin 11
#include "xbee.h"
147 Kevin 12
#include "timers.h"
148 Kevin 13
#include "lux_TSL2561.h"
14
#include "oled_NHD-0216KZW-AB5.h"
119 Kevin 15
#include <delays.h>
121 Kevin 16
#include <string.h>
119 Kevin 17
 
127 Kevin 18
#ifdef USE_EXT_OSC
119 Kevin 19
#pragma config OSC = HSPLL          // Use external oscillator (101)
20
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
127 Kevin 21
#else
22
#pragma config OSC = INTOSCPLL          // Use internal oscillator
23
#pragma config PLLDIV = 2           // Set PPL prescaler to 2 (to get 4MHz)
24
#endif
147 Kevin 25
 
26
#pragma config WDTEN = OFF          // Turn off watchdog timer
27
#pragma config XINST = OFF          // Turn off extended instruction set
119 Kevin 28
#pragma config CFGPLLEN = ON        // Enable PLL on startup
29
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
147 Kevin 30
#pragma config SOSCSEL = LOW        // Low Power T1OSC/SOSC circuit selected
126 Kevin 31
//#pragma config ADCSEL = BIT12       // 12-bit ADrC
119 Kevin 32
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
33
 
121 Kevin 34
#ifdef _TEST_UART
119 Kevin 35
 
36
void main(void) {
121 Kevin 37
    unsigned char length = 0;
38
    unsigned char buffer[100];
39
 
40
    /* --------------------- Oscillator Configuration --------------------- */
41
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
42
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
43
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
44
    /* -------------------------------------------------------------------- */
45
 
46
    // Set all ports as digial I/O
47
    ANCON0 = 0xFF;
48
    ANCON1 = 0x1F;
49
 
50
    UART1_Init(); // Initialize the UART handler code
51
 
52
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
53
    interrupt_init(); // Initialize the interrupt priorities
54
 
55
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
56
 
57
    while (1) {
58
 
127 Kevin 59
        length = UART1_Read_Buffer((unsigned char *) buffer);
121 Kevin 60
        if (length != 0) {
61
            UART1_WriteB((char *) buffer, length);
62
        }
63
 
64
        Delay10KTCYx(255);
65
        Delay10KTCYx(255);
66
    }
67
}
68
#endif
69
 
70
#ifdef _TEST_I2C_MASTER
71
 
72
void main(void) {
119 Kevin 73
    unsigned char i = 0;
74
    unsigned char length = 0;
120 Kevin 75
    unsigned char result = 0;
119 Kevin 76
    unsigned char buffer[100];
121 Kevin 77
 
119 Kevin 78
    /* --------------------- Oscillator Configuration --------------------- */
121 Kevin 79
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
80
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
119 Kevin 81
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
82
    /* -------------------------------------------------------------------- */
83
 
84
    // Set all ports as digial I/O
85
    ANCON0 = 0xFF;
86
    ANCON1 = 0x1F;
87
 
88
    UART1_Init(); // Initialize the UART handler code
120 Kevin 89
    I2C_Init(); // Initialize the I2C handler code
121 Kevin 90
 
147 Kevin 91
    I2C_Configure_Master(I2C_100KHZ);
121 Kevin 92
 
119 Kevin 93
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
94
    interrupt_init(); // Initialize the interrupt priorities
95
 
96
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
97
 
121 Kevin 98
    while (1) {
99
 
147 Kevin 100
        buffer[0] = 0x8;
121 Kevin 101
 
147 Kevin 102
        I2C_Master_Send(0x24, 1, buffer);
148 Kevin 103
        do {
121 Kevin 104
            result = I2C_Get_Status();
148 Kevin 105
        } while (!result);
121 Kevin 106
        DBG_PRINT_MAIN("S:%X ", result);
107
 
147 Kevin 108
        I2C_Master_Recv(0x24, 2);
148 Kevin 109
        do {
121 Kevin 110
            result = I2C_Get_Status();
148 Kevin 111
        } while (!result);
121 Kevin 112
        DBG_PRINT_MAIN("S:%X ", result);
113
        length = I2C_Read_Buffer(buffer);
114
        DBG_PRINT_MAIN("L:%d D:", length);
115
        for (i = 0; i < length; i++) {
116
            DBG_PRINT_MAIN("%c ", buffer[i]);
117
        }
118
 
147 Kevin 119
//        I2C_Master_Restart(0x30, 0xBB, 2);
120
//        result = I2C_Get_Status();
121
//        while (!result) {
122
//            result = I2C_Get_Status();
123
//        }
124
//        DBG_PRINT_MAIN("S:%X ", result);
125
//        length = I2C_Read_Buffer(buffer);
126
//        DBG_PRINT_MAIN("L:%d D:", length);
127
//        for (i = 0; i < length; i++) {
128
//            DBG_PRINT_MAIN("%c ", buffer[i]);
129
//        }
121 Kevin 130
 
131
        DBG_PRINT_MAIN("\r\n");
132
 
133
        Delay10KTCYx(255);
134
        Delay10KTCYx(255);
135
    }
136
}
137
#endif
138
 
139
#ifdef _TEST_I2C_SLAVE
140
 
141
void main(void) {
142
    unsigned char i = 0;
143
    unsigned char length = 0;
144
    unsigned char result = 0;
145
    unsigned char buffer[100];
146
 
147
    /* --------------------- Oscillator Configuration --------------------- */
148
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
149
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
150
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
151
    /* -------------------------------------------------------------------- */
152
 
153
    // Set all ports as digial I/O
154
    ANCON0 = 0xFF;
155
    ANCON1 = 0x1F;
156
 
157
    UART1_Init(); // Initialize the UART handler code
158
    I2C_Init(); // Initialize the I2C handler code
159
 
147 Kevin 160
    I2C_Configure_Slave(0x24);
120 Kevin 161
 
121 Kevin 162
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
163
    interrupt_init(); // Initialize the interrupt priorities
164
 
165
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
166
 
119 Kevin 167
    while (1) {
120 Kevin 168
 
169
        result = I2C_Get_Status();
170
        while (!result) {
171
            result = I2C_Get_Status();
119 Kevin 172
        }
120 Kevin 173
        DBG_PRINT_MAIN("%X ", result);
174
        length = I2C_Read_Buffer(buffer);
175
        DBG_PRINT_MAIN("%d ", length);
176
        for (i = 0; i < length; i++) {
177
            DBG_PRINT_MAIN("%X ", buffer[i]);
178
        }
179
        DBG_PRINT_MAIN("\r\n");
121 Kevin 180
 
181
        Delay10KTCYx(255);
182
        Delay10KTCYx(255);
119 Kevin 183
    }
121 Kevin 184
}
185
#endif
186
 
187
#ifdef _TEST_NFC
188
 
189
void main(void) {
126 Kevin 190
    unsigned char i, length = 0;
121 Kevin 191
 
192
    // NFC stuff
126 Kevin 193
    NFC_FIRMWARE_VERSION version;
194
    NFC_TargetDataMiFare cardData[2];
195
    NFC_TargetDataMiFare cardData_prev[2];
121 Kevin 196
 
197
    /* --------------------- Oscillator Configuration --------------------- */
198
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
199
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
200
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
201
    /* -------------------------------------------------------------------- */
202
 
203
    // Set all ports as digial I/O
204
    ANCON0 = 0xFF;
205
    ANCON1 = 0x1F;
206
 
207
    UART1_Init(); // Initialize the UART handler code
208
    I2C_Init(); // Initialize the I2C handler code
209
    NFC_Init(); // Initialize the NFC chip (uses I2C)
210
 
211
    I2C_Configure_Master(I2C_400KHZ);
212
 
213
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
214
    interrupt_init(); // Initialize the interrupt priorities
215
 
216
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
217
 
218
    version = NFC_getFirmwareVersion();
219
    while (!version.IC) {
220
        DBG_PRINT_MAIN("Waiting for NFC board..\r\n");
221
        Delay10KTCYx(3);
222
        version = NFC_getFirmwareVersion();
223
    }
224
    DBG_PRINT_MAIN("Found chip PN5%X\r\n", version.IC);
225
    DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
226
    NFC_SAMConfig();
227
 
126 Kevin 228
    memset(cardData, 0, 24);
127 Kevin 229
 
121 Kevin 230
    while (1) {
231
 
127 Kevin 232
        //        // This query will hang until the NFC chip replies (card detected)
233
        //        length = NFC_readPassiveTargetID(cardData);
234
        //        if (length) {
235
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
236
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
237
        //            DBG_PRINT_MAIN("UID: ");
238
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
239
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
240
        //            }
241
        //            DBG_PRINT_MAIN("\r\n");
242
        //            if (length == 2) {
243
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
244
        //                DBG_PRINT_MAIN("UID: ");
245
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
246
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
247
        //                }
248
        //                DBG_PRINT_MAIN("\r\n");
249
        //            }
250
        //        }
251
 
129 Kevin 252
        //        // This query will hang until the NFC chip replies (card detected)
253
        //        length = NFC_readPassiveTargetID(cardData);
254
        //        if (length) {
255
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
256
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
257
        //            DBG_PRINT_MAIN("UID: ");
258
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
259
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
260
        //            }
261
        //            DBG_PRINT_MAIN("\r\n");
262
        //            if (length == 2) {
263
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
264
        //                DBG_PRINT_MAIN("UID: ");
265
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
266
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
267
        //                }
268
        //                DBG_PRINT_MAIN("\r\n");
269
        //            }
270
        //        }
271
 
126 Kevin 272
        // This query will not wait for a detection before responding
273
        length = NFC_pollTargets(1, 1, cardData);
274
        if (!length) {
275
            memset(cardData_prev, 0, 24);
276
        } else if (length == 1) {
277
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
278
                // Do nothing
279
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
127 Kevin 280
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
126 Kevin 281
            } else {
282
                DBG_PRINT_MAIN("UID: ");
283
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
284
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
285
                }
286
                DBG_PRINT_MAIN("\r\n");
127 Kevin 287
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
121 Kevin 288
            }
126 Kevin 289
            memset(&cardData_prev[1], 0, 12);
290
        } else if (length == 2) {
291
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
292
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
293
                // Do nothing
294
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
295
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
127 Kevin 296
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
297
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
126 Kevin 298
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
299
                // First card matched
300
                DBG_PRINT_MAIN("UID2: ");
301
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
302
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
303
                }
304
                DBG_PRINT_MAIN("\r\n");
127 Kevin 305
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
126 Kevin 306
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
307
                // Second card matched
308
                DBG_PRINT_MAIN("UID1: ");
309
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
310
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
311
                }
312
                DBG_PRINT_MAIN("\r\n");
127 Kevin 313
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
126 Kevin 314
            } else {
315
                // No match
316
                DBG_PRINT_MAIN("UID1: ");
317
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
318
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
319
                }
320
                DBG_PRINT_MAIN("\r\n");
127 Kevin 321
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
126 Kevin 322
                DBG_PRINT_MAIN("UID2: ");
323
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
324
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
325
                }
326
                DBG_PRINT_MAIN("\r\n");
127 Kevin 327
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
126 Kevin 328
            }
121 Kevin 329
        }
330
    }
331
}
332
#endif
333
 
334
#ifdef _TEST_LED_BACKPACK
335
 
336
void main(void) {
337
    unsigned char i = 0;
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
147 Kevin 454
    SPI2_Init(SPI2_FOSC_4); // Initialize the SPI module
121 Kevin 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
147 Kevin 572
    SPI2_Init(SPI2_FOSC_64); // Initialize the SPI module
121 Kevin 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();
147 Kevin 581
 
121 Kevin 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);
147 Kevin 594
        SSD1331_Write_String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabit adipiscing ante sed nibh tincidunt feugiat.");
121 Kevin 595
 
147 Kevin 596
//        Delay10KTCYx(255);
597
//        Delay10KTCYx(255);
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");
602
//
603
//        Delay10KTCYx(255);
604
//        Delay10KTCYx(255);
605
//        SSD1331_Set_Rotation(0);
606
//        SSD1331_Test_DrawLines(SSD1331_YELLOW);
607
//
608
//        Delay10KTCYx(255);
609
//        Delay10KTCYx(255);
610
//        SSD1331_Set_Rotation(3);
611
//        SSD1331_Test_DrawLines(SSD1331_BLUE);
121 Kevin 612
 
147 Kevin 613
//        Delay10KTCYx(255);
614
//        Delay10KTCYx(255);
615
//        SSD1331_Set_Rotation(0);
616
//        SSD1331_Test_DrawRect(SSD1331_GREEN);
617
//
618
//        Delay10KTCYx(255);
619
//        Delay10KTCYx(255);
620
//        SSD1331_Set_Rotation(1);
621
//        SSD1331_Test_DrawRect(SSD1331_RED);
622
//
623
//        Delay10KTCYx(255);
624
//        Delay10KTCYx(255);
625
//        SSD1331_Set_Rotation(2);
626
//        SSD1331_Test_DrawRect(SSD1331_BLUE);
627
//
628
//        Delay10KTCYx(255);
629
//        Delay10KTCYx(255);
630
//        SSD1331_Set_Rotation(3);
631
//        SSD1331_Test_DrawRect(SSD1331_YELLOW);
632
//
633
//        Delay10KTCYx(255);
634
//        Delay10KTCYx(255);
635
//        SSD1331_Set_Rotation(0);
636
//        SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
637
//
638
//        Delay10KTCYx(255);
639
//        Delay10KTCYx(255);
640
//        SSD1331_Set_Rotation(3);
641
//        SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
121 Kevin 642
 
147 Kevin 643
//        Delay10KTCYx(255);
644
//        Delay10KTCYx(255);
645
//        SSD1331_Set_Rotation(0);
646
//        SSD1331_Clear_Display();
647
//        SSD1331_Test_FillCircle(10, SSD1331_BLUE);
648
//        SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
649
//
650
//        Delay10KTCYx(255);
651
//        Delay10KTCYx(255);
652
//        SSD1331_Set_Rotation(3);
653
//        SSD1331_Clear_Display();
654
//        SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
655
//        SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
656
//
657
//        Delay10KTCYx(255);
658
//        Delay10KTCYx(255);
659
//        SSD1331_Set_Rotation(0);
660
//        SSD1331_Test_DrawTria();
661
//
662
//        Delay10KTCYx(255);
663
//        Delay10KTCYx(255);
664
//        SSD1331_Set_Rotation(3);
665
//        SSD1331_Test_DrawTria();
121 Kevin 666
 
147 Kevin 667
//        Delay10KTCYx(255);
668
//        Delay10KTCYx(255);
669
//        SSD1331_Set_Rotation(0);
670
//        SSD1331_Test_DrawRoundRect();
671
//
672
//        Delay10KTCYx(255);
673
//        Delay10KTCYx(255);
674
//        SSD1331_Set_Rotation(3);
675
//        SSD1331_Test_DrawRoundRect();
121 Kevin 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
 
147 Kevin 907
#ifdef _TEST_NFC_TO_SSD1306_OLED
121 Kevin 908
 
909
void main(void) {
910
    unsigned char length = 0;
911
 
127 Kevin 912
    // NFC stuff
913
    NFC_FIRMWARE_VERSION version;
914
    NFC_TargetDataMiFare cardData[2];
915
    NFC_TargetDataMiFare cardData_prev[2];
916
 
121 Kevin 917
    /* --------------------- Oscillator Configuration --------------------- */
918
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
919
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
920
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
921
    /* -------------------------------------------------------------------- */
922
 
123 Kevin 923
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
924
    ANCON0 = 0xF8;
121 Kevin 925
    ANCON1 = 0x1F;
926
 
127 Kevin 927
    UART1_Init();
928
    I2C_Init();
929
    NFC_Init();
147 Kevin 930
    SPI2_Init(SPI2_FOSC_4);
129 Kevin 931
    SSD1306_Init();
121 Kevin 932
 
126 Kevin 933
    I2C_Configure_Master(I2C_400KHZ);
127 Kevin 934
 
121 Kevin 935
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
936
    interrupt_init(); // Initialize the interrupt priorities
937
 
938
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
939
 
129 Kevin 940
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
127 Kevin 941
    memset(cardData, 0, 24);
942
    memset(cardData_prev, 0, 24);
129 Kevin 943
    SSD1306_Clear_Display();
944
    SSD1306_Set_Rotation(0);
945
    SSD1306_Set_Cursor(0, 0);
122 Kevin 946
 
127 Kevin 947
    version = NFC_getFirmwareVersion();
948
    while (!version.IC) {
129 Kevin 949
        SSD1306_Write_String("Waiting for NFC board..\r");
950
        SSD1306_Display();
127 Kevin 951
        Delay10KTCYx(3);
952
        version = NFC_getFirmwareVersion();
953
    }
129 Kevin 954
    SSD1306_Write_String("PN5%X Ver. %d.%d\r", version.IC, version.Ver, version.Rev);
955
    SSD1306_Display();
127 Kevin 956
    NFC_SAMConfig();
957
 
121 Kevin 958
    while (1) {
959
 
127 Kevin 960
        // This query will not wait for a detection before responding
961
        length = NFC_pollTargets(1, 1, cardData);
962
        if (!length) {
963
            memset(cardData_prev, 0, 24);
964
        } else if (length == 1) {
965
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
966
                // Do nothing
967
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
968
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
969
            } else {
129 Kevin 970
                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]);
971
                SSD1306_Display();
127 Kevin 972
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
973
            }
974
            memset(&cardData_prev[1], 0, 12);
975
        } else if (length == 2) {
976
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
977
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
978
                // Do nothing
979
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
980
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
981
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
982
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
983
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
984
                // First card matched
129 Kevin 985
                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]);
986
                SSD1306_Display();
127 Kevin 987
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
988
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
989
                // Second card matched
129 Kevin 990
                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]);
991
                SSD1306_Display();
127 Kevin 992
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
993
            } else {
994
                // No match
129 Kevin 995
                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]);
996
                SSD1306_Display();
127 Kevin 997
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
129 Kevin 998
                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]);
999
                SSD1306_Display();
127 Kevin 1000
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
1001
            }
126 Kevin 1002
        }
121 Kevin 1003
    }
1004
}
1005
#endif
147 Kevin 1006
 
1007
#ifdef _TEST_TIMER1_RTC
1008
 
1009
void main(void) {
1010
 
1011
    /* --------------------- Oscillator Configuration --------------------- */
1012
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
1013
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
1014
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
1015
    /* -------------------------------------------------------------------- */
1016
 
1017
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1018
    ANCON0 = 0xF8;
1019
    ANCON1 = 0x1F;
1020
 
1021
    Timer1_Init();
1022
 
1023
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
1024
    interrupt_init(); // Initialize the interrupt priorities
1025
 
1026
    LED_BLUE_TRIS = 0;
1027
    LED_RED_TRIS = 0;
1028
 
1029
    Timer1_Enable();
1030
 
1031
    while (1) {
1032
 
1033
    }
1034
}
1035
#endif
1036
 
1037
#ifdef _TEST_LUX
1038
 
1039
void main(void) {
148 Kevin 1040
    unsigned int ir, full;
1041
    unsigned long lum;
147 Kevin 1042
 
1043
    /* --------------------- Oscillator Configuration --------------------- */
1044
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
1045
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
1046
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
1047
    /* -------------------------------------------------------------------- */
1048
 
1049
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1050
    ANCON0 = 0xF8;
1051
    ANCON1 = 0x1F;
1052
 
1053
    UART1_Init();
1054
    I2C_Init();
148 Kevin 1055
    LUX_Init(TSL2561_ADDR_FLOAT);
147 Kevin 1056
 
1057
    I2C_Configure_Master(I2C_100KHZ);
1058
 
1059
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
1060
    interrupt_init(); // Initialize the interrupt priorities
1061
 
148 Kevin 1062
    LUX_Begin();
1063
 
1064
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
1065
    LUX_SetGain(TSL2561_GAIN_0X);   // set no gain (for bright situtations)
1066
//    LUX_SetGain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
1067
 
1068
    // Changing the integration time gives you a longer time over which to sense light
1069
    // longer timelines are slower, but are good in very low light situtations!
1070
    LUX_SetTiming(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)
1071
//    LUX_SetTiming(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
1072
//    LUX_SetTiming(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
1073
 
147 Kevin 1074
    while (1) {
148 Kevin 1075
        lum = LUX_GetFullLuminosity();
1076
        ir = lum >> 16;
1077
        full = lum & 0xFFFF;
1078
        DBG_PRINT_LUX("IR: %d\r\n", ir);
1079
        DBG_PRINT_LUX("Visible: %d\r\n", full - ir);
1080
        DBG_PRINT_LUX("Full: %d\r\n", full);
1081
        DBG_PRINT_LUX("Lux: %ld\r\n\r\n", LUX_CalculateLux(full, ir));
147 Kevin 1082
 
148 Kevin 1083
        Delay10KTCYx(255);
1084
        Delay10KTCYx(255);
1085
        Delay10KTCYx(255);
1086
        Delay10KTCYx(255);
147 Kevin 1087
    }
1088
}
1089
#endif
1090
 
148 Kevin 1091
#ifdef _TEST_OLED_CHAR
1092
 
1093
void main(void) {
1094
    int i;
1095
    unsigned char *buffer = "Test String";
1096
 
1097
    /* --------------------- Oscillator Configuration --------------------- */
1098
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
1099
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
1100
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
1101
    /* -------------------------------------------------------------------- */
1102
 
1103
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1104
    ANCON0 = 0xFF;
1105
    ANCON1 = 0x1F;
1106
 
1107
//    UART1_Init();
1108
    NHD_Init();
1109
 
1110
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
1111
    interrupt_init(); // Initialize the interrupt priorities
1112
 
1113
    NHD_Begin(16, 2);
1114
 
1115
    NHD_Write_String("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do");
1116
    NHD_Set_Cursor(0,1);
1117
    NHD_Write_String("eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e");
1118
 
1119
    while (1) {
1120
        Delay10KTCYx(150);
1121
        NHD_Scroll_Display_Left();
1122
    }
1123
}
1124
#endif
1125
 
147 Kevin 1126
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
1127
    !defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
1128
    !defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
1129
    !defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
1130
    !defined(_TEST_ADC) && !defined(_TEST_XBEE) && \
1131
    !defined(_TEST_NFC_TO_SSD1306_OLED) && !defined(_TEST_TIMER1_RTC) && \
148 Kevin 1132
    !defined(_TEST_LUX) && !defined(_TEST_OLED_CHAR)
147 Kevin 1133
 
1134
void main(void) {
149 Kevin 1135
    unsigned int ir, full;
1136
    unsigned long lum;
1137
 
147 Kevin 1138
    /* --------------------- Oscillator Configuration --------------------- */
1139
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
1140
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
1141
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
1142
    /* -------------------------------------------------------------------- */
1143
 
1144
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1145
    ANCON0 = 0xF8;
1146
    ANCON1 = 0x1F;
1147
 
149 Kevin 1148
//    UART1_Init();
1149
    I2C_Init();
1150
    NHD_Init();
1151
    LUX_Init(TSL2561_ADDR_FLOAT);
147 Kevin 1152
 
149 Kevin 1153
    I2C_Configure_Master(I2C_400KHZ);
1154
 
1155
 
147 Kevin 1156
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
1157
    interrupt_init(); // Initialize the interrupt priorities
1158
 
149 Kevin 1159
    NHD_Begin(16, 2);
147 Kevin 1160
 
149 Kevin 1161
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
1162
//    LUX_SetGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
1163
    LUX_SetGain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
147 Kevin 1164
 
149 Kevin 1165
    // Changing the integration time gives you a longer time over which to sense light
1166
    // longer timelines are slower, but are good in very low light situtations!
1167
    LUX_SetTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
1168
    //    LUX_SetTiming(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
1169
    //    LUX_SetTiming(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
1170
 
147 Kevin 1171
    while (1) {
149 Kevin 1172
        lum = LUX_GetFullLuminosity();
1173
        ir = lum >> 16;
1174
        full = lum & 0xFFFF;
1175
        NHD_Clear();
1176
        NHD_Set_Cursor(0, 0);
1177
        NHD_Write_String("I: %d ", ir);
1178
        NHD_Write_String("V: %d", full - ir);
1179
        NHD_Set_Cursor(0, 1);
1180
        NHD_Write_String("Lux: %ld", LUX_CalculateLux(full, ir));
147 Kevin 1181
 
149 Kevin 1182
        Delay10KTCYx(255);
147 Kevin 1183
    }
1184
}
1185
#endif