Rev 126 | Blame | Last modification | View Log | RSS feed
#include "maindefs.h"
#include "Adafruit_GFX.h"
#include "interrupts.h"
#include "uart.h"
#include "i2c.h"
#include "spi.h"
#include "nfc.h"
#include "led_backpack.h"
#include "oled_ssd1306.h"
#include "oled_ssd1331.h"
#include "adc.h"
#include "xbee.h"
#include <delays.h>
#include <string.h>
#pragma config WDTEN = OFF // Turn off watchdog timer
#pragma config XINST = OFF // Turn off extended instruction set
#ifdef USE_EXT_OSC
#pragma config OSC = HSPLL // Use external oscillator (101)
#pragma config PLLDIV = 3 // Set PPL prescaler to 3 (to get 4MHz)
#else
#pragma config OSC = INTOSCPLL // Use internal oscillator
#pragma config PLLDIV = 2 // Set PPL prescaler to 2 (to get 4MHz)
#endif
#pragma config CFGPLLEN = ON // Enable PLL on startup
#pragma config PLLSEL = PLL96 // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
//#pragma config SOSCSEL = HIGH // High Power T1OSC/SOSC circuit selected
//#pragma config ADCSEL = BIT12 // 12-bit ADrC
//#pragma config IOL1WAY = OFF // IOLOCK bit can be set and cleared as needed
/* ----------- IO Pins -----------
* RA0 - A/D X
* RA1 - A/D Y
* RA2 - A/D Z
* RA3 -
* RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
* RA5 -
* RA6 - Oscillator
* RA7 - Oscillator
*
* RB0 - UART2 Tx
* RB1 - UART2 Rx
* RB2 - SPI2 MOSI
* RB3 - SPI2 MISO
* RB4 - SPI2 CLK
* RB5 - SPI2 D/C
* RB6 - SPI2 RESET
* RB7 - SPI2 CS
*
* RC0 -
* RC1 - IRQ
* RC2 - Reset
* RC3 - I2C CLK
* RC4 - I2C DATA
* RC5 -
* RC6 - UART1 Tx
* RC7 - UART1 Rx
* ---------------------------- */
#ifdef _TEST_UART
void main(void) {
unsigned char length = 0;
unsigned char buffer[100];
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
while (1) {
length = UART1_Read_Buffer((unsigned char *) buffer);
if (length != 0) {
UART1_WriteB((char *) buffer, length);
}
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#endif
#ifdef _TEST_I2C_MASTER
void main(void) {
unsigned char i = 0;
unsigned char length = 0;
unsigned char result = 0;
unsigned char buffer[100];
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
I2C_Init(); // Initialize the I2C handler code
I2C_Configure_Master();
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
while (1) {
buffer[0] = 0xBB;
I2C_Master_Send(0x30, 1, buffer);
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
DBG_PRINT_MAIN("S:%X ", result);
I2C_Master_Recv(0x30, 2);
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
DBG_PRINT_MAIN("S:%X ", result);
length = I2C_Read_Buffer(buffer);
DBG_PRINT_MAIN("L:%d D:", length);
for (i = 0; i < length; i++) {
DBG_PRINT_MAIN("%c ", buffer[i]);
}
I2C_Master_Restart(0x30, 0xBB, 2);
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
DBG_PRINT_MAIN("S:%X ", result);
length = I2C_Read_Buffer(buffer);
DBG_PRINT_MAIN("L:%d D:", length);
for (i = 0; i < length; i++) {
DBG_PRINT_MAIN("%c ", buffer[i]);
}
DBG_PRINT_MAIN("\r\n");
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#endif
#ifdef _TEST_I2C_SLAVE
void main(void) {
unsigned char i = 0;
unsigned char length = 0;
unsigned char result = 0;
unsigned char buffer[100];
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
I2C_Init(); // Initialize the I2C handler code
I2C_Configure_Slave(0x30);
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
while (1) {
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
DBG_PRINT_MAIN("%X ", result);
length = I2C_Read_Buffer(buffer);
DBG_PRINT_MAIN("%d ", length);
for (i = 0; i < length; i++) {
DBG_PRINT_MAIN("%X ", buffer[i]);
}
DBG_PRINT_MAIN("\r\n");
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#endif
#ifdef _TEST_NFC
void main(void) {
unsigned char i, length = 0;
// NFC stuff
NFC_FIRMWARE_VERSION version;
NFC_TargetDataMiFare cardData[2];
NFC_TargetDataMiFare cardData_prev[2];
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
I2C_Init(); // Initialize the I2C handler code
NFC_Init(); // Initialize the NFC chip (uses I2C)
I2C_Configure_Master(I2C_400KHZ);
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
version = NFC_getFirmwareVersion();
while (!version.IC) {
DBG_PRINT_MAIN("Waiting for NFC board..\r\n");
Delay10KTCYx(3);
version = NFC_getFirmwareVersion();
}
DBG_PRINT_MAIN("Found chip PN5%X\r\n", version.IC);
DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
NFC_SAMConfig();
memset(cardData, 0, 24);
while (1) {
// // This query will hang until the NFC chip replies (card detected)
// length = NFC_readPassiveTargetID(cardData);
// if (length) {
// DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
// DBG_PRINT_MAIN("UID: ");
// for (i = 0; i < cardData[0].NFCID_LEN; i++) {
// DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
// }
// DBG_PRINT_MAIN("\r\n");
// if (length == 2) {
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
// DBG_PRINT_MAIN("UID: ");
// for (i = 0; i < cardData[1].NFCID_LEN; i++) {
// DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
// }
// DBG_PRINT_MAIN("\r\n");
// }
// }
// // This query will hang until the NFC chip replies (card detected)
// length = NFC_readPassiveTargetID(cardData);
// if (length) {
// DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
// DBG_PRINT_MAIN("UID: ");
// for (i = 0; i < cardData[0].NFCID_LEN; i++) {
// DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
// }
// DBG_PRINT_MAIN("\r\n");
// if (length == 2) {
// DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
// DBG_PRINT_MAIN("UID: ");
// for (i = 0; i < cardData[1].NFCID_LEN; i++) {
// DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
// }
// DBG_PRINT_MAIN("\r\n");
// }
// }
// This query will not wait for a detection before responding
length = NFC_pollTargets(1, 1, cardData);
if (!length) {
memset(cardData_prev, 0, 24);
} else if (length == 1) {
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
// Do nothing
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
} else {
DBG_PRINT_MAIN("UID: ");
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
}
memset(&cardData_prev[1], 0, 12);
} else if (length == 2) {
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
// Do nothing
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
// First card matched
DBG_PRINT_MAIN("UID2: ");
for (i = 0; i < cardData[1].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
} else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
// Second card matched
DBG_PRINT_MAIN("UID1: ");
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
} else {
// No match
DBG_PRINT_MAIN("UID1: ");
for (i = 0; i < cardData[0].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
DBG_PRINT_MAIN("UID2: ");
for (i = 0; i < cardData[1].NFCID_LEN; i++) {
DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
}
DBG_PRINT_MAIN("\r\n");
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
}
}
}
}
#endif
#ifdef _TEST_LED_BACKPACK
void main(void) {
unsigned char i = 0;
unsigned char length = 0;
unsigned char buffer[100];
unsigned int counter = 0;
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
I2C_Init(); // Initialize the I2C handler code
LED_Init(); // Initialize the LED backpack (uses I2C);
I2C_Configure_Master(I2C_400KHZ);
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
LED_Start();
LED_writeDigitNum(0, 1, 1);
LED_writeDigitNum(1, 2, 0);
LED_writeDigitNum(2, 3, 0);
LED_writeDigitNum(3, 4, 0);
LED_writeDisplay();
for (i = 0; i < 15; i++) {
LED_setBrightness(15 - i);
Delay10KTCYx(100);
}
for (i = 0; i < 15; i++) {
LED_setBrightness(i);
Delay10KTCYx(100);
}
LED_blinkRate(HT16K33_BLINK_OFF);
while (1) {
LED_writeNum(counter);
counter++;
if (counter > 9999)
counter = 0;
// Delay10KTCYx(255);
}
}
#endif
#ifdef _TEST_SPI
void main(void) {
unsigned char i = 0;
unsigned char length = 0;
unsigned char result = 0;
unsigned char buffer[100];
unsigned char test[8] = "ASDF123";
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
while (1) {
SPI2_Write(test, 7);
while (result != 7) {
length = SPI2_Buffer_Read(buffer);
if (length) {
result += length;
}
}
result = 0;
for (i = 0; i < result; i++) {
DBG_PRINT_MAIN("%X ", buffer[i]);
}
DBG_PRINT_MAIN("\r\n");
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#endif
#ifdef _TEST_SSD1306_OLED
void main(void) {
unsigned int i = 0;
unsigned long l = 0;
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1306_Init(); // Initialize the OLED code
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
SSD1306_Display(); // Show splashscreen
while (1) {
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// for (i = 0; i < 32; i++) {
// SSD1306_Draw_Pixel(i, i, WHITE);
// }
// SSD1306_Display();
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_DrawLine();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_DrawRect();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_FillRect();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_DrawCircle();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// GFX_fillCircle(GFX_width() / 2, GFX_height() / 2, 10, WHITE);
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_DrawRoundRect();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_FillRoundRect();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_DrawTriangle();
// SSD1306_Display();
//
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_FillTriangle();
// SSD1306_Display();
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// SSD1306_Test_DrawChar();
// SSD1306_Display();
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1306_Clear_Display();
// GFX_setTextSize(1);
// GFX_setTextColor(WHITE);
// GFX_setCursor(0,0);
// GFX_writeString("Hello World!\n");
//// GFX_setTextColorBG(BLACK, WHITE);
// i = 65535;
// GFX_writeString("%u %d\n", i, i);
//// GFX_setTextSize(2);
//// GFX_setTextColor(WHITE);
// l = 0xDEADBEEF;
// GFX_writeString("0x%X", (long)l);
// SSD1306_Display();
SSD1306_Clear_Display();
GFX_setRotation(0);
GFX_setTextSize(1);
GFX_setTextColor(SSD1306_WHITE);
GFX_setCursor(0, 0);
GFX_writeString("%u", i);
i++;
SSD1306_Display();
}
}
#endif
#ifdef _TEST_SSD1331_OLED
void main(void) {
unsigned int i = 0;
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1331_Init(); // Initialize the OLED code
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
SSD1331_Begin();
while (1) {
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
SSD1331_Test_Pattern();
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_clearScreen();
GFX_setRotation(0);
GFX_setCursor(0, 0);
GFX_writeString("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_clearScreen();
GFX_setRotation(3);
GFX_setCursor(0, 0);
GFX_writeString("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
SSD1331_Test_DrawLines(SSD1331_YELLOW);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(3);
SSD1331_Test_DrawLines(SSD1331_BLUE);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
SSD1331_Test_DrawRect(SSD1331_GREEN);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(1);
SSD1331_Test_DrawRect(SSD1331_RED);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(2);
SSD1331_Test_DrawRect(SSD1331_BLUE);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(3);
SSD1331_Test_DrawRect(SSD1331_YELLOW);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(3);
SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
GFX_clearScreen();
SSD1331_Test_FillCircle(10, SSD1331_BLUE);
SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(3);
GFX_clearScreen();
SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
SSD1331_Test_DrawTria();
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(3);
SSD1331_Test_DrawTria();
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(0);
SSD1331_Test_DrawRoundRect();
Delay10KTCYx(255);
Delay10KTCYx(255);
GFX_setRotation(3);
SSD1331_Test_DrawRoundRect();
// GFX_clearScreen();
// GFX_setRotation(3);
// GFX_setCursor(0,0);
// GFX_setTextColorBG(SSD1331_WHITE, SSD1331_BLACK);
// GFX_writeString("%u", i);
// i++;
}
}
#endif
#ifdef _TEST_ADC
void main(void) {
unsigned int x, y, z;
unsigned char buffer[60];
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
ADC_Init(ADC_TAD_20, ADC_FOSC_64);
// I2C_Configure_Master(I2C_400KHZ);
SSD1331_Begin();
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
memset(buffer, 0, 60);
GFX_clearScreen();
GFX_setRotation(3);
while (1) {
// ADC read from AN0-AN2 and prints to display
ADC_Start(ADC_CHANNEL_AN2);
GFX_fillRect(0, 0, GFX_width(), 8, SSD1331_BLACK);
GFX_setCursor(0, 0);
while (!ADC_Get_Result(&x));
GFX_writeString("X: %u", x);
ADC_Start(ADC_CHANNEL_AN1);
GFX_fillRect(0, 8, GFX_width(), 8, SSD1331_BLACK);
GFX_setCursor(0, 8);
while (!ADC_Get_Result(&y));
GFX_writeString("Y: %u", y);
ADC_Start(ADC_CHANNEL_AN0);
GFX_fillRect(0, 16, GFX_width(), 8, SSD1331_BLACK);
GFX_setCursor(0, 16);
while (!ADC_Get_Result(&z));
GFX_writeString("Z: %u", z);
}
}
#endif
#ifdef _TEST_XBEE
void main(void) {
unsigned int i, length = 0;
unsigned char buffer[100];
XBEE_RX_DATA_PACKET_FRAME *rx_data_frame;
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
UART1_Init(); // Initialize the UART handler code
XBee_Init();
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
while (1) {
//#define _ROUTER
#define _COORDINATOR
#ifdef _ROUTER
XBEE_TX_DATA_PACKET_FRAME *tx_data_frame;
tx_data_frame = (void *) buffer;
tx_data_frame->frame_type = XBEE_TX_DATA_PACKET;
tx_data_frame->frame_id = 1;
tx_data_frame->destination_64.UPPER_32.long_value = 0x00000000;
tx_data_frame->destination_64.LOWER_32.long_value = 0x00000000;
tx_data_frame->destination_16.INT_16.int_value = 0xFEFF;
tx_data_frame->broadcast_radius = 0;
tx_data_frame->options = 0;
tx_data_frame->data[0] = 0x54;
tx_data_frame->data[1] = 0x78;
tx_data_frame->data[2] = 0x32;
tx_data_frame->data[3] = 0x43;
tx_data_frame->data[4] = 0x6F;
tx_data_frame->data[5] = 0x6F;
tx_data_frame->data[6] = 0x72;
tx_data_frame->data[7] = 0x11;
XBee_Process_Transmit_Frame(buffer, XBEE_TX_DATA_PACKET_FRAME_SIZE + 8);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
#endif
#ifdef _COORDINATOR
length = XBee_Get_Received_Frame(buffer);
if (length != 0) {
switch (*(unsigned char *) buffer) {
case XBEE_RX_AT_COMMAND_RESPONSE:
DBG_PRINT_MAIN("XBEE: parsing recieved AT command response frame\r\n");
break;
case XBEE_RX_DATA_PACKET:
rx_data_frame = (void *)buffer;
DBG_PRINT_MAIN("XBEE: parsing recieved data recieved frame\r\n");
XBee_ConvertEndian64(&(rx_data_frame->source_64));
XBee_ConvertEndian16(&(rx_data_frame->source_16));
DBG_PRINT_MAIN("Source 64: %08lX %08lX Source 16: %04X Options: %X\r\n", \\
rx_data_frame->source_64.UPPER_32.long_value, \\
rx_data_frame->source_64.LOWER_32.long_value, \\
rx_data_frame->source_16.INT_16.int_value, \\
rx_data_frame->recieve_options);
DBG_PRINT_MAIN("Data: ");
for (i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
DBG_PRINT_MAIN("%02X ", rx_data_frame->data[i]);
}
DBG_PRINT_MAIN("\r\n");
break;
case XBEE_RX_DATA_TX_STATUS:
DBG_PRINT_MAIN("XBEE: parsing recieved TX status frame\r\n");
break;
case XBEE_RX_IO_DATA_SAMPLE:
DBG_PRINT_MAIN("XBEE: parsing recieved IO data sample frame\r\n");
break;
case XBEE_RX_EXPLICIT_COMMAND:
DBG_PRINT_MAIN("XBEE: parsing recieved explicit command frame\r\n");
break;
case XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
DBG_PRINT_MAIN("XBEE: parsing recieved remote AT command frame\r\n");
break;
case XBEE_RX_ROUTE_RECORD:
DBG_PRINT_MAIN("XBEE: parsing recieved route record frame\r\n");
break;
case XBEE_RX_NODE_IDENTIFICATION:
DBG_PRINT_MAIN("XBEE: parsing recieved node identification frame\r\n");
break;
case XBEE_RX_FRAME_MODEM_STATUS:
DBG_PRINT_MAIN("XBEE: parsing recieved modem status frame\r\n");
break;
default:
DBG_PRINT_MAIN("??\r\n");
break;
}
}
#endif
}
}
#endif
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
!defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
!defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
!defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
!defined(_TEST_ADC) && !defined(_TEST_XBEE)
void main(void) {
unsigned char length = 0;
// NFC stuff
NFC_FIRMWARE_VERSION version;
NFC_TargetDataMiFare cardData[2];
NFC_TargetDataMiFare cardData_prev[2];
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
/* -------------------------------------------------------------------- */
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
UART1_Init();
I2C_Init();
NFC_Init();
SPI2_Init(SPI2_FOSC_8);
SSD1331_Init();
I2C_Configure_Master(I2C_400KHZ);
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
DBG_PRINT_MAIN("\r\nBegin Program\r\n");
SSD1331_Begin();
memset(cardData, 0, 24);
memset(cardData_prev, 0, 24);
GFX_clearScreen();
GFX_setRotation(0);
GFX_setCursor(0, 0);
version = NFC_getFirmwareVersion();
while (!version.IC) {
GFX_appendString("Waiting for NFC board..\r");
Delay10KTCYx(3);
version = NFC_getFirmwareVersion();
}
GFX_appendString("PN5%X Ver. %d.%d\r", version.IC, version.Ver, version.Rev);
NFC_SAMConfig();
while (1) {
// This query will not wait for a detection before responding
length = NFC_pollTargets(1, 1, cardData);
if (!length) {
memset(cardData_prev, 0, 24);
} else if (length == 1) {
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
// Do nothing
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
} else {
GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
}
memset(&cardData_prev[1], 0, 12);
} else if (length == 2) {
if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
// Do nothing
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
} else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
// First card matched
GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
} else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
// Second card matched
GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
} else {
// No match
GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
}
}
}
}
#endif