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