/PIC Stuff/PIC_27J13/adc.c |
---|
12,7 → 12,7 |
adc_data.result = 0; |
ADCON0bits.VCFG1 = 0; // VRef- = AVss |
ADCON0bits.VCFG0 = 0; // VRef+ = AVdd |
ADCON0bits.VCFG0 = 1; // VRef+ != AVdd |
ADCON1bits.ADFM = 1; // Right justified result |
ADCON1bits.ADCAL = 1; // Calibrate A/D |
ADCON1bits.ACQT = TAD; |
/PIC Stuff/PIC_27J13/adc.h |
---|
17,7 → 17,7 |
#define ADC_CHANNEL_VDDCORE 0b1110 |
#define ADC_CHANNEL_ABG 0b1111 |
#define ADC_TAD_20 0b111; |
#define ADC_TAD_20 0b111 |
#define ADC_TAD_16 0b110 |
#define ADC_TAD_12 0b101 |
#define ADC_TAD_8 0b100 |
/PIC Stuff/PIC_27J13/led_backpack.c |
---|
42,6 → 42,7 |
LED_blinkRate(HT16K33_BLINK_OFF); |
LED_setBrightness(15); // Max brightness |
LED_clear(); |
LED_writeDisplay(); |
} |
void LED_setBrightness(unsigned char c) { |
/PIC Stuff/PIC_27J13/main.c |
---|
20,7 → 20,7 |
#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 ADCSEL = BIT12 // 12-bit ADrC |
//#pragma config IOL1WAY = OFF // IOLOCK bit can be set and cleared as needed |
/* ----------- IO Pins ----------- |
210,15 → 210,12 |
#ifdef _TEST_NFC |
void main(void) { |
unsigned char i = 0; |
unsigned char length = 0; |
unsigned char result = 0; |
NFC_FIRMWARE_VERSION version; |
unsigned char buffer[50]; |
unsigned char i, length = 0; |
// NFC stuff |
unsigned char uid[7]; |
unsigned char uidLen; |
NFC_FIRMWARE_VERSION version; |
NFC_TargetDataMiFare cardData[2]; |
NFC_TargetDataMiFare cardData_prev[2]; |
/* --------------------- Oscillator Configuration --------------------- */ |
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL |
251,23 → 248,90 |
DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev); |
NFC_SAMConfig(); |
memset(cardData, 0, 24); |
while (1) { |
// This call will hang until the NFC chip replies (card detected) |
if (NFC_readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLen)) { |
DBG_PRINT_MAIN("Card Found!\r\n"); |
DBG_PRINT_MAIN("UID Length: %d bytes\r\n", uidLen); |
// // 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 < uidLen; i++) { |
DBG_PRINT_MAIN("%02X ", uid[i]); |
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); |
} |
// Delay10KTCYx(255); |
// Delay10KTCYx(255); |
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], &cardData[1], 12); |
} |
} |
} |
} |
#endif |
#ifdef _TEST_LED_BACKPACK |
652,7 → 716,7 |
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_0, ADC_FOSC_64); |
ADC_Init(ADC_TAD_20, ADC_FOSC_64); |
// I2C_Configure_Master(I2C_400KHZ); |
SSD1331_Begin(); |
697,7 → 761,6 |
!defined(_TEST_ADC) |
void main(void) { |
unsigned int x,y,z; |
unsigned char length = 0; |
unsigned char buffer[60]; |
712,15 → 775,13 |
ANCON1 = 0x1F; |
UART1_Init(); // Initialize the UART handler code |
// I2C_Init(); // Initialize the I2C handler code |
I2C_Init(); // Initialize the I2C handler code |
SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module |
SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2) |
// NFC_Init(); // Initialize the NFC chip (uses I2C) |
// LED_Init(); // Initialize the LED backpack (uses I2C) |
ADC_Init(ADC_TAD_0, ADC_FOSC_64); |
LED_Init(); // Initialize the LED backpack (uses I2C) |
// I2C_Configure_Master(I2C_400KHZ); |
SSD1331_Begin(); |
I2C_Configure_Master(I2C_400KHZ); |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts |
interrupt_init(); // Initialize the interrupt priorities |
727,6 → 788,9 |
DBG_PRINT_MAIN("\r\nBegin Program\r\n"); |
SSD1331_Begin(); |
LED_Start(); |
memset(buffer, 0, 60); |
GFX_clearScreen(); |
GFX_setRotation(3); |
734,30 → 798,12 |
while (1) { |
// Reads input from UART and prints to display |
// length = UART1_Read_Buffer(buffer); |
// if (length != 0) { |
// buffer[length] = 0; |
// GFX_appendString("%s", buffer); |
// } |
// ADC read from AN0-AN2 and prints to display |
ADC_Start(ADC_CHANNEL_AN0); |
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_AN2); |
GFX_fillRect(0,16,GFX_width(),8,SSD1331_BLACK); |
GFX_setCursor(0,16); |
while (!ADC_Get_Result(&z)); |
GFX_writeString("Z: %u", z); |
length = UART1_Read_Buffer(buffer); |
if (length != 0) { |
buffer[length] = 0; |
GFX_appendString("%s", buffer); |
LED_writeNum(length-1); |
} |
} |
} |
#endif |
/PIC Stuff/PIC_27J13/maindefs.h |
---|
2,10 → 2,9 |
#define __maindefs_h |
#include <p18f27j13.h> |
#include <stdio.h> |
#include "uart.h" |
//#define _DEBUG |
#define _DEBUG |
//#define _TEST_UART |
//#define _TEST_I2C_MASTER |
//#define _TEST_I2C_SLAVE |
14,7 → 13,7 |
//#define _TEST_LED_BACKPACK |
//#define _TEST_SSD1306_OLED |
//#define _TEST_SSD1331_OLED |
#define _TEST_ADC |
//#define _TEST_ADC |
// Option to choose between active OLED displays |
//#define GFX_SSD1306 |
22,10 → 21,10 |
// Enable or disable debug prints depending on project preprocessor (_DEBUG) |
#ifdef _DEBUG |
#define DBG_PRINT_MAIN(x) printf(x) |
#define DBG_PRINT_UART(x) printf(x) |
#define DBG_PRINT_I2C(x) printf(x) |
#define DBG_PRINT_SPI(x) printf(x) |
#define DBG_PRINT_MAIN(x) UART1_WriteS(x) |
#define DBG_PRINT_UART(x) UART1_WriteS(x) |
#define DBG_PRINT_I2C(x) UART1_WriteS(x) |
#define DBG_PRINT_SPI(x) UART1_WriteS(x) |
#define DBG_PRINT_XBEE(x) |
#define DBG_PRINT_PORTB_INT(x) |
#define DBG_PRINT_INT(x) |
/PIC Stuff/PIC_27J13/nfc.c |
---|
87,13 → 87,12 |
return 1; |
} |
// Waits for an ISO14443A target to enter the field |
unsigned char NFC_readPassiveTargetID(unsigned char cardbaudrate, unsigned char * uid, unsigned char * uidLength) { |
unsigned char i = 0; |
// Passive polling, waits for an ISO14443A target to enter the field |
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *cardData) { |
nfc_data.packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; |
nfc_data.packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) |
nfc_data.packetbuffer[2] = cardbaudrate; |
nfc_data.packetbuffer[1] = 2; // Max 2 cards at once |
nfc_data.packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 3)) |
return 0; |
101,35 → 100,112 |
// Wait for IRQ line |
while (NFC_I2C_Read_Status() != PN532_I2C_READY); |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 20); |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 35); |
/* ISO14443A card response should be in the following format: |
// (byte 0 is actually 0x01 (data ready)) |
byte Description |
------------- ------------------------------------------ |
b0..6 Frame header and preamble |
b7 Tags Found |
b8 Tag Number (only one used in this example) |
b9..10 SENS_RES |
b11 SEL_RES |
b12 NFCID Length |
b13..NFCIDLen NFCID */ |
/* InListPassiveTarget response should be in the following format: |
* Byte Description |
* ---------- ------------------ |
* b0 Data ACK |
* b1..7 Frame header and preamble |
* b8 Tags found |
* b9..N NFC_TargetDataMiFare[2] |
* bN+1..N+2 Checksum + postamble |
*/ |
// Check # of tags found |
if (nfc_data.packetbuffer[8] != 1) |
if (!nfc_data.packetbuffer[8]) |
return 0; |
// Save UID length |
*uidLength = nfc_data.packetbuffer[13]; |
// Save data from first card |
if (nfc_data.packetbuffer[13] == 4) { |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 9); |
} else { |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[9], 12); |
} |
// Save UID |
for (i = 0; i < *uidLength; i++) { |
uid[i] = nfc_data.packetbuffer[14+i]; |
// Save data from second card |
if (nfc_data.packetbuffer[8] == 2) { |
// Offset will vary depending on length of first card |
if (nfc_data.packetbuffer[13] == 4) { |
if (nfc_data.packetbuffer[22] == 4) { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[18], 9); |
} else { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[18], 12); |
} |
} else { // Length of first UID is 7 |
if (nfc_data.packetbuffer[25] == 4) { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 9); |
} else { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[21], 12); |
} |
} |
} |
return 1; |
// Return the number of cards detected |
return nfc_data.packetbuffer[8]; |
} |
// Active polling, returns number of cards in the field |
unsigned char NFC_pollTargets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *cardData) { |
nfc_data.packetbuffer[0] = PN532_COMMAND_INAUTOPOLL; |
nfc_data.packetbuffer[1] = number; // Number of polling |
nfc_data.packetbuffer[2] = period; // Polling period in units of 150ms |
nfc_data.packetbuffer[3] = 0x10; // Check for Mifare cards only |
if (!NFC_sendCommandCheckAck(nfc_data.packetbuffer, 4)) |
return 0; |
// Wait for IRQ line |
while (NFC_I2C_Read_Status() != PN532_I2C_READY); |
NFC_I2C_Read_Data(nfc_data.packetbuffer, 37); |
/* InAutoPoll response should be in the following format: |
* Byte Description |
* ---------- ------------------ |
* b0 Data ACK |
* b1..7 Frame header and preamble |
* b6 Tags found |
* b7 Polled target type (should be 0x10 Mifare) |
* b8 TargetData length (1/2) |
* b9..N NFC_TargetDataMiFare[1/2] |
* bN+1..N+2 Checksum + postamble |
*/ |
// Check # of tags found |
if (!nfc_data.packetbuffer[8]) |
return 0; |
// Save data from first card |
if (nfc_data.packetbuffer[15] == 4) { |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 9); |
} else { |
memcpy((char *)&cardData[0], (const char *)&nfc_data.packetbuffer[11], 12); |
} |
// Save data from second card |
if (nfc_data.packetbuffer[8] == 2) { |
// Offset will vary depending on length of first card |
if (nfc_data.packetbuffer[15] == 4) { |
if (nfc_data.packetbuffer[26] == 4) { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 9); |
} else { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[22], 12); |
} |
} else { |
if (nfc_data.packetbuffer[29] == 4) { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 9); |
} else { |
memcpy((char *)&cardData[1], (const char *)&nfc_data.packetbuffer[25], 12); |
} |
} |
} |
// Return the number of cards detected |
return nfc_data.packetbuffer[8]; |
} |
// Indicates whether the specified block number is the first block |
// in the sector (block 0 relative to the current sector) |
unsigned char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock) { |
323,7 → 399,7 |
// Check ACK |
NFC_I2C_Read_Data(buffer, 6); |
// Return if the 7 bytes matches the ACK |
// Return if the 7 bytes matches the ACK pattern |
return (strncmp((char *)buffer, (char *)pn532ack, 7) == 0); |
} |
351,18 → 427,18 |
} |
I2C_Read_Buffer((char *)buffer); |
// Note: First byte is always 0x01 (ready status from PN532) |
/* Remaining packet byte layout is as follows: |
Byte Description |
----- ---------------------- |
* 0 Preamble (0x00) |
* 1-2 Start code (0x00,0xFF) |
* 3 Length (TFI to N) |
* 4 Length Checksum (Length + LCS = 0x00) |
* 5 TFI (Frame identifier) |
* 0 Data ready ACK |
* 1 Preamble (0x00) |
* 2-3 Start code (0x00,0xFF) |
* 4 Length (TFI to N) |
* 5 Length Checksum (Length + LCS = 0x00) |
* 6 TFI (Frame identifier) |
* 0xD4 - Host to PN532 |
* 0xD5 - PN532 to Host |
* 6-N Data (Length - 1 bytes) |
* 7-N Data (Length - 1 bytes) |
* N+1 Data checksum (TFI + Data~N + DCS = 0x00) |
* N+2 Postamble (0x00) */ |
} |
/PIC Stuff/PIC_27J13/nfc.h |
---|
125,6 → 125,15 |
unsigned char Support; |
} NFC_FIRMWARE_VERSION; |
typedef struct { |
unsigned char TG; |
unsigned char SENS_RES[2]; |
unsigned char SEL_RES; |
unsigned char NFCID_LEN; |
unsigned char NFCID[7]; |
} NFC_TargetDataMiFare; |
// Size can be 9 or 12 bytes |
typedef struct __NFC_DATA { |
unsigned char packetbuffer[PN532_PACKBUFFSIZ]; |
} NFC_DATA; |
139,7 → 148,8 |
//unsigned char NFC_readGPIO(void); |
// ISO14443A functions |
unsigned char NFC_readPassiveTargetID(unsigned char cardbaudrate, unsigned char * uid, unsigned char * uidLength); |
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *uidData); |
unsigned char NFC_pollTargets(unsigned char number, unsigned char period, NFC_TargetDataMiFare *uidData); |
// Mifare Classic functions |
unsigned char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock); |
/PIC Stuff/PIC_27J13/uart.c |
---|
1,6 → 1,7 |
#include "maindefs.h" |
#include "uart.h" |
#include <string.h> |
#include <stdio.h> |
static UART_DATA uart_1_data; |
59,6 → 60,7 |
if (PIR1bits.RC1IF) { // Check if data receive flag is set |
c = RCREG1; |
#ifdef UART1_RECV_BUFFER |
// Save received data into buffer |
uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c; |
if (uart_1_data.buffer_in_write_ind == MAXUARTBUF - 1) { |
83,6 → 85,10 |
uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp; |
uart_1_data.buffer_in_len_tmp = 0; |
} |
#endif |
#ifdef UART1_RECV_XBEE |
#endif |
} |
if (RCSTAbits.OERR == 1) { |
120,7 → 126,21 |
} |
void UART1_WriteS(const rom char *fmt, ...) { |
#ifdef _DEBUG |
unsigned char i; |
va_list args; |
va_start(args, fmt); |
vsprintf((char *) uart_1_data.buffer_out, fmt, args); |
va_end(args); |
uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out); |
uart_1_data.buffer_out_ind = 1; |
for (i = 0; i < uart_1_data.buffer_out_len; i++) { |
TXREG1 = uart_1_data.buffer_out[i]; |
Nop(); |
while(!PIR1bits.TX1IF); // Wait for byte to be transmitted |
} |
#else |
va_list args; |
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending |
va_start(args, fmt); |
vsprintf((char *) uart_1_data.buffer_out, fmt, args); |
129,10 → 149,19 |
uart_1_data.buffer_out_ind = 1; |
TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR |
TXSTA1bits.TXEN = 1; // Begin transmission |
#endif |
} |
void UART1_WriteB(const char *msg, unsigned char length) { |
unsigned char i; |
#ifdef _DEBUG |
for (i = 0; i < length; i++) { |
TXREG1 = msg[i]; |
Nop(); |
while(!PIR1bits.TX1IF); // Wait for byte to be transmitted |
} |
#else |
unsigned char i; |
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending |
uart_1_data.buffer_out_len = length; |
uart_1_data.buffer_out_ind = 1; |
141,6 → 170,7 |
} |
TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR |
TXSTA1bits.TXEN = 1; // Begin transmission |
#endif |
} |
unsigned char UART1_Buffer_Len() { |
/PIC Stuff/PIC_27J13/uart.h |
---|
5,6 → 5,9 |
#define UART1_BREAK_CHAR 0x0D //(CR) |
#define UART1_RECV_BUFFER |
//#define UART1_RECV_XBEE |
typedef struct __UART_DATA { |
unsigned char buffer_in[MAXUARTBUF]; |
unsigned char buffer_in_read_ind; |