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