Subversion Repositories Code-Repo

Rev

Rev 152 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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