Subversion Repositories Code-Repo

Rev

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

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