Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 147 → Rev 148

/PIC Stuff/PIC_27J13/nfc.c
File deleted
\ No newline at end of file
/PIC Stuff/PIC_27J13/nfc.h
File deleted
/PIC Stuff/PIC_27J13/defines.h
14,7 → 14,7
// Option to disable SPI MISO
#define SPI2_WRITE_ONLY
 
//#define _DEBUG
#define _DEBUG
//#define _TEST_UART
//#define _TEST_I2C_MASTER
//#define _TEST_I2C_SLAVE
27,7 → 27,8
//#define _TEST_XBEE
//#define _TEST_NFC_TO_SSD1306_OLED
//#define _TEST_TIMER1_RTC
#define _TEST_LUX
//#define _TEST_LUX
#define _TEST_OLED_CHAR
 
// Enable or disable debug prints depending on project preprocessor (_DEBUG)
#ifdef _DEBUG
39,6 → 40,7
#define DBG_PRINT_PORTB_INT(x)
#define DBG_PRINT_INT(x)
#define DBG_PRINT_BUFFER(x)
#define DBG_PRINT_LUX(x) UART1_WriteS(x)
#else
#define DBG_PRINT_MAIN(x)
#define DBG_PRINT_UART(x)
48,6 → 50,7
#define DBG_PRINT_PORTB_INT(x)
#define DBG_PRINT_INT(x)
#define DBG_PRINT_BUFFER(x)
#define DBG_PRINT_LUX(x)
#endif
 
// Pin allocations
78,6 → 81,23
#define SPI_SLAVE_SELECT_TRIS TRISAbits.TRISA3
#define SPI_SLAVE_SELECT_LAT LATAbits.LATA3
 
#define PARALLEL_RS_TRIS TRISBbits.TRISB7
#define PARALLEL_RS_LAT LATBbits.LATB7
#define PARALLEL_RW_TRIS TRISBbits.TRISB6
#define PARALLEL_RW_LAT LATBbits.LATB6
#define PARALLEL_EN_TRIS TRISBbits.TRISB5
#define PARALLEL_EN_LAT LATBbits.LATB5
#define PARALLEL_D4_TRIS TRISBbits.TRISB4
#define PARALLEL_D4_LAT LATBbits.LATB4
#define PARALLEL_D5_TRIS TRISBbits.TRISB3
#define PARALLEL_D5_LAT LATBbits.LATB3
#define PARALLEL_D6_TRIS TRISBbits.TRISB2
#define PARALLEL_D6_LAT LATBbits.LATB2
#define PARALLEL_D7_TRIS TRISBbits.TRISB1
#define PARALLEL_D7_LAT LATBbits.LATB1
#define PARALLEL_BUSY_TRIS TRISBbits.TRISB1
#define PARALLEL_BUSY_PORT PORTBbits.RB1
 
#define NFC_IRQ_TRIS TRISAbits.TRISA5
#define NFC_IRQ_PORT PORTAbits.RA5
//#define NFC_RESET_TRIS TRISCbits.TRISC2
/PIC Stuff/PIC_27J13/lux_TSL2561.c
1,5 → 1,7
#include "lux_TSL2561.h"
#include "defines.h"
#include "i2c.h"
#include <delays.h>
 
static TSL2561_DATA tsl2561_data;
static TSL2561_DATA *tsl2561_data_p = &tsl2561_data;
11,5 → 13,217
}
 
void LUX_Begin(void) {
unsigned char i, result, length, buffer[10];
unsigned char toSend = TSL2561_REGISTER_ID;
DBG_PRINT_LUX("Sending %X to address %X\r\n", toSend, tsl2561_data_p->address);
I2C_Master_Send(tsl2561_data_p->address, 1, &toSend);
do {
result = I2C_Get_Status();
} while (!result);
 
I2C_Master_Recv(tsl2561_data_p->address, 1);
do {
result = I2C_Get_Status();
} while (!result);
length = I2C_Read_Buffer((char *)buffer);
DBG_PRINT_LUX("Received %d bytes: ", length);
for (i = 0; i < length; i++) {
DBG_PRINT_LUX("%c ", buffer[i]);
}
DBG_PRINT_LUX("\r\n");
 
// Set default integration time and gain
LUX_SetTiming(tsl2561_data_p->integration);
LUX_SetGain(tsl2561_data_p->gain);
 
// Start the chip in power-down mode
LUX_Disable();
}
 
void LUX_Enable() {
LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);
}
 
void LUX_Disable() {
LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF);
}
 
void LUX_SetGain(tsl2561Gain_t gain) {
LUX_Enable();
tsl2561_data_p->gain = gain;
LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
tsl2561_data_p->integration | tsl2561_data_p->gain);
LUX_Disable();
}
 
void LUX_SetTiming(tsl2561IntegrationTime_t integration) {
LUX_Enable();
tsl2561_data_p->integration = integration;
LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
tsl2561_data_p->integration | tsl2561_data_p->gain);
LUX_Disable();
}
 
unsigned long LUX_CalculateLux(unsigned int ch0, unsigned int ch1) {
unsigned long chScale, channel0, channel1, ratio1, ratio, temp, lux;
unsigned int b, m;
 
switch (tsl2561_data_p->integration) {
case TSL2561_INTEGRATIONTIME_13MS:
chScale = TSL2561_LUX_CHSCALE_TINT0;
break;
case TSL2561_INTEGRATIONTIME_101MS:
chScale = TSL2561_LUX_CHSCALE_TINT1;
break;
default: // No scaling ... integration time = 402ms
chScale = (1 << TSL2561_LUX_CHSCALE);
break;
}
 
// Scale for gain (1x or 16x)
if (!tsl2561_data_p->gain)
chScale = chScale << 4;
 
// scale the channel values
channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE;
channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE;
 
// find the ratio of the channel values (Channel1/Channel0)
ratio1 = 0;
if (channel0 != 0)
ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0;
 
// round the ratio value
ratio = (ratio1 + 1) >> 1;
 
#ifdef TSL2561_PACKAGE_CS
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) {
b = TSL2561_LUX_B1C; m = TSL2561_LUX_M1C;
} else if (ratio <= TSL2561_LUX_K2C) {
b = TSL2561_LUX_B2C; m = TSL2561_LUX_M2C;
} else if (ratio <= TSL2561_LUX_K3C) {
b = TSL2561_LUX_B3C; m = TSL2561_LUX_M3C;
} else if (ratio <= TSL2561_LUX_K4C) {
b = TSL2561_LUX_B4C; m = TSL2561_LUX_M4C;
} else if (ratio <= TSL2561_LUX_K5C) {
b = TSL2561_LUX_B5C; m = TSL2561_LUX_M5C;
} else if (ratio <= TSL2561_LUX_K6C) {
b = TSL2561_LUX_B6C; m = TSL2561_LUX_M6C;
} else if (ratio <= TSL2561_LUX_K7C) {
b = TSL2561_LUX_B7C; m = TSL2561_LUX_M7C;
} else if (ratio > TSL2561_LUX_K8C) {
b = TSL2561_LUX_B8C; m = TSL2561_LUX_M8C;
}
#else
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) {
b = TSL2561_LUX_B1T; m = TSL2561_LUX_M1T;
} else if (ratio <= TSL2561_LUX_K2T) {
b = TSL2561_LUX_B2T; m = TSL2561_LUX_M2T;
} else if (ratio <= TSL2561_LUX_K3T) {
b = TSL2561_LUX_B3T; m = TSL2561_LUX_M3T;
} else if (ratio <= TSL2561_LUX_K4T) {
b = TSL2561_LUX_B4T; m = TSL2561_LUX_M4T;
} else if (ratio <= TSL2561_LUX_K5T) {
b = TSL2561_LUX_B5T; m = TSL2561_LUX_M5T;
} else if (ratio <= TSL2561_LUX_K6T) {
b = TSL2561_LUX_B6T; m = TSL2561_LUX_M6T;
} else if (ratio <= TSL2561_LUX_K7T) {
b = TSL2561_LUX_B7T; m = TSL2561_LUX_M7T;
} else if (ratio > TSL2561_LUX_K8T) {
b = TSL2561_LUX_B8T; m = TSL2561_LUX_M8T;
}
#endif
temp = ((channel0 * b) - (channel1 * m));
 
// do not allow negative lux value
if (temp < 0)
temp = 0;
 
// round lsb (2^(LUX_SCALE-1))
temp += (1 << (TSL2561_LUX_LUXSCALE-1));
 
// strip off fractional portion
lux = temp >> TSL2561_LUX_LUXSCALE;
 
return lux;
}
 
unsigned long LUX_GetFullLuminosity() {
unsigned long x;
 
// Enable the device by setting the control bit to 0x03
LUX_Enable();
 
// Wait x ms for ADC to complete
switch (tsl2561_data_p->integration) {
case TSL2561_INTEGRATIONTIME_13MS:
Delay10KTCYx(67);
break;
case TSL2561_INTEGRATIONTIME_101MS:
Delay10KTCYx(255);
Delay10KTCYx(230);
break;
default:
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(145);
break;
}
 
x = LUX_Read2Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
x <<= 16;
x |= LUX_Read2Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
 
LUX_Disable();
 
return x;
}
 
unsigned int LUX_GetLuminosity(unsigned char channel) {
unsigned long x = LUX_GetFullLuminosity();
 
if (channel == 0) {
// Reads two byte value from channel 0 (visible + infrared)
return (x & 0xFFFF);
} else if (channel == 1) {
// Reads two byte value from channel 1 (infrared)
return (x >> 16);
} else if (channel == 2) {
// Reads all and subtracts out just the visible!
return ( (x & 0xFFFF) - (x >> 16));
}
 
// Unknown channel!
return 0;
}
 
void LUX_Write2Bytes(unsigned char reg, unsigned char value) {
unsigned char buffer[2], result;
buffer[0] = reg;
buffer[1] = value;
I2C_Master_Send(tsl2561_data_p->address, 2, buffer);
do {
result = I2C_Get_Status();
} while (!result);
}
 
unsigned int LUX_Read2Bytes(unsigned char reg) {
unsigned char result, length, buffer[2];
unsigned int ret;
 
I2C_Master_Restart(tsl2561_data_p->address, reg, 2);
do {
result = I2C_Get_Status();
} while (!result);
length = I2C_Read_Buffer((char *)buffer);
ret = buffer[1] << 8;
ret |= buffer[0];
 
return ret;
}
/PIC Stuff/PIC_27J13/lux_TSL2561.h
117,15 → 117,18
tsl2561Gain_t gain;
} TSL2561_DATA;
 
void LUX_Init(void);
void LUX_Init(unsigned char address);
void LUX_Begin(void);
void LUX_Enable(void);
void LUX_Disable(void);
unsigned long LUX_CalculateLux(unsigned int ch0, unsigned int ch1);
void LUX_SetTiming(tsl2561IntegrationTime_t integration);
void LUX_SetGain(tsl2561Gain_t gain);
unsigned long LUX_CalculateLux(unsigned int ch0, unsigned int ch1);
unsigned long LUX_GetFullLuminosity(void);
unsigned int LUX_GetLuminosity(unsigned char channel);
unsigned long LUX_GetFullLuminosity(void);
 
void LUX_Write2Bytes(unsigned char reg, unsigned char value);
unsigned int LUX_Read2Bytes(unsigned char reg);
 
#endif /* LUX_TSL2561_H */
 
/PIC Stuff/PIC_27J13/main.c
3,7 → 3,7
#include "uart.h"
#include "i2c.h"
#include "spi.h"
#include "nfc.h"
#include "nfc_PN532.h"
#include "led_HT16K33.h"
#include "oled_ssd1306.h"
#include "oled_ssd1331.h"
10,6 → 10,8
#include "adc.h"
#include "xbee.h"
#include "timers.h"
#include "lux_TSL2561.h"
#include "oled_NHD-0216KZW-AB5.h"
#include <delays.h>
#include <string.h>
 
98,17 → 100,15
buffer[0] = 0x8;
 
I2C_Master_Send(0x24, 1, buffer);
result = I2C_Get_Status();
while (!result) {
do {
result = I2C_Get_Status();
}
} while (!result);
DBG_PRINT_MAIN("S:%X ", result);
 
I2C_Master_Recv(0x24, 2);
result = I2C_Get_Status();
while (!result) {
do {
result = I2C_Get_Status();
}
} while (!result);
DBG_PRINT_MAIN("S:%X ", result);
length = I2C_Read_Buffer(buffer);
DBG_PRINT_MAIN("L:%d D:", length);
1037,6 → 1037,8
#ifdef _TEST_LUX
 
void main(void) {
unsigned int ir, full;
unsigned long lum;
 
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
1050,6 → 1052,7
 
UART1_Init();
I2C_Init();
LUX_Init(TSL2561_ADDR_FLOAT);
I2C_Configure_Master(I2C_100KHZ);
 
1056,12 → 1059,70
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
 
LUX_Begin();
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
LUX_SetGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
// LUX_SetGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)
 
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
LUX_SetTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
// LUX_SetTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
// LUX_SetTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)
 
while (1) {
lum = LUX_GetFullLuminosity();
ir = lum >> 16;
full = lum & 0xFFFF;
DBG_PRINT_LUX("IR: %d\r\n", ir);
DBG_PRINT_LUX("Visible: %d\r\n", full - ir);
DBG_PRINT_LUX("Full: %d\r\n", full);
DBG_PRINT_LUX("Lux: %ld\r\n\r\n", LUX_CalculateLux(full, ir));
 
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#endif
 
#ifdef _TEST_OLED_CHAR
 
void main(void) {
int i;
unsigned char *buffer = "Test String";
/* --------------------- 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 = 0xFF;
ANCON1 = 0x1F;
 
// UART1_Init();
NHD_Init();
 
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
interrupt_init(); // Initialize the interrupt priorities
 
NHD_Begin(16, 2);
NHD_Write_String("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do");
NHD_Set_Cursor(0,1);
NHD_Write_String("eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e");
 
while (1) {
Delay10KTCYx(150);
NHD_Scroll_Display_Left();
}
}
#endif
 
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
!defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
!defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
1068,7 → 1129,7
!defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
!defined(_TEST_ADC) && !defined(_TEST_XBEE) && \
!defined(_TEST_NFC_TO_SSD1306_OLED) && !defined(_TEST_TIMER1_RTC) && \
!defined(_TEST_LUX)
!defined(_TEST_LUX) && !defined(_TEST_OLED_CHAR)
 
void main(void) {
 
/PIC Stuff/PIC_27J13/nbproject/Makefile-default.mk
45,11 → 45,11
DISTDIR=dist/${CND_CONF}/${IMAGE_TYPE}
 
# Object Files Quoted if spaced
OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/main.o ${OBJECTDIR}/i2c.o ${OBJECTDIR}/interrupts.o ${OBJECTDIR}/nfc.o ${OBJECTDIR}/spi.o ${OBJECTDIR}/uart.o ${OBJECTDIR}/oled_ssd1306.o ${OBJECTDIR}/glcdfont.o ${OBJECTDIR}/adc.o ${OBJECTDIR}/xbee.o ${OBJECTDIR}/oled_ssd1331.o ${OBJECTDIR}/timers.o ${OBJECTDIR}/led_HT16K33.o ${OBJECTDIR}/lux_TSL2561.o
POSSIBLE_DEPFILES=${OBJECTDIR}/main.o.d ${OBJECTDIR}/i2c.o.d ${OBJECTDIR}/interrupts.o.d ${OBJECTDIR}/nfc.o.d ${OBJECTDIR}/spi.o.d ${OBJECTDIR}/uart.o.d ${OBJECTDIR}/oled_ssd1306.o.d ${OBJECTDIR}/glcdfont.o.d ${OBJECTDIR}/adc.o.d ${OBJECTDIR}/xbee.o.d ${OBJECTDIR}/oled_ssd1331.o.d ${OBJECTDIR}/timers.o.d ${OBJECTDIR}/led_HT16K33.o.d ${OBJECTDIR}/lux_TSL2561.o.d
OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/main.o ${OBJECTDIR}/i2c.o ${OBJECTDIR}/interrupts.o ${OBJECTDIR}/spi.o ${OBJECTDIR}/uart.o ${OBJECTDIR}/oled_ssd1306.o ${OBJECTDIR}/glcdfont.o ${OBJECTDIR}/adc.o ${OBJECTDIR}/xbee.o ${OBJECTDIR}/oled_ssd1331.o ${OBJECTDIR}/timers.o ${OBJECTDIR}/led_HT16K33.o ${OBJECTDIR}/lux_TSL2561.o ${OBJECTDIR}/nfc_PN532.o ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o
POSSIBLE_DEPFILES=${OBJECTDIR}/main.o.d ${OBJECTDIR}/i2c.o.d ${OBJECTDIR}/interrupts.o.d ${OBJECTDIR}/spi.o.d ${OBJECTDIR}/uart.o.d ${OBJECTDIR}/oled_ssd1306.o.d ${OBJECTDIR}/glcdfont.o.d ${OBJECTDIR}/adc.o.d ${OBJECTDIR}/xbee.o.d ${OBJECTDIR}/oled_ssd1331.o.d ${OBJECTDIR}/timers.o.d ${OBJECTDIR}/led_HT16K33.o.d ${OBJECTDIR}/lux_TSL2561.o.d ${OBJECTDIR}/nfc_PN532.o.d ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o.d
 
# Object Files
OBJECTFILES=${OBJECTDIR}/main.o ${OBJECTDIR}/i2c.o ${OBJECTDIR}/interrupts.o ${OBJECTDIR}/nfc.o ${OBJECTDIR}/spi.o ${OBJECTDIR}/uart.o ${OBJECTDIR}/oled_ssd1306.o ${OBJECTDIR}/glcdfont.o ${OBJECTDIR}/adc.o ${OBJECTDIR}/xbee.o ${OBJECTDIR}/oled_ssd1331.o ${OBJECTDIR}/timers.o ${OBJECTDIR}/led_HT16K33.o ${OBJECTDIR}/lux_TSL2561.o
OBJECTFILES=${OBJECTDIR}/main.o ${OBJECTDIR}/i2c.o ${OBJECTDIR}/interrupts.o ${OBJECTDIR}/spi.o ${OBJECTDIR}/uart.o ${OBJECTDIR}/oled_ssd1306.o ${OBJECTDIR}/glcdfont.o ${OBJECTDIR}/adc.o ${OBJECTDIR}/xbee.o ${OBJECTDIR}/oled_ssd1331.o ${OBJECTDIR}/timers.o ${OBJECTDIR}/led_HT16K33.o ${OBJECTDIR}/lux_TSL2561.o ${OBJECTDIR}/nfc_PN532.o ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o
 
 
CFLAGS=
101,13 → 101,6
@${DEP_GEN} -d ${OBJECTDIR}/interrupts.o
@${FIXDEPS} "${OBJECTDIR}/interrupts.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/nfc.o: nfc.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/nfc.o.d
${MP_CC} $(MP_EXTRA_CC_PRE) -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -p$(MP_PROCESSOR_OPTION) -oi -ml -oa- -I ${MP_CC_DIR}\\..\\h -fo ${OBJECTDIR}/nfc.o nfc.c
@${DEP_GEN} -d ${OBJECTDIR}/nfc.o
@${FIXDEPS} "${OBJECTDIR}/nfc.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/spi.o: spi.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/spi.o.d
178,6 → 171,20
@${DEP_GEN} -d ${OBJECTDIR}/lux_TSL2561.o
@${FIXDEPS} "${OBJECTDIR}/lux_TSL2561.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/nfc_PN532.o: nfc_PN532.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/nfc_PN532.o.d
${MP_CC} $(MP_EXTRA_CC_PRE) -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -p$(MP_PROCESSOR_OPTION) -oi -ml -oa- -I ${MP_CC_DIR}\\..\\h -fo ${OBJECTDIR}/nfc_PN532.o nfc_PN532.c
@${DEP_GEN} -d ${OBJECTDIR}/nfc_PN532.o
@${FIXDEPS} "${OBJECTDIR}/nfc_PN532.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/oled_NHD-0216KZW-AB5.o: oled_NHD-0216KZW-AB5.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o.d
${MP_CC} $(MP_EXTRA_CC_PRE) -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -p$(MP_PROCESSOR_OPTION) -oi -ml -oa- -I ${MP_CC_DIR}\\..\\h -fo ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o oled_NHD-0216KZW-AB5.c
@${DEP_GEN} -d ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o
@${FIXDEPS} "${OBJECTDIR}/oled_NHD-0216KZW-AB5.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
else
${OBJECTDIR}/main.o: main.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
200,13 → 207,6
@${DEP_GEN} -d ${OBJECTDIR}/interrupts.o
@${FIXDEPS} "${OBJECTDIR}/interrupts.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/nfc.o: nfc.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/nfc.o.d
${MP_CC} $(MP_EXTRA_CC_PRE) -p$(MP_PROCESSOR_OPTION) -oi -ml -oa- -I ${MP_CC_DIR}\\..\\h -fo ${OBJECTDIR}/nfc.o nfc.c
@${DEP_GEN} -d ${OBJECTDIR}/nfc.o
@${FIXDEPS} "${OBJECTDIR}/nfc.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/spi.o: spi.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/spi.o.d
277,6 → 277,20
@${DEP_GEN} -d ${OBJECTDIR}/lux_TSL2561.o
@${FIXDEPS} "${OBJECTDIR}/lux_TSL2561.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/nfc_PN532.o: nfc_PN532.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/nfc_PN532.o.d
${MP_CC} $(MP_EXTRA_CC_PRE) -p$(MP_PROCESSOR_OPTION) -oi -ml -oa- -I ${MP_CC_DIR}\\..\\h -fo ${OBJECTDIR}/nfc_PN532.o nfc_PN532.c
@${DEP_GEN} -d ${OBJECTDIR}/nfc_PN532.o
@${FIXDEPS} "${OBJECTDIR}/nfc_PN532.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
${OBJECTDIR}/oled_NHD-0216KZW-AB5.o: oled_NHD-0216KZW-AB5.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o.d
${MP_CC} $(MP_EXTRA_CC_PRE) -p$(MP_PROCESSOR_OPTION) -oi -ml -oa- -I ${MP_CC_DIR}\\..\\h -fo ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o oled_NHD-0216KZW-AB5.c
@${DEP_GEN} -d ${OBJECTDIR}/oled_NHD-0216KZW-AB5.o
@${FIXDEPS} "${OBJECTDIR}/oled_NHD-0216KZW-AB5.o.d" $(SILENT) -rsi ${MP_CC_DIR}../ -c18
endif
 
# ------------------------------------------------------------------------------------
/PIC Stuff/PIC_27J13/nbproject/Makefile-genesis.properties
1,5 → 1,5
#
#Thu Dec 20 15:51:53 EST 2012
#Fri Dec 21 04:23:33 EST 2012
default.com-microchip-mplab-nbide-toolchainC18-C18LanguageToolchain.md5=e4cd2f1e5bba11c8ba952dafee98d887
default.languagetoolchain.dir=C\:\\Program Files (x86)\\Microchip\\mplabc18\\v3.42\\bin
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=d94e033fce233e60ccb9abf3a212a9b7
/PIC Stuff/PIC_27J13/nbproject/configurations.xml
6,7 → 6,6
projectFiles="true">
<itemPath>i2c.h</itemPath>
<itemPath>interrupts.h</itemPath>
<itemPath>nfc.h</itemPath>
<itemPath>spi.h</itemPath>
<itemPath>uart.h</itemPath>
<itemPath>oled_ssd1306.h</itemPath>
17,6 → 16,8
<itemPath>timers.h</itemPath>
<itemPath>led_HT16K33.h</itemPath>
<itemPath>lux_TSL2561.h</itemPath>
<itemPath>nfc_PN532.h</itemPath>
<itemPath>oled_NHD-0216KZW-AB5.h</itemPath>
</logicalFolder>
<logicalFolder name="LibraryFiles"
displayName="Library Files"
37,7 → 38,6
<itemPath>main.c</itemPath>
<itemPath>i2c.c</itemPath>
<itemPath>interrupts.c</itemPath>
<itemPath>nfc.c</itemPath>
<itemPath>spi.c</itemPath>
<itemPath>uart.c</itemPath>
<itemPath>oled_ssd1306.c</itemPath>
48,6 → 48,8
<itemPath>timers.c</itemPath>
<itemPath>led_HT16K33.c</itemPath>
<itemPath>lux_TSL2561.c</itemPath>
<itemPath>nfc_PN532.c</itemPath>
<itemPath>oled_NHD-0216KZW-AB5.c</itemPath>
</logicalFolder>
<logicalFolder name="ExternalFiles"
displayName="Important Files"
/PIC Stuff/PIC_27J13/nfc_PN532.c
0,0 → 1,481
#include "defines.h"
#include "nfc_PN532.h"
#include "i2c.h"
#include <string.h>
#include <delays.h>
 
static NFC_DATA nfc_data;
static NFC_DATA *nfc_data_p = &nfc_data;
 
// Const value arrays for comparison use
static char pn532response_firmwarevers[] = {0x01, 0x00, 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
static char pn532ack[] = {0x01, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
 
void NFC_Init() {
NFC_IRQ_TRIS = 1; // IRQ Pin is RC5
 
/* NFC reset is disabled due to lack of pins */
// NFC_RESET_TRIS = 0; // Reset Pin is RC2
//
// // Reset the PN532
// NFC_RESET_LAT = 1;
// NFC_RESET_LAT = 0;
// Delay10TCYx(1);
// NFC_RESET_LAT = 1;
}
 
// Configures the SAM (Secure Access Module)
unsigned char NFC_SAMConfig() {
nfc_data_p->packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
nfc_data_p->packetbuffer[1] = 0x01; // Normal mode
nfc_data_p->packetbuffer[2] = 0x14; // Timeout 50ms * 20 = 1s
nfc_data_p->packetbuffer[3] = 0x01; // Use IRQ pin
 
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
return 0;
 
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 8);
 
return (nfc_data_p->packetbuffer[7] == 0x15);
}
 
// Checks the firmware version of the PN5xx chip
NFC_FIRMWARE_VERSION NFC_getFirmwareVersion(void) {
NFC_FIRMWARE_VERSION response = {0, 0, 0, 0};
 
// Create and send command
nfc_data_p->packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
 
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 1))
return response;
 
// Read back data from the PN532
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 12);
 
// Compare and check returned values
if (strncmp((char *) nfc_data_p->packetbuffer, (char *) pn532response_firmwarevers, 8) != 0)
return response;
 
// Save and return info
response.IC = nfc_data_p->packetbuffer[8];
response.Ver = nfc_data_p->packetbuffer[9];
response.Rev = nfc_data_p->packetbuffer[10];
response.Support = nfc_data_p->packetbuffer[11];
 
return response;
}
 
// Sends a command and waits a specified period for the ACK
unsigned char NFC_sendCommandCheckAck(unsigned char *cmd, unsigned char cmdlen) {
unsigned int timer = 0;
 
// Write the command
NFC_I2C_Write_Cmd(cmd, cmdlen);
 
// Wait for chip to be ready
while (NFC_I2C_Read_Status() != PN532_I2C_READY) {
if (PN532_TIMEOUT != 0) {
timer += 1;
if (timer > PN532_TIMEOUT)
return 0;
}
Delay10TCYx(1);
}
 
// Check ACK
if (!NFC_I2C_Read_ACK()) {
return 0;
}
 
return 1;
}
 
// Passive polling, waits for an ISO14443A target to enter the field
unsigned char NFC_readPassiveTargetID(NFC_TargetDataMiFare *cardData) {
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
nfc_data_p->packetbuffer[1] = 2; // Max 2 cards at once
nfc_data_p->packetbuffer[2] = PN532_MIFARE_ISO14443A; // Mifare only
 
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 3))
return 0;
 
// Wait for IRQ line
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 35);
 
/* 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_p->packetbuffer[8])
return 0;
// Save data from first card
if (nfc_data_p->packetbuffer[13] == 4) {
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[9], 9);
} else {
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[9], 12);
}
 
// Save data from second card
if (nfc_data_p->packetbuffer[8] == 2) {
// Offset will vary depending on length of first card
if (nfc_data_p->packetbuffer[13] == 4) {
if (nfc_data_p->packetbuffer[22] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[18], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[18], 12);
}
} else { // Length of first UID is 7
if (nfc_data_p->packetbuffer[25] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[21], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[21], 12);
}
}
}
// Return the number of cards detected
return nfc_data_p->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_p->packetbuffer[0] = PN532_COMMAND_INAUTOPOLL;
nfc_data_p->packetbuffer[1] = number; // Number of polling
nfc_data_p->packetbuffer[2] = period; // Polling period in units of 150ms
nfc_data_p->packetbuffer[3] = 0x10; // Check for Mifare cards only
 
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
return 0;
 
// Wait for IRQ line
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
NFC_I2C_Read_Data(nfc_data_p->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_p->packetbuffer[8])
return 0;
 
// Save data from first card
if (nfc_data_p->packetbuffer[15] == 4) {
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[11], 9);
} else {
memcpy((char *)&cardData[0], (const char *)&nfc_data_p->packetbuffer[11], 12);
}
 
// Save data from second card
if (nfc_data_p->packetbuffer[8] == 2) {
// Offset will vary depending on length of first card
if (nfc_data_p->packetbuffer[15] == 4) {
if (nfc_data_p->packetbuffer[26] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[22], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[22], 12);
}
} else {
if (nfc_data_p->packetbuffer[29] == 4) {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[25], 9);
} else {
memcpy((char *)&cardData[1], (const char *)&nfc_data_p->packetbuffer[25], 12);
}
}
}
 
// Return the number of cards detected
return nfc_data_p->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) {
// Test if we are in the small or big sectors
if (uiBlock < 128)
return ((uiBlock) % 4 == 0);
else
return ((uiBlock) % 16 == 0);
}
 
// Indicates whether the specified block number is the sector trailer
unsigned char NFC_mifareclassic_IsTrailerBlock(unsigned long uiBlock) {
// Test if we are in the small or big sectors
if (uiBlock < 128)
return ((uiBlock + 1) % 4 == 0);
else
return ((uiBlock + 1) % 16 == 0);
}
 
// Tries to authenticate a block of memory on a MIFARE card using the INDATAEXCHANGE command
unsigned char NFC_mifareclassic_AuthenticateBlock(unsigned char *uid, unsigned char uidLen, unsigned long blockNumber, unsigned char keyNumber, unsigned char *keyData) {
// See section 7.3.8 of the PN532 User Manual
// blockNumber = The block number to authenticate. (0..63 for 1KB cards, and 0..255 for 4KB cards)\
// keyNumber = Which key type to use during authentication (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B)
// keyData = Pointer to a byte array containing the 6 byte key value
 
unsigned char i;
 
// Assemble frame data
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
nfc_data_p->packetbuffer[1] = 1; /* Max card numbers */
nfc_data_p->packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_A : MIFARE_CMD_AUTH_B;
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
for (i = 0; i < 6; i++) {
nfc_data_p->packetbuffer[4 + i] = keyData[i];
}
for (i = 0; i < uidLen; i++) {
nfc_data_p->packetbuffer[10 + i] = uid[i];
}
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 10 + uidLen))
return 0;
 
// Read response from PN532
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 12);
 
return 1;
}
 
// Tries to read an entire 16-byte data block at the specified block address
unsigned char NFC_mifareclassic_ReadDataBlock(unsigned char blockNumber, unsigned char *data) {
unsigned char i;
 
// Assemble frame data
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_data_p->packetbuffer[1] = 1; /* Card number */
nfc_data_p->packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 4))
return 0;
 
// Read reponse
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 26);
 
// If byte 9 isnt 0x00 we probably have and error
if (nfc_data_p->packetbuffer[8] != 0x00) {
return 0;
}
 
// Copy the 16 data bytes into the data buffer
// Block contents starts at byte 10 of a valid response
for (i = 0; i < 16; i++) {
data[i] = nfc_data_p->packetbuffer[9 + i];
}
 
return 1;
}
 
// Tries to write an entire 16-byte data block at the specified block address
unsigned char NFC_mifareclassic_WriteDataBlock(unsigned char blockNumber, unsigned char *data) {
unsigned char i;
 
// Assemble frame data
nfc_data_p->packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
nfc_data_p->packetbuffer[1] = 1; /* Card number */
nfc_data_p->packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
nfc_data_p->packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
for (i = 0; i < 16; i++) { /* Data Payload */
nfc_data_p->packetbuffer[4 + i] = data[i];
}
 
// Send frame and check for ACK
if (!NFC_sendCommandCheckAck(nfc_data_p->packetbuffer, 20))
return 0;
 
// Read response
NFC_I2C_Read_Data(nfc_data_p->packetbuffer, 26);
 
return 1;
}
 
// Formats a Mifare Classic card to store NDEF Records
unsigned char NFC_mifareclassic_FormatNDEF(void) {
unsigned char sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
unsigned char sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
unsigned char sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 
// Write blocks 1 and 2
if (!NFC_mifareclassic_WriteDataBlock(1, sectorbuffer1))
return 0;
if (!NFC_mifareclassic_WriteDataBlock(2, sectorbuffer2))
return 0;
// Write key A and access rights
if (!NFC_mifareclassic_WriteDataBlock(3, sectorbuffer3))
return 0;
 
return 1;
}
 
// Writes an NDEF URI Record to the specified sector (1..15)
/* Note that this function assumes that the Mifare Classic card is
already formatted to work as an "NFC Forum Tag" and uses a MAD1
file system. You can use the NXP TagWriter app on Android to
properly format cards for this. */
unsigned char NFC_mifareclassic_WriteNDEFURI(unsigned char sectorNumber, unsigned char uriIdentifier, const char * url) {
// uriIdentifier = The uri identifier code (0 = none, 0x01 = "http://www.", etc.)
// url = The uri text to write (max 38 characters)
 
// Figure out how long the string is
unsigned char len = strlen(url);
 
unsigned char sectorbuffer1[16] = {0x00, 0x00, 0x03, len + 5, 0xD1, 0x01, len + 1, 0x55, uriIdentifier, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 
// Make sure we're within a 1K limit for the sector number
if ((sectorNumber < 1) || (sectorNumber > 15))
return 0;
 
// Make sure the URI payload is between 1 and 38 chars
if ((len < 1) || (len > 38))
return 0;
 
if (len <= 6) {
// Unlikely we'll get a url this short, but why not ...
memcpy(sectorbuffer1 + 9, url, len);
sectorbuffer1[len + 9] = 0xFE;
} else if (len == 7) {
// 0xFE needs to be wrapped around to next block
memcpy(sectorbuffer1 + 9, url, len);
sectorbuffer2[0] = 0xFE;
} else if ((len > 7) || (len <= 22)) {
// Url fits in two blocks
memcpy(sectorbuffer1 + 9, url, 7);
memcpy(sectorbuffer2, url + 7, len - 7);
sectorbuffer2[len - 7] = 0xFE;
} else if (len == 23) {
// 0xFE needs to be wrapped around to final block
memcpy(sectorbuffer1 + 9, url, 7);
memcpy(sectorbuffer2, url + 7, len - 7);
sectorbuffer3[0] = 0xFE;
} else {
// Url fits in three blocks
memcpy(sectorbuffer1 + 9, url, 7);
memcpy(sectorbuffer2, url + 7, 16);
memcpy(sectorbuffer3, url + 23, len - 24);
sectorbuffer3[len - 22] = 0xFE;
}
 
// Now write all three blocks back to the card
if (!(NFC_mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1)))
return 0;
if (!(NFC_mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2)))
return 0;
if (!(NFC_mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3)))
return 0;
if (!(NFC_mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4)))
return 0;
 
return 1;
}
 
// Reads and checks for the ACK signal
unsigned char NFC_I2C_Read_ACK() {
unsigned char buffer[7];
 
// Check ACK
NFC_I2C_Read_Data(buffer, 6);
 
// Return if the 7 bytes matches the ACK pattern
return (strncmp((char *) buffer, (char *) pn532ack, 7) == 0);
}
 
// Checks the IRQ pin to know if the PN532 is ready
unsigned char NFC_I2C_Read_Status() {
if (NFC_IRQ_PORT == 1) {
return PN532_I2C_BUSY;
} else {
return PN532_I2C_READY;
}
}
 
// Reads n bytes of data from the PN532 via I2C
void NFC_I2C_Read_Data(unsigned char *buffer, unsigned char length) {
unsigned char result;
 
// Wait for IRQ to go low
while (NFC_I2C_Read_Status() != PN532_I2C_READY);
 
// Read bytes from PN532 into buffer
I2C_Master_Recv(PN532_I2C_ADDRESS, length + 2);
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
I2C_Read_Buffer((char *) buffer);
 
/* Remaining packet byte layout is as follows:
Byte Description
----- ----------------------
* 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
* 7-N Data (Length - 1 bytes)
* N+1 Data checksum (TFI + Data~N + DCS = 0x00)
* N+2 Postamble (0x00) */
}
 
// Writes a command to the PN532, automatically inserting the preamble and required frame details (checksum, len, etc.)
void NFC_I2C_Write_Cmd(unsigned char* cmd, unsigned char cmdlen) {
int i;
unsigned char checksum;
unsigned char buffer[PN532_PACKBUFFSIZ + 8];
unsigned char buffer_ind = 6;
cmdlen++;
 
checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2 + PN532_HOSTTOPN532;
 
// Fill out required frame fields
buffer[0] = PN532_PREAMBLE;
buffer[1] = PN532_PREAMBLE;
buffer[2] = PN532_STARTCODE2;
buffer[3] = cmdlen;
buffer[4] = ~cmdlen + 1;
buffer[5] = PN532_HOSTTOPN532;
 
 
// Copy cmd to be sent
for (i = 0; i < cmdlen - 1; i++) {
checksum += cmd[i];
buffer[buffer_ind] = cmd[i];
buffer_ind++;
}
 
buffer[buffer_ind] = ~checksum;
buffer_ind++;
buffer[buffer_ind] = PN532_POSTAMBLE;
buffer_ind++;
 
I2C_Master_Send(PN532_I2C_ADDRESS, buffer_ind, buffer);
}
/PIC Stuff/PIC_27J13/nfc_PN532.h
0,0 → 1,173
#ifndef NFC_H
#define NFC_H
 
/* PN532 NFC Reader from Adafruit */
 
#define PN532_PREAMBLE (0x00)
#define PN532_STARTCODE1 (0x00)
#define PN532_STARTCODE2 (0xFF)
#define PN532_POSTAMBLE (0x00)
 
#define PN532_HOSTTOPN532 (0xD4)
 
// PN532 Commands
#define PN532_COMMAND_DIAGNOSE (0x00)
#define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
#define PN532_COMMAND_GETGENERALSTATUS (0x04)
#define PN532_COMMAND_READREGISTER (0x06)
#define PN532_COMMAND_WRITEREGISTER (0x08)
#define PN532_COMMAND_READGPIO (0x0C)
#define PN532_COMMAND_WRITEGPIO (0x0E)
#define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
#define PN532_COMMAND_SETPARAMETERS (0x12)
#define PN532_COMMAND_SAMCONFIGURATION (0x14)
#define PN532_COMMAND_POWERDOWN (0x16)
#define PN532_COMMAND_RFCONFIGURATION (0x32)
#define PN532_COMMAND_RFREGULATIONTEST (0x58)
#define PN532_COMMAND_INJUMPFORDEP (0x56)
#define PN532_COMMAND_INJUMPFORPSL (0x46)
#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
#define PN532_COMMAND_INATR (0x50)
#define PN532_COMMAND_INPSL (0x4E)
#define PN532_COMMAND_INDATAEXCHANGE (0x40)
#define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
#define PN532_COMMAND_INDESELECT (0x44)
#define PN532_COMMAND_INRELEASE (0x52)
#define PN532_COMMAND_INSELECT (0x54)
#define PN532_COMMAND_INAUTOPOLL (0x60)
#define PN532_COMMAND_TGINITASTARGET (0x8C)
#define PN532_COMMAND_TGSETGENERALBYTES (0x92)
#define PN532_COMMAND_TGGETDATA (0x86)
#define PN532_COMMAND_TGSETDATA (0x8E)
#define PN532_COMMAND_TGSETMETADATA (0x94)
#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
 
#define PN532_WAKEUP (0x55)
 
#define PN532_SPI_STATREAD (0x02)
#define PN532_SPI_DATAWRITE (0x01)
#define PN532_SPI_DATAREAD (0x03)
#define PN532_SPI_READY (0x01)
 
#define PN532_I2C_ADDRESS (0x48 >> 1)
#define PN532_I2C_READBIT (0x01)
#define PN532_I2C_BUSY (0x00)
#define PN532_I2C_READY (0x01)
#define PN532_I2C_READYTIMEOUT (20)
 
#define PN532_MIFARE_ISO14443A (0x00)
 
// Mifare Commands
#define MIFARE_CMD_AUTH_A (0x60)
#define MIFARE_CMD_AUTH_B (0x61)
#define MIFARE_CMD_READ (0x30)
#define MIFARE_CMD_WRITE (0xA0)
#define MIFARE_CMD_TRANSFER (0xB0)
#define MIFARE_CMD_DECREMENT (0xC0)
#define MIFARE_CMD_INCREMENT (0xC1)
#define MIFARE_CMD_STORE (0xC2)
 
// Prefixes for NDEF Records (to identify record type)
#define NDEF_URIPREFIX_NONE (0x00)
#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01)
#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02)
#define NDEF_URIPREFIX_HTTP (0x03)
#define NDEF_URIPREFIX_HTTPS (0x04)
#define NDEF_URIPREFIX_TEL (0x05)
#define NDEF_URIPREFIX_MAILTO (0x06)
#define NDEF_URIPREFIX_FTP_ANONAT (0x07)
#define NDEF_URIPREFIX_FTP_FTPDOT (0x08)
#define NDEF_URIPREFIX_FTPS (0x09)
#define NDEF_URIPREFIX_SFTP (0x0A)
#define NDEF_URIPREFIX_SMB (0x0B)
#define NDEF_URIPREFIX_NFS (0x0C)
#define NDEF_URIPREFIX_FTP (0x0D)
#define NDEF_URIPREFIX_DAV (0x0E)
#define NDEF_URIPREFIX_NEWS (0x0F)
#define NDEF_URIPREFIX_TELNET (0x10)
#define NDEF_URIPREFIX_IMAP (0x11)
#define NDEF_URIPREFIX_RTSP (0x12)
#define NDEF_URIPREFIX_URN (0x13)
#define NDEF_URIPREFIX_POP (0x14)
#define NDEF_URIPREFIX_SIP (0x15)
#define NDEF_URIPREFIX_SIPS (0x16)
#define NDEF_URIPREFIX_TFTP (0x17)
#define NDEF_URIPREFIX_BTSPP (0x18)
#define NDEF_URIPREFIX_BTL2CAP (0x19)
#define NDEF_URIPREFIX_BTGOEP (0x1A)
#define NDEF_URIPREFIX_TCPOBEX (0x1B)
#define NDEF_URIPREFIX_IRDAOBEX (0x1C)
#define NDEF_URIPREFIX_FILE (0x1D)
#define NDEF_URIPREFIX_URN_EPC_ID (0x1E)
#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F)
#define NDEF_URIPREFIX_URN_EPC_PAT (0x20)
#define NDEF_URIPREFIX_URN_EPC_RAW (0x21)
#define NDEF_URIPREFIX_URN_EPC (0x22)
#define NDEF_URIPREFIX_URN_NFC (0x23)
 
#define PN532_GPIO_VALIDATIONBIT (0x80)
#define PN532_GPIO_P30 (0)
#define PN532_GPIO_P31 (1)
#define PN532_GPIO_P32 (2)
#define PN532_GPIO_P33 (3)
#define PN532_GPIO_P34 (4)
#define PN532_GPIO_P35 (5)
 
#define PN532_PACKBUFFSIZ 64
#define PN532_TIMEOUT 1000
 
typedef struct {
unsigned char IC;
unsigned char Ver;
unsigned char Rev;
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;
 
void NFC_Init(void);
 
// Generic PN532 functions
unsigned char NFC_SAMConfig(void);
NFC_FIRMWARE_VERSION NFC_getFirmwareVersion(void);
unsigned char NFC_sendCommandCheckAck(unsigned char *cmd, unsigned char cmdlen);
//unsigned char NFC_writeGPIO(unsigned char pinstate);
//unsigned char NFC_readGPIO(void);
 
// ISO14443A functions
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);
unsigned char NFC_mifareclassic_IsTrailerBlock(unsigned long uiBlock);
unsigned char NFC_mifareclassic_AuthenticateBlock(unsigned char *uid, unsigned char uidLen, unsigned long blockNumber, unsigned char keyNumber, unsigned char *keyData);
unsigned char NFC_mifareclassic_ReadDataBlock(unsigned char blockNumber, unsigned char *data);
unsigned char NFC_mifareclassic_WriteDataBlock(unsigned char blockNumber, unsigned char *data);
unsigned char NFC_mifareclassic_FormatNDEF(void);
unsigned char NFC_mifareclassic_WriteNDEFURI(unsigned char sectorNumber, unsigned char uriIdentifier, const char * url);
 
// Mifare Ultralight functions
//unsigned char NFC_mifareultralight_ReadPage(unsigned char page, unsigned char * buffer);
 
// Low level SPI functions
unsigned char NFC_I2C_Read_ACK(void);
unsigned char NFC_I2C_Read_Status(void);
void NFC_I2C_Read_Data(unsigned char *buffer, unsigned char length);
void NFC_I2C_Write_Cmd(unsigned char *cmd, unsigned char cmdlen);
 
#endif
 
/PIC Stuff/PIC_27J13/oled_NHD-0216KZW-AB5.c
0,0 → 1,196
#include "oled_NHD-0216KZW-AB5.h"
#include "defines.h"
#include <delays.h>
#include <string.h>
#include <stdio.h>
 
static OLED_CHAR_DATA oled_char_data;
static OLED_CHAR_DATA *oled_char_data_p = &oled_char_data;
 
void NHD_Init() {
PARALLEL_RS_TRIS = 0;
PARALLEL_RW_TRIS = 0;
PARALLEL_EN_TRIS = 0;
 
PARALLEL_D4_TRIS = 0;
PARALLEL_D5_TRIS = 0;
PARALLEL_D6_TRIS = 0;
PARALLEL_D7_TRIS = 0;
 
oled_char_data_p->display_function = LCD_FUNCTIONSET | LCD_4BITMODE;
}
 
void NHD_Begin(char cols, char rows) {
oled_char_data_p->num_lines = rows;
oled_char_data_p->current_line = 0;
 
PARALLEL_RS_LAT = 0;
PARALLEL_RW_LAT = 0;
PARALLEL_EN_LAT = 0;
 
PARALLEL_D4_LAT = 0;
PARALLEL_D5_LAT = 0;
PARALLEL_D6_LAT = 0;
PARALLEL_D7_LAT = 0;
Delay10KTCYx(1); // ~1ms
 
// Initialization sequence
NHD_Write_4_Bits(0x3);
NHD_Write_4_Bits(0x2);
NHD_Write_4_Bits(0x2);
NHD_Write_4_Bits(0x8);
NHD_Wait_For_Ready();
 
NHD_Send_Command(0x08); // Turn Off
NHD_Send_Command(0x01); // Clear Display
NHD_Send_Command(0x06); // Set Entry Mode
NHD_Send_Command(0x02); // Return to Home Position
NHD_Send_Command(0x0C); // Turn On
}
 
void NHD_Clear() {
NHD_Send_Command(LCD_CLEARDISPLAY);
}
 
void NHD_Home() {
NHD_Send_Command(LCD_RETURNHOME);
}
 
void NHD_Set_Cursor(unsigned char col, unsigned char row) {
unsigned char row_offsets[] = {0x00, 0x40, 0x14, 0x54};
if (row >= oled_char_data_p->num_lines) {
row = 0;
}
NHD_Send_Command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}
 
void NHD_Display(char option) {
if (option) {
oled_char_data_p->display_control |= LCD_DISPLAYON;
} else {
oled_char_data_p->display_control &= ~LCD_DISPLAYON;
}
NHD_Send_Command(LCD_DISPLAYCONTROL | oled_char_data_p->display_control);
}
 
void NHD_Blink(char option) {
if (option) {
oled_char_data_p->display_control |= LCD_BLINKON;
} else {
oled_char_data_p->display_control &= ~LCD_BLINKON;
}
NHD_Send_Command(LCD_DISPLAYCONTROL | oled_char_data_p->display_control);
}
 
void NHD_Cursor(char option) {
if (option) {
oled_char_data_p->display_control |= LCD_CURSORON;
} else {
oled_char_data_p->display_control &= ~LCD_CURSORON;
}
NHD_Send_Command(LCD_DISPLAYCONTROL | oled_char_data_p->display_control);
}
 
void NHD_Scroll_Display_Left() {
NHD_Send_Command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
 
void NHD_Scroll_Display_Right() {
NHD_Send_Command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}
 
void NHD_Left_To_Rigtht() {
oled_char_data_p->display_mode |= LCD_ENTRYLEFT;
NHD_Send_Command(LCD_ENTRYMODESET | oled_char_data_p->display_mode);
}
 
void NHD_Right_To_Left() {
oled_char_data_p->display_mode &= ~LCD_ENTRYLEFT;
NHD_Send_Command(LCD_ENTRYMODESET | oled_char_data_p->display_mode);
}
 
void NHD_Autoscroll(char option) {
if (option) {
oled_char_data_p->display_mode |= LCD_ENTRYSHIFTINCREMENT;
} else {
oled_char_data_p->display_mode &= ~LCD_ENTRYSHIFTINCREMENT;
}
NHD_Send_Command(LCD_ENTRYMODESET | oled_char_data_p->display_mode);
}
 
void NHD_Create_Char(unsigned char location, unsigned char *charmap) {
char i;
location &= 0x7;
NHD_Send_Command(LCD_SETCGRAMADDR | (location << 3));
for (i = 0; i < 8; i++) {
NHD_Send_Data(charmap[i]);
}
}
 
void NHD_Send_Command(unsigned char value) {
PARALLEL_RS_LAT = 0;
PARALLEL_RW_LAT = 0;
NHD_Write_4_Bits(value>>4);
NHD_Write_4_Bits(value);
NHD_Wait_For_Ready();
}
 
void NHD_Send_Data(unsigned char value) {
PARALLEL_RS_LAT = 1;
PARALLEL_RW_LAT = 0;
NHD_Write_4_Bits(value>>4);
NHD_Write_4_Bits(value);
NHD_Wait_For_Ready();
}
 
void NHD_Pulse_Enable(void) {
PARALLEL_EN_LAT = 1;
Nop();
Nop();
PARALLEL_EN_LAT = 0;
}
 
void NHD_Write_4_Bits(unsigned char value) {
PARALLEL_D4_LAT = (value) & 0x01;
PARALLEL_D5_LAT = (value>>1) & 0x01;
PARALLEL_D6_LAT = (value>>2) & 0x01;
PARALLEL_D7_LAT = (value>>3) & 0x01;
NHD_Pulse_Enable();
}
 
void NHD_Wait_For_Ready() {
char busy;
PARALLEL_BUSY_TRIS = 1;
PARALLEL_RS_LAT = 0;
PARALLEL_RW_LAT = 1;
do {
NHD_Pulse_Enable();
Nop();
busy = PARALLEL_BUSY_PORT;
NHD_Pulse_Enable();
} while (busy);
PARALLEL_BUSY_TRIS = 0;
PARALLEL_RW_LAT = 0;
}
 
void NHD_Write_String(const rom char *fmt, ...) {
unsigned char i, len;
unsigned char buffer[NHD_STRING_BUFFER_SIZE];
 
// Parse and create string
va_list args;
va_start(args, fmt);
vsprintf((char *) buffer, fmt, args);
va_end(args);
len = strlen((char *) buffer);
 
// Make sure string to insert fits in buffer, truncate if necessary
if (len > NHD_STRING_BUFFER_SIZE)
len = NHD_STRING_BUFFER_SIZE;
 
// Print buffer to string
for (i = 0; i < len; i++) {
NHD_Send_Data(buffer[i]);
}
}
/PIC Stuff/PIC_27J13/oled_NHD-0216KZW-AB5.h
0,0 → 1,78
#ifndef OLED_NHD_0216KZW_AB5_H
#define OLED_NHD_0216KZW_AB5_H
 
#define NHD_STRING_BUFFER_SIZE 64
 
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x28
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
 
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
 
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
 
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
 
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_JAPANESE 0x00
#define LCD_EUROPEAN_I 0x01
#define LCD_RUSSIAN 0x02
#define LCD_EUROPEAN_II 0x03
 
typedef struct __OLED_CHAR_DATA {
unsigned char display_function;
unsigned char display_control;
unsigned char display_mode;
unsigned char current_line;
unsigned char num_lines;
} OLED_CHAR_DATA;
 
void NHD_Init(void);
void NHD_Begin(char cols, char rows);
void NHD_Clear(void);
void NHD_Home(void);
void NHD_Display(char option);
void NHD_Blink(char option);
void NHD_Cursor(char option);
void NHD_Autoscroll(char option);
void NHD_Scroll_Display_Left(void);
void NHD_Scroll_Display_Right(void);
void NHD_Left_To_Rigtht(void);
void NHD_Right_To_Left(void);
 
void NHD_Create_Char(unsigned char location, unsigned char *charmap);
void NHD_Set_Cursor(unsigned char col, unsigned char row);
 
void NHD_Send_Data(unsigned char value);
void NHD_Send_Command(unsigned char value);
 
void NHD_Pulse_Enable(void);
void NHD_Write_4_Bits(unsigned char value);
void NHD_Wait_For_Ready(void);
 
void NHD_Write_String(const rom char *fmt, ...);
 
#endif /* OLED_NHD_0216KZW_AB5_H */
 
/PIC Stuff/PIC_27J13/oled_ssd1306.c
1,7 → 1,6
#include "defines.h"
#include "oled_ssd1306.h"
#include "spi.h"
#include "string.h"
#include "glcdfont.c"
#include <delays.h>
#include <string.h>