Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 154 → Rev 155

/PIC Stuff/PICX_27J13/adc.c
0,0 → 1,55
#include <xc.h>
#include "defines.h"
#include "adc.h"
 
static ADC_DATA *adc_data_p;
 
void ADC_Init(ADC_DATA *data, char TAD, char FOSC) {
adc_data_p = data;
ADC_AN0_TRIS = 1;
ADC_AN1_TRIS = 1;
ADC_AN2_TRIS = 1;
 
adc_data_p->last_channel = 0;
adc_data_p->result = 0;
 
ADCON0bits.VCFG1 = 0; // VRef- = AVss
ADCON0bits.VCFG0 = 1; // VRef+ != AVdd
ADCON1bits.ADFM = 1; // Right justified result
ADCON1bits.ADCAL = 1; // Calibrate A/D
ADCON1bits.ACQT = TAD;
ADCON1bits.ADCS = FOSC;
ADCON0bits.ADON = 1; // Enable A/D module
 
ADCON0bits.GO_DONE = 1; // Start calibration
while (ADCON0bits.GO_DONE); // Wait for calibration to finish
PIR1bits.ADIF = 0; // Clear the IF flag
ADCON1bits.ADCAL = 0; // Normal A/D operation
 
PIE1bits.ADIE = 1; // Enable A/D interrupt
 
}
 
void ADC_Start(char channel) {
adc_data_p->last_channel = channel;
ADCON0bits.CHS = channel; // Set A/D channel
ADCON0bits.GO_DONE = 1; // Start A/D conversion
}
 
void ADC_Stop() {
ADCON0bits.ADON = 0; // Disable A/D module
}
 
void ADC_Interrupt_Handler() {
adc_data_p->result = ADRES;
}
 
char ADC_Get_Result(unsigned int* ret) {
if (ADCON0bits.GO_DONE) {
return 0;
} else {
*ret = adc_data_p->result;
return 1;
}
}
/PIC Stuff/PICX_27J13/adc.h
0,0 → 1,48
#ifndef ADC_H
#define ADC_H
 
#define ADC_CHANNEL_AN0 0b0000
#define ADC_CHANNEL_AN1 0b0001
#define ADC_CHANNEL_AN2 0b0010
#define ADC_CHANNEL_AN3 0b0011
#define ADC_CHANNEL_AN4 0b0100
#define ADC_CHANNEL_AN5 0b0101
#define ADC_CHANNEL_AN6 0b0110
#define ADC_CHANNEL_AN7 0b0111
#define ADC_CHANNEL_AN8 0b1000
#define ADC_CHANNEL_AN9 0b1001
#define ADC_CHANNEL_AN10 0b1010
#define ADC_CHANNEL_AN11 0b1011
#define ADC_CHANNEL_AN12 0b1100
#define ADC_CHANNEL_VDDCORE 0b1110
#define ADC_CHANNEL_ABG 0b1111
 
#define ADC_TAD_20 0b111
#define ADC_TAD_16 0b110
#define ADC_TAD_12 0b101
#define ADC_TAD_8 0b100
#define ADC_TAD_6 0b011
#define ADC_TAD_4 0b010
#define ADC_TAD_2 0b001
#define ADC_TAD_0 0b000
 
#define ADC_FOSC_64_ 0b110
#define ADC_FOSC_32_ 0b010
#define ADC_FOSC_16_ 0b101
#define ADC_FOSC_8_ 0b001
#define ADC_FOSC_4_ 0b100
#define ADC_FOSC_2_ 0b000
#define ADC_FOSC_FRC_ 0b011
 
typedef struct __ADC_DATA {
char last_channel;
unsigned int result;
} ADC_DATA;
 
void ADC_Init(ADC_DATA *data, char TAD, char FOSC);
void ADC_Start(char channel);
void ADC_Stop(void);
void ADC_Interrupt_Handler(void);
char ADC_Get_Result(unsigned int *ret);
 
#endif
/PIC Stuff/PICX_27J13/defines.h
5,6 → 5,7
//#define UART1_RX_TO_XBEE
 
#define _DEBUG
 
//#define _TEST_UART
//#define _TEST_I2C_MASTER
//#define _TEST_I2C_SLAVE
13,28 → 14,41
//#define _TEST_LED_BACKPACK
//#define _TEST_SSD1306_OLED
//#define _TEST_SSD1331_OLED
//#define _TEST_TIMER1_RTC
//#define _TEST_LUX
//#define _TEST_OLED_CHAR
//#define _TEST_NFC_TO_SSD1306_OLED
//#define _TEST_LUX_TO_CHAR_OLED
//#define _TEST_ADC
//#define _TEST_XBEE
//#define _TEST_BMP //TODO: Test this again once compiler is fixed
//#define _TEST_XBEE // TODO: Reimplement this
 
 
 
 
//#define _TEST_BMP
 
// Enable or disable debug prints depending on project preprocessor (_DEBUG)
#ifdef _DEBUG
#define DBG_PRINT_MAIN UART1_WriteS
#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) UART1_WriteS(x)
#define DBG_PRINT_PORTB_INT(x)
#define DBG_PRINT_INT(x)
#define DBG_PRINT_BUFFER(x)
#define DBG_PRINT_UART UART1_WriteS
#define DBG_PRINT_I2C UART1_WriteS
#define DBG_PRINT_SPI UART1_WriteS
#define DBG_PRINT_XBEE UART1_WriteS
#define DBG_PRINT_PORTB_INT
#define DBG_PRINT_INT
#define DBG_PRINT_LUX
#define DBG_PRINT_BMP
#else
#define DBG_PRINT_MAIN(x)
#define DBG_PRINT_UART(x)
#define DBG_PRINT_I2C(x)
#define DBG_PRINT_SPI(x)
#define DBG_PRINT_XBEE(x)
#define DBG_PRINT_PORTB_INT(x)
#define DBG_PRINT_INT(x)
#define DBG_PRINT_BUFFER(x)
#define DBG_PRINT_MAIN
#define DBG_PRINT_UART
#define DBG_PRINT_I2C
#define DBG_PRINT_SPI
#define DBG_PRINT_XBEE
#define DBG_PRINT_PORTB_INT
#define DBG_PRINT_INT
#define DBG_PRINT_LUX
#define DBG_PRINT_BMP
#endif
 
// Pin allocations
/PIC Stuff/PICX_27J13/funclist
1,4 → 1,22
__stringdata: SMALLCONST, 3840 0 25
_main: CODE, 3866 0 224
__initialization: CODE, 4090 0 44
Total: 293
_I2C_Interrupt_Master: CODE, 67304 0 1764
exp@coeff: MEDIUMCONST, 0 0 65155
__stringdata: MEDIUMCONST, 65206 0 329
_UART1_WriteS: CODE, 71142 0 64
_I2C_Interrupt_Slave: CODE, 65536 0 1768
_strlen: CODE, 71074 0 68
_font: MEDIUMCONST, 63824 0 1275
_UART1_Recv_Interrupt_Handler: CODE, 70092 0 428
_pn532response_firmwarevers: MEDIUMCONST, 65191 0 8
_main: CODE, 71210 0 4
_numbertable: MEDIUMCONST, 65099 0 10
_pn532ack: MEDIUMCONST, 65199 0 7
_I2C_Process_Send: CODE, 70520 0 164
_I2C_Interrupt_Handler: CODE, 71002 0 72
__initialization: CODE, 70822 0 90
_alphatable: MEDIUMCONST, 65109 0 6
_sprintf: CODE, 70918 0 84
_InterruptHandlerLow: CODE, 24 0 154
_Timer1_Interrupt_Handler: CODE, 71214 0 2
_InterruptHandlerHigh: CODE, 8 0 70814
log@coeff: MEDIUMCONST, 0 0 65191
Total: 207457
/PIC Stuff/PICX_27J13/glcdfont.c
0,0 → 1,263
#ifndef FONT5X7_H
#define FONT5X7_H
 
// standard ascii 5x7 font
 
const char font[] = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x1C, 0x57, 0x7D, 0x57, 0x1C,
0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
0x00, 0x18, 0x3C, 0x18, 0x00,
0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
0x00, 0x18, 0x24, 0x18, 0x00,
0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
0x30, 0x48, 0x3A, 0x06, 0x0E,
0x26, 0x29, 0x79, 0x29, 0x26,
0x40, 0x7F, 0x05, 0x05, 0x07,
0x40, 0x7F, 0x05, 0x25, 0x3F,
0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
0x7F, 0x3E, 0x1C, 0x1C, 0x08,
0x08, 0x1C, 0x1C, 0x3E, 0x7F,
0x14, 0x22, 0x7F, 0x22, 0x14,
0x5F, 0x5F, 0x00, 0x5F, 0x5F,
0x06, 0x09, 0x7F, 0x01, 0x7F,
0x00, 0x66, 0x89, 0x95, 0x6A,
0x60, 0x60, 0x60, 0x60, 0x60,
0x94, 0xA2, 0xFF, 0xA2, 0x94,
0x08, 0x04, 0x7E, 0x04, 0x08,
0x10, 0x20, 0x7E, 0x20, 0x10,
0x08, 0x08, 0x2A, 0x1C, 0x08,
0x08, 0x1C, 0x2A, 0x08, 0x08,
0x1E, 0x10, 0x10, 0x10, 0x10,
0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
0x30, 0x38, 0x3E, 0x38, 0x30,
0x06, 0x0E, 0x3E, 0x0E, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14,
0x24, 0x2A, 0x7F, 0x2A, 0x12,
0x23, 0x13, 0x08, 0x64, 0x62,
0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x08, 0x07, 0x03, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
0x08, 0x08, 0x3E, 0x08, 0x08,
0x00, 0x80, 0x70, 0x30, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08,
0x00, 0x00, 0x60, 0x60, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02,
0x3E, 0x51, 0x49, 0x45, 0x3E,
0x00, 0x42, 0x7F, 0x40, 0x00,
0x72, 0x49, 0x49, 0x49, 0x46,
0x21, 0x41, 0x49, 0x4D, 0x33,
0x18, 0x14, 0x12, 0x7F, 0x10,
0x27, 0x45, 0x45, 0x45, 0x39,
0x3C, 0x4A, 0x49, 0x49, 0x31,
0x41, 0x21, 0x11, 0x09, 0x07,
0x36, 0x49, 0x49, 0x49, 0x36,
0x46, 0x49, 0x49, 0x29, 0x1E,
0x00, 0x00, 0x14, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41,
0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x41, 0x22, 0x14, 0x08,
0x02, 0x01, 0x59, 0x09, 0x06,
0x3E, 0x41, 0x5D, 0x59, 0x4E,
0x7C, 0x12, 0x11, 0x12, 0x7C,
0x7F, 0x49, 0x49, 0x49, 0x36,
0x3E, 0x41, 0x41, 0x41, 0x22,
0x7F, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x49, 0x49, 0x49, 0x41,
0x7F, 0x09, 0x09, 0x09, 0x01,
0x3E, 0x41, 0x41, 0x51, 0x73,
0x7F, 0x08, 0x08, 0x08, 0x7F,
0x00, 0x41, 0x7F, 0x41, 0x00,
0x20, 0x40, 0x41, 0x3F, 0x01,
0x7F, 0x08, 0x14, 0x22, 0x41,
0x7F, 0x40, 0x40, 0x40, 0x40,
0x7F, 0x02, 0x1C, 0x02, 0x7F,
0x7F, 0x04, 0x08, 0x10, 0x7F,
0x3E, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x09, 0x09, 0x09, 0x06,
0x3E, 0x41, 0x51, 0x21, 0x5E,
0x7F, 0x09, 0x19, 0x29, 0x46,
0x26, 0x49, 0x49, 0x49, 0x32,
0x03, 0x01, 0x7F, 0x01, 0x03,
0x3F, 0x40, 0x40, 0x40, 0x3F,
0x1F, 0x20, 0x40, 0x20, 0x1F,
0x3F, 0x40, 0x38, 0x40, 0x3F,
0x63, 0x14, 0x08, 0x14, 0x63,
0x03, 0x04, 0x78, 0x04, 0x03,
0x61, 0x59, 0x49, 0x4D, 0x43,
0x00, 0x7F, 0x41, 0x41, 0x41,
0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x41, 0x41, 0x41, 0x7F,
0x04, 0x02, 0x01, 0x02, 0x04,
0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x03, 0x07, 0x08, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40,
0x7F, 0x28, 0x44, 0x44, 0x38,
0x38, 0x44, 0x44, 0x44, 0x28,
0x38, 0x44, 0x44, 0x28, 0x7F,
0x38, 0x54, 0x54, 0x54, 0x18,
0x00, 0x08, 0x7E, 0x09, 0x02,
0x18, 0xA4, 0xA4, 0x9C, 0x78,
0x7F, 0x08, 0x04, 0x04, 0x78,
0x00, 0x44, 0x7D, 0x40, 0x00,
0x20, 0x40, 0x40, 0x3D, 0x00,
0x7F, 0x10, 0x28, 0x44, 0x00,
0x00, 0x41, 0x7F, 0x40, 0x00,
0x7C, 0x04, 0x78, 0x04, 0x78,
0x7C, 0x08, 0x04, 0x04, 0x78,
0x38, 0x44, 0x44, 0x44, 0x38,
0xFC, 0x18, 0x24, 0x24, 0x18,
0x18, 0x24, 0x24, 0x18, 0xFC,
0x7C, 0x08, 0x04, 0x04, 0x08,
0x48, 0x54, 0x54, 0x54, 0x24,
0x04, 0x04, 0x3F, 0x44, 0x24,
0x3C, 0x40, 0x40, 0x20, 0x7C,
0x1C, 0x20, 0x40, 0x20, 0x1C,
0x3C, 0x40, 0x30, 0x40, 0x3C,
0x44, 0x28, 0x10, 0x28, 0x44,
0x4C, 0x90, 0x90, 0x90, 0x7C,
0x44, 0x64, 0x54, 0x4C, 0x44,
0x00, 0x08, 0x36, 0x41, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02,
0x3C, 0x26, 0x23, 0x26, 0x3C,
0x1E, 0xA1, 0xA1, 0x61, 0x12,
0x3A, 0x40, 0x40, 0x20, 0x7A,
0x38, 0x54, 0x54, 0x55, 0x59,
0x21, 0x55, 0x55, 0x79, 0x41,
0x21, 0x54, 0x54, 0x78, 0x41,
0x21, 0x55, 0x54, 0x78, 0x40,
0x20, 0x54, 0x55, 0x79, 0x40,
0x0C, 0x1E, 0x52, 0x72, 0x12,
0x39, 0x55, 0x55, 0x55, 0x59,
0x39, 0x54, 0x54, 0x54, 0x59,
0x39, 0x55, 0x54, 0x54, 0x58,
0x00, 0x00, 0x45, 0x7C, 0x41,
0x00, 0x02, 0x45, 0x7D, 0x42,
0x00, 0x01, 0x45, 0x7C, 0x40,
0xF0, 0x29, 0x24, 0x29, 0xF0,
0xF0, 0x28, 0x25, 0x28, 0xF0,
0x7C, 0x54, 0x55, 0x45, 0x00,
0x20, 0x54, 0x54, 0x7C, 0x54,
0x7C, 0x0A, 0x09, 0x7F, 0x49,
0x32, 0x49, 0x49, 0x49, 0x32,
0x32, 0x48, 0x48, 0x48, 0x32,
0x32, 0x4A, 0x48, 0x48, 0x30,
0x3A, 0x41, 0x41, 0x21, 0x7A,
0x3A, 0x42, 0x40, 0x20, 0x78,
0x00, 0x9D, 0xA0, 0xA0, 0x7D,
0x39, 0x44, 0x44, 0x44, 0x39,
0x3D, 0x40, 0x40, 0x40, 0x3D,
0x3C, 0x24, 0xFF, 0x24, 0x24,
0x48, 0x7E, 0x49, 0x43, 0x66,
0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
0xFF, 0x09, 0x29, 0xF6, 0x20,
0xC0, 0x88, 0x7E, 0x09, 0x03,
0x20, 0x54, 0x54, 0x79, 0x41,
0x00, 0x00, 0x44, 0x7D, 0x41,
0x30, 0x48, 0x48, 0x4A, 0x32,
0x38, 0x40, 0x40, 0x22, 0x7A,
0x00, 0x7A, 0x0A, 0x0A, 0x72,
0x7D, 0x0D, 0x19, 0x31, 0x7D,
0x26, 0x29, 0x29, 0x2F, 0x28,
0x26, 0x29, 0x29, 0x29, 0x26,
0x30, 0x48, 0x4D, 0x40, 0x20,
0x38, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x38,
0x2F, 0x10, 0xC8, 0xAC, 0xBA,
0x2F, 0x10, 0x28, 0x34, 0xFA,
0x00, 0x00, 0x7B, 0x00, 0x00,
0x08, 0x14, 0x2A, 0x14, 0x22,
0x22, 0x14, 0x2A, 0x14, 0x08,
0xAA, 0x00, 0x55, 0x00, 0xAA,
0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x00, 0x00, 0x00, 0xFF, 0x00,
0x10, 0x10, 0x10, 0xFF, 0x00,
0x14, 0x14, 0x14, 0xFF, 0x00,
0x10, 0x10, 0xFF, 0x00, 0xFF,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x14, 0x14, 0x14, 0xFC, 0x00,
0x14, 0x14, 0xF7, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x14, 0x14, 0xF4, 0x04, 0xFC,
0x14, 0x14, 0x17, 0x10, 0x1F,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0x1F, 0x00,
0x10, 0x10, 0x10, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x1F, 0x10,
0x10, 0x10, 0x10, 0x1F, 0x10,
0x10, 0x10, 0x10, 0xF0, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0xFF, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x14,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x1F, 0x10, 0x17,
0x00, 0x00, 0xFC, 0x04, 0xF4,
0x14, 0x14, 0x17, 0x10, 0x17,
0x14, 0x14, 0xF4, 0x04, 0xF4,
0x00, 0x00, 0xFF, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0xF7, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x17, 0x14,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0xF4, 0x14,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x00, 0x00, 0x1F, 0x10, 0x1F,
0x00, 0x00, 0x00, 0x1F, 0x14,
0x00, 0x00, 0x00, 0xFC, 0x14,
0x00, 0x00, 0xF0, 0x10, 0xF0,
0x10, 0x10, 0xFF, 0x10, 0xFF,
0x14, 0x14, 0x14, 0xFF, 0x14,
0x10, 0x10, 0x10, 0x1F, 0x00,
0x00, 0x00, 0x00, 0xF0, 0x10,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0xFF,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x38, 0x44, 0x44, 0x38, 0x44,
0x7C, 0x2A, 0x2A, 0x3E, 0x14,
0x7E, 0x02, 0x02, 0x06, 0x06,
0x02, 0x7E, 0x02, 0x7E, 0x02,
0x63, 0x55, 0x49, 0x41, 0x63,
0x38, 0x44, 0x44, 0x3C, 0x04,
0x40, 0x7E, 0x20, 0x1E, 0x20,
0x06, 0x02, 0x7E, 0x02, 0x02,
0x99, 0xA5, 0xE7, 0xA5, 0x99,
0x1C, 0x2A, 0x49, 0x2A, 0x1C,
0x4C, 0x72, 0x01, 0x72, 0x4C,
0x30, 0x4A, 0x4D, 0x4D, 0x30,
0x30, 0x48, 0x78, 0x48, 0x30,
0xBC, 0x62, 0x5A, 0x46, 0x3D,
0x3E, 0x49, 0x49, 0x49, 0x00,
0x7E, 0x01, 0x01, 0x01, 0x7E,
0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
0x44, 0x44, 0x5F, 0x44, 0x44,
0x40, 0x51, 0x4A, 0x44, 0x40,
0x40, 0x44, 0x4A, 0x51, 0x40,
0x00, 0x00, 0xFF, 0x01, 0x03,
0xE0, 0x80, 0xFF, 0x00, 0x00,
0x08, 0x08, 0x6B, 0x6B, 0x08,
0x36, 0x12, 0x36, 0x24, 0x36,
0x06, 0x0F, 0x09, 0x0F, 0x06,
0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x00, 0x10, 0x10, 0x00,
0x30, 0x40, 0xFF, 0x01, 0x01,
0x00, 0x1F, 0x01, 0x01, 0x1E,
0x00, 0x19, 0x1D, 0x17, 0x12,
0x00, 0x3C, 0x3C, 0x3C, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00,
};
#endif
/PIC Stuff/PICX_27J13/i2c.c
0,0 → 1,564
#include <xc.h>
#include <stdio.h>
#include <string.h>
#include "defines.h"
#include "i2c.h"
#include "uart.h"
 
static I2C_DATA *i2c_data_p;
 
// Set up the data structures for the i2c code
// Should be called once before any i2c routines are called
void I2C_Init(I2C_DATA *data) {
i2c_data_p = data;
i2c_data_p->buffer_in_len = 0;
i2c_data_p->buffer_in_len_tmp = 0;
i2c_data_p->buffer_in_read_ind = 0;
i2c_data_p->buffer_in_write_ind = 0;
 
i2c_data_p->buffer_out_ind = 0;
i2c_data_p->buffer_out_len = 0;
i2c_data_p->operating_mode = 0;
i2c_data_p->operating_state = I2C_IDLE;
i2c_data_p->return_status = 0;
 
i2c_data_p->slave_in_last_byte = 0;
i2c_data_p->slave_sending_data = 0;
 
i2c_data_p->master_dest_addr = 0;
i2c_data_p->master_status = I2C_MASTER_IDLE;
// Enable I2C interrupt
PIE1bits.SSPIE = 1;
}
 
// Setup the PIC to operate as a master.
void I2C_Configure_Master(char speed) {
i2c_data_p->operating_mode = I2C_MODE_MASTER;
 
I2C_CLK_TRIS = 1;
I2C_DAT_TRIS = 1;
 
SSPSTAT = 0x0;
SSPCON1 = 0x0;
SSPCON2 = 0x0;
SSPCON1bits.SSPM = 0x8; // I2C Master Mode
if (speed) {
SSPADD = 0x74; // Operate at 100KHz (48MHz)
} else {
SSPADD = 0x1A; // Operate at 400KHz (48MHz)
}
SSPSTATbits.SMP = 1; // Disable Slew Rate Control
SSPCON1bits.SSPEN = 1; // Enable MSSP Module
}
 
// Sends length number of bytes in msg to specified address (no R/W bit)
void I2C_Master_Send(char address, char length, char *msg) {
char i;
if (length == 0)
return;
// Copy message to send into buffer and save length/address
for (i = 0; i < length; i++) {
i2c_data_p->buffer_in[i] = msg[i];
}
i2c_data_p->buffer_in_len = length;
i2c_data_p->master_dest_addr = address;
i2c_data_p->buffer_in_read_ind = 0;
i2c_data_p->buffer_in_write_ind = 0;
 
// Change status to 'next' operation
i2c_data_p->operating_state = I2C_SEND_ADDR;
i2c_data_p->master_status = I2C_MASTER_SEND;
// Generate start condition
SSPCON2bits.SEN = 1;
}
 
// Reads length number of bytes from address (no R/W bit)
void I2C_Master_Recv(char address, char length) {
if (length == 0)
return;
 
// Save length and address to get data from
i2c_data_p->buffer_in_len = length;
i2c_data_p->master_dest_addr = address;
i2c_data_p->buffer_in_read_ind = 0;
i2c_data_p->buffer_in_write_ind = 0;
 
// Change status to 'next' operation
i2c_data_p->operating_state = I2C_SEND_ADDR;
i2c_data_p->master_status = I2C_MASTER_RECV;
// Generate start condition
SSPCON2bits.SEN = 1;
}
 
// Writes msg to address then reads length number of bytes from address
void I2C_Master_Restart(char address, char msg, char length) {
char c;
if (length == 0) {
c = msg;
I2C_Master_Send(address, 1, &c);
return;
}
 
// Save length and address to get data from
i2c_data_p->buffer_in[0] = msg;
i2c_data_p->buffer_in_len = length;
i2c_data_p->master_dest_addr = address;
i2c_data_p->buffer_in_read_ind = 0;
i2c_data_p->buffer_in_write_ind = 0;
 
// Change status to 'next' operation
i2c_data_p->operating_state = I2C_SEND_ADDR;
i2c_data_p->master_status = I2C_MASTER_RESTART;
 
// Generate start condition
SSPCON2bits.SEN = 1;
}
 
// Setup the PIC to operate as a slave. The address must not include the R/W bit
void I2C_Configure_Slave(char addr) {
i2c_data_p->operating_mode = I2C_MODE_SLAVE;
 
// Ensure the two lines are set for input (we are a slave)
I2C_CLK_TRIS = 1;
I2C_DAT_TRIS = 1;
 
SSPADD = addr << 1; // Set the slave address
 
SSPSTAT = 0x0;
SSPCON1 = 0x0;
SSPCON2 = 0x0;
SSPCON1bits.SSPM = 0xE; // Enable Slave 7-bit w/ start/stop interrupts
SSPSTATbits.SMP = 1; // Slew Off
SSPCON2bits.SEN = 1; // Enable clock-stretching
SSPCON1bits.SSPEN = 1; // Enable MSSP Module
}
 
void I2C_Interrupt_Handler() {
// Call interrupt depending on which mode we are operating in
if (i2c_data_p->operating_mode == I2C_MODE_MASTER) {
I2C_Interrupt_Master();
} else if (i2c_data_p->operating_mode == I2C_MODE_SLAVE) {
I2C_Interrupt_Slave();
}
}
 
// An internal subroutine used in the master version of the i2c_interrupt_handler
void I2C_Interrupt_Master() {
// If we are in the middle of sending data
if (i2c_data_p->master_status == I2C_MASTER_SEND) {
switch (i2c_data_p->operating_state) {
case I2C_IDLE:
break;
case I2C_SEND_ADDR:
// Send the address with read bit set
i2c_data_p->operating_state = I2C_CHECK_ACK_SEND;
SSPBUF = (i2c_data_p->master_dest_addr << 1) | 0x0;
break;
case I2C_CHECK_ACK_SEND:
// Check if ACK is received or not
if (!SSPCON2bits.ACKSTAT) {
// If an ACK is received, send next byte of data
if (i2c_data_p->buffer_in_read_ind < i2c_data_p->buffer_in_len) {
SSPBUF = i2c_data_p->buffer_in[i2c_data_p->buffer_in_read_ind];
i2c_data_p->buffer_in_read_ind++;
} else {
// If no more data is to be sent, send stop bit
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_SEND_OK;
}
} else {
// If a NACK is received, stop transmission and send error
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_SEND_FAIL;
}
break;
}
// If we are in the middle of receiving data
} else if (i2c_data_p->master_status == I2C_MASTER_RECV) {
switch (i2c_data_p->operating_state) {
case I2C_IDLE:
break;
case I2C_SEND_ADDR:
// Send address with write bit set
i2c_data_p->operating_state = I2C_CHECK_ACK_RECV;
SSPBUF = (i2c_data_p->master_dest_addr << 1) | 0x1;
break;
case I2C_CHECK_ACK_RECV:
// Check if ACK is received
if (!SSPCON2bits.ACKSTAT) {
// If an ACK is received, set module to receive 1 byte of data
i2c_data_p->operating_state = I2C_RCV_DATA;
SSPCON2bits.RCEN = 1;
} else {
// If a NACK is received, stop transmission and send error
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_RECV_FAIL;
}
break;
case I2C_RCV_DATA:
// On receive, save byte into buffer
// TODO: handle i2c buffer overflow
i2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = SSPBUF;
i2c_data_p->buffer_in_write_ind++;
if (i2c_data_p->buffer_in_write_ind < i2c_data_p->buffer_in_len) {
// If we still need to read, send an ACK to the slave
i2c_data_p->operating_state = I2C_REQ_DATA;
SSPCON2bits.ACKDT = 0; // ACK
SSPCON2bits.ACKEN = 1;
} else {
// If we are done reading, send an NACK to the slave
i2c_data_p->operating_state = I2C_SEND_STOP;
SSPCON2bits.ACKDT = 1; // NACK
SSPCON2bits.ACKEN = 1;
}
break;
case I2C_REQ_DATA:
// Set module to receive one byte of data
i2c_data_p->operating_state = I2C_RCV_DATA;
SSPCON2bits.RCEN = 1;
break;
case I2C_SEND_STOP:
// Send the stop bit and copy message to send to Main()
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_RECV_OK;
break;
}
} else if (i2c_data_p->master_status == I2C_MASTER_RESTART) {
switch (i2c_data_p->operating_state) {
case I2C_IDLE:
break;
case I2C_SEND_ADDR:
// Send the address with read bit set
i2c_data_p->operating_state = I2C_CHECK_ACK_SEND;
SSPBUF = (i2c_data_p->master_dest_addr << 1) | 0x0;
break;
case I2C_CHECK_ACK_SEND:
// Check if ACK is received or not
if (!SSPCON2bits.ACKSTAT) {
// If an ACK is received, send first byte of data
SSPBUF = i2c_data_p->buffer_in[0];
i2c_data_p->operating_state = I2C_CHECK_ACK_RESTART;
} else {
// If a NACK is received, stop transmission and send error
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_SEND_FAIL;
}
break;
case I2C_CHECK_ACK_RESTART:
if (!SSPCON2bits.ACKSTAT) {
SSPCON2bits.RSEN = 1;
i2c_data_p->operating_state = I2C_SEND_ADDR_2;
} else {
// If a NACK is received, stop transmission and send error
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_SEND_FAIL;
}
break;
case I2C_SEND_ADDR_2:
// Send the address with read bit set
i2c_data_p->operating_state = I2C_CHECK_ACK_RECV;
SSPBUF = (i2c_data_p->master_dest_addr << 1) | 0x1;
break;
case I2C_CHECK_ACK_RECV:
// Check if ACK is received
if (!SSPCON2bits.ACKSTAT) {
// If an ACK is received, set module to receive 1 byte of data
i2c_data_p->operating_state = I2C_RCV_DATA;
SSPCON2bits.RCEN = 1;
} else {
// If a NACK is received, stop transmission and send error
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_RECV_FAIL;
}
break;
case I2C_RCV_DATA:
// On receive, save byte into buffer
// TODO: handle i2c buffer overflow
i2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = SSPBUF;
i2c_data_p->buffer_in_write_ind++;
if (i2c_data_p->buffer_in_write_ind < i2c_data_p->buffer_in_len) {
// If we still need to read, send an ACK to the slave
i2c_data_p->operating_state = I2C_REQ_DATA;
SSPCON2bits.ACKDT = 0; // ACK
SSPCON2bits.ACKEN = 1;
} else {
// If we are done reading, send an NACK to the slave
i2c_data_p->operating_state = I2C_SEND_STOP;
SSPCON2bits.ACKDT = 1; // NACK
SSPCON2bits.ACKEN = 1;
}
break;
case I2C_REQ_DATA:
// Set module to receive one byte of data
i2c_data_p->operating_state = I2C_RCV_DATA;
SSPCON2bits.RCEN = 1;
break;
case I2C_SEND_STOP:
// Send the stop bit and copy message to send to Main()
i2c_data_p->operating_state = I2C_IDLE;
SSPCON2bits.PEN = 1;
i2c_data_p->master_status = I2C_MASTER_IDLE;
i2c_data_p->return_status = I2C_RECV_OK;
break;
}
}
}
 
void I2C_Interrupt_Slave() {
char received_data;
char data_read_from_buffer = 0;
char data_written_to_buffer = 0;
char overrun_error = 0;
char output[64];
 
// Clear SSPOV (overflow bit)
if (SSPCON1bits.SSPOV == 1) {
sprintf(output, "I2C: (ERROR) overflow detectedr\r\n");
DBG_PRINT_I2C(output, strlen(output));
SSPCON1bits.SSPOV = 0;
// We failed to read the buffer in time, so we know we
// can't properly receive this message, just put us in the
// a state where we are looking for a new message
i2c_data_p->operating_state = I2C_IDLE;
overrun_error = 1;
i2c_data_p->return_status = I2C_ERR_OVERRUN;
}
 
// Read SPPxBUF if it is full
if (SSPSTATbits.BF == 1) {
received_data = SSPBUF;
// DBG_PRINT_I2C("I2C: data read from buffer: %x\r\n", SSPBUF);
data_read_from_buffer = 1;
}
 
if (!overrun_error) {
switch (i2c_data_p->operating_state) {
case I2C_IDLE:
{
// Ignore anything except a start
if (SSPSTATbits.S == 1) {
i2c_data_p->buffer_in_len_tmp = 0;
i2c_data_p->operating_state = I2C_STARTED;
// if (data_read_from_buffer) {
// if (SSPSTATbits.D_A == 1) {
// DBG_PRINT_I2C("I2C Start: (ERROR) no address recieved\r\n");
// // This is bad because we got data and we wanted an address
// i2c_data_p->operating_state = I2C_IDLE;
// i2c_data_p->return_status = I2C_ERR_NOADDR;
// } else {
// // Determine if we are sending or receiving data
// if (SSPSTATbits.R_W == 1) {
// i2c_data_p->operating_state = I2C_SEND_DATA;
// } else {
// i2c_data_p->operating_state = I2C_RCV_DATA;
// }
// }
// } else {
// i2c_data_p->operating_state = I2C_STARTED;
// }
}
break;
}
case I2C_STARTED:
{
// In this case, we expect either an address or a stop bit
if (SSPSTATbits.P == 1) {
// Return to idle mode
i2c_data_p->operating_state = I2C_IDLE;
} else if (data_read_from_buffer) {
if (SSPSTATbits.D_A == 0) {
// Address received
if (SSPSTATbits.R_W == 0) {
// Slave write mode
i2c_data_p->operating_state = I2C_RCV_DATA;
} else {
// Slave read mode
i2c_data_p->operating_state = I2C_SEND_DATA;
// Process the first byte immediatly if sending data
goto send;
}
} else {
sprintf(output, "I2C: (ERROR) no data recieved\r\n");
DBG_PRINT_I2C(output, strlen(output));
i2c_data_p->operating_state = I2C_IDLE;
i2c_data_p->return_status = I2C_ERR_NODATA;
}
}
break;
}
send:
case I2C_SEND_DATA:
{
if (!i2c_data_p->slave_sending_data) {
// If we are not currently sending data, figure out what to reply with
if (I2C_Process_Send(i2c_data_p->slave_in_last_byte)) {
// Data exists to be returned, send first byte
SSPBUF = i2c_data_p->buffer_out[0];
i2c_data_p->buffer_out_ind = 1;
i2c_data_p->slave_sending_data = 1;
data_written_to_buffer = 1;
} else {
// Unknown request
i2c_data_p->slave_sending_data = 0;
i2c_data_p->operating_state = I2C_IDLE;
}
} else {
// Sending remaining data back to master
if (i2c_data_p->buffer_out_ind < i2c_data_p->buffer_out_len) {
SSPBUF = i2c_data_p->buffer_out[i2c_data_p->buffer_out_ind];
i2c_data_p->buffer_out_ind++;
data_written_to_buffer = 1;
} else {
// Nothing left to send
i2c_data_p->slave_sending_data = 0;
i2c_data_p->operating_state = I2C_IDLE;
}
}
break;
}
case I2C_RCV_DATA:
{
// We expect either data or a stop bit or a (if a restart, an addr)
if (SSPSTATbits.P == 1) {
// Stop bit detected, we need to check to see if we also read data
if (data_read_from_buffer) {
if (SSPSTATbits.D_A == 1) {
// Data received with stop bit
// TODO: handle i2c buffer overflow
i2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = received_data;
if (i2c_data_p->buffer_in_write_ind == MAXI2CBUF-1) {
i2c_data_p->buffer_in_write_ind = 0;
} else {
i2c_data_p->buffer_in_write_ind++;
}
i2c_data_p->buffer_in_len_tmp++;
// Save the last byte received
i2c_data_p->slave_in_last_byte = received_data;
i2c_data_p->return_status = I2C_DATA_AVAL;
} else {
sprintf(output, "I2C: (ERROR) no data recieved\r\n");
DBG_PRINT_I2C(output, strlen(output));
i2c_data_p->operating_state = I2C_IDLE;
i2c_data_p->return_status = I2C_ERR_NODATA;
}
}
i2c_data_p->buffer_in_len += i2c_data_p->buffer_in_len_tmp;
i2c_data_p->operating_state = I2C_IDLE;
} else if (data_read_from_buffer) {
if (SSPSTATbits.D_A == 1) {
// Data received
i2c_data_p->buffer_in[i2c_data_p->buffer_in_write_ind] = received_data;
if (i2c_data_p->buffer_in_write_ind == MAXI2CBUF-1) {
i2c_data_p->buffer_in_write_ind = 0;
} else {
i2c_data_p->buffer_in_write_ind++;
}
i2c_data_p->buffer_in_len_tmp++;
// Save the last byte received
i2c_data_p->slave_in_last_byte = received_data;
i2c_data_p->return_status = I2C_DATA_AVAL;
} else {
// Restart bit detected
if (SSPSTATbits.R_W == 1) {
i2c_data_p->buffer_in_len += i2c_data_p->buffer_in_len_tmp;
i2c_data_p->operating_state = I2C_SEND_DATA;
// Process the first byte immediatly if sending data
goto send;
} else {
// Bad to recv an address again, we aren't ready
sprintf(output, "I2C: (ERROR) no data recieved\r\n");
DBG_PRINT_I2C(output, strlen(output));
i2c_data_p->operating_state = I2C_IDLE;
i2c_data_p->return_status = I2C_ERR_NODATA;
}
}
}
break;
}
}
}
 
// Release the clock stretching bit (if we should)
if (data_read_from_buffer || data_written_to_buffer) {
// Release the clock
if (SSPCON1bits.CKP == 0) {
SSPCON1bits.CKP = 1;
}
}
}
 
/* Returns 0 if I2C module is currently busy, otherwise returns status code */
char I2C_Get_Status() {
if (i2c_data_p->operating_mode == I2C_MODE_MASTER) {
if (i2c_data_p->master_status != I2C_MASTER_IDLE || i2c_data_p->buffer_in_len == 0) {
return 0;
} else {
return i2c_data_p->return_status;
}
} else {
if (i2c_data_p->operating_state != I2C_IDLE || i2c_data_p->buffer_in_len == 0) {
return 0;
} else {
return i2c_data_p->return_status;
}
}
}
 
char I2C_Buffer_Len() {
return i2c_data_p->buffer_in_len;
}
 
/* Returns 0 if I2C module is currently busy, otherwise returns buffer length */
char I2C_Read_Buffer(char *buffer) {
char i = 0;
while (i2c_data_p->buffer_in_len != 0) {
buffer[i] = i2c_data_p->buffer_in[i2c_data_p->buffer_in_read_ind];
i++;
if (i2c_data_p->buffer_in_read_ind == MAXI2CBUF-1) {
i2c_data_p->buffer_in_read_ind = 0;
} else {
i2c_data_p->buffer_in_read_ind++;
}
i2c_data_p->buffer_in_len--;
}
return i;
}
 
/* Put data to be returned here */
char I2C_Process_Send(char c) {
char ret = 0;
switch (c) {
case 0xAA:
i2c_data_p->buffer_out[0] = 'A';
i2c_data_p->buffer_out_len = 1;
ret = 1;
break;
case 0xBB:
i2c_data_p->buffer_out[0] = '1';
i2c_data_p->buffer_out[1] = '2';
i2c_data_p->buffer_out_len = 2;
ret = 1;
break;
}
return ret;
}
/PIC Stuff/PICX_27J13/i2c.h
0,0 → 1,81
#ifndef I2C_H
#define I2C_H
 
#define MAXI2CBUF 64
 
// I2C Operating Speed
#define I2C_400KHZ 0x0
#define I2C_100KHZ 0x1
 
// Operating State
#define I2C_IDLE 0x1
#define I2C_STARTED 0x2
#define I2C_RCV_DATA 0x3
#define I2C_SEND_DATA 0x4
#define I2C_SEND_ADDR 0x5
#define I2C_SEND_ADDR_2 0x6
#define I2C_CHECK_ACK_SEND 0x7
#define I2C_CHECK_ACK_RECV 0x8
#define I2C_CHECK_ACK_RESTART 0x9
#define I2C_REQ_DATA 0xA
#define I2C_SEND_STOP 0xB
#define I2C_SEND_START 0xC
 
// Operating Mode
#define I2C_MODE_SLAVE 0x10
#define I2C_MODE_MASTER 0x11
 
// Master Status
#define I2C_MASTER_SEND 0x20
#define I2C_MASTER_RECV 0x21
#define I2C_MASTER_RESTART 0x22
#define I2C_MASTER_IDLE 0x23
 
// Return Status
#define I2C_SEND_OK 0x30
#define I2C_SEND_FAIL 0x31
#define I2C_RECV_OK 0x32
#define I2C_RECV_FAIL 0x33
#define I2C_DATA_AVAL 0x34
#define I2C_ERR_NOADDR 0x35
#define I2C_ERR_OVERRUN 0x36
#define I2C_ERR_NODATA 0x37
#define I2C_ERR_BUFFER_OVERRUN 0x38
 
typedef struct {
char buffer_in[MAXI2CBUF];
char buffer_in_len;
char buffer_in_len_tmp;
char buffer_in_read_ind;
char buffer_in_write_ind;
char buffer_out[MAXI2CBUF];
char buffer_out_len;
char buffer_out_ind;
 
char operating_mode;
char operating_state;
char return_status;
 
char master_dest_addr;
char master_status;
char slave_in_last_byte;
char slave_sending_data;
} I2C_DATA;
 
void I2C_Init(I2C_DATA *data);
void I2C_Interrupt_Handler(void);
void I2C_Interrupt_Slave(void);
void I2C_Interrupt_Master(void);
void I2C_Configure_Slave(char);
void I2C_Configure_Master(char speed);
void I2C_Master_Send(char address, char length, char *msg);
void I2C_Master_Recv(char address, char length);
void I2C_Master_Restart(char address, char msg, char length);
char I2C_Get_Status(void);
char I2C_Buffer_Len(void);
char I2C_Read_Buffer(char *buffer);
char I2C_Process_Send(char);
 
#endif
/PIC Stuff/PICX_27J13/interrupts.c
0,0 → 1,169
#include <xc.h>
#include "defines.h"
#include "interrupts.h"
#include "uart.h"
#include "i2c.h"
#include "timers.h"
 
//----------------------------------------------------------------------------
// Note: This code for processing interrupts is configured to allow for high and
// low priority interrupts. The high priority interrupt can interrupt the
// the processing of a low priority interrupt. However, only one of each type
// can be processed at the same time. It is possible to enable nesting of low
// priority interrupts, but this code is not setup for that and this nesting
// is not enabled.
 
void Interrupt_Init() {
// Peripheral interrupts can have their priority set to high or low
// Decide on the priority of the enabled peripheral interrupts (0 is low, 1 is high)
 
// High priority interrupts
IPR1bits.RC1IP = 1; // USART1 RX interrupt
IPR1bits.TX1IP = 1; // USART1 TX interrupt
// IPR3bits.RC2IP = 1; // USART2 RX interrupt
IPR1bits.SSPIP = 1; // I2C interrupt
// IPR3bits.SSP2IP = 1; // MSSP2 (SPI2) interrupt
 
// Low priority interrupts
// INTCON2bits.TMR0IP = 0; // Timer0 interrupt
IPR1bits.TMR1IP = 0; // Timer1 interrupt
// IPR2bits.TMR3IP = 0; // Timer 3 interrupt
// IPR1bits.ADIP = 0; // ADC interupt
// INTCON2bits.RBIP = 0; // Port B interrupt
// INTCON3bits.INT1IP = 0; // INT1 interrupt
// Enable Port B interrupt
// INTCONbits.RBIE = 1;
// Enable interrupt for INT1
// INTCON3bits.INT1IE = 1;
}
 
void Interrupt_Enable() {
// Peripheral interrupts can have their priority set to high or low.
// Enable both high-priority interrupts and low-priority interrupts
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
}
 
void Interrupt_Disable() {
RCONbits.IPEN = 0;
INTCONbits.GIEH = 0;
INTCONbits.GIEL = 0;
}
 
void interrupt InterruptHandlerHigh(void) {
// We need to check the interrupt flag of each enabled high-priority interrupt to
// see which device generated this interrupt. Then we can call the correct handler.
 
// // Check to see if we have an SPI2 interrupt
// if (PIR3bits.SSP2IF) {
// // Call the handler
// SPI2_Recv_Interrupt_Handler();
//
// // Clear the interrupt flag
// PIR3bits.SSP2IF = 0;
//
// return;
// }
 
// Check to see if we have an I2C interrupt
if (PIR1bits.SSPIF) {
 
// Call the handler
I2C_Interrupt_Handler();
 
// Clear the interrupt flag
PIR1bits.SSPIF = 0;
 
return;
}
 
// Check to see if we have an interrupt on USART1 RX
if (PIR1bits.RC1IF) {
// Call the interrupt handler
UART1_Recv_Interrupt_Handler();
 
// Clear the interrupt flag
PIR1bits.RC1IF = 0;
 
return;
}
 
#ifndef _DEBUG // Disable UART1 TX interrupt for debug mode (using printf)
// Check to see if we have an interrupt on USART1 TX
if (PIR1bits.TX1IF) {
// Call the interrupt handler
UART1_Send_Interrupt_Handler();
 
// Clear the interrupt flag
PIR1bits.TX1IF = 0;
 
return;
}
#endif
 
// // Check to see if we have an interrupt on USART2 RX
// if (PIR3bits.RC2IF) {
// DBG_PRINT_INT("INT: UART2 RX\r\n");
// // Call the interrupt handler
// uart_2_recv_interrupt_handler();
//
// // Clear the interrupt flag
// PIR3bits.RC2IF = 0;
// }
}
 
void interrupt low_priority InterruptHandlerLow() {
// // Check to see if we have an interrupt on INT1
// if (INTCON3bits.INT1IF) {
// DBG_PRINT_INT("INT: INT1\r\n");
// int1_interrupt_handler();
//
// INTCON3bits.INT1IF = 0;
// }
 
// // Check to see if we have an interrupt on any port B inputs <4:7>
// if (INTCONbits.RBIF) {
// DBG_PRINT_INT("INT: Port B\r\n");
// port_b_int_interrupt_handler();
//
// INTCONbits.RBIF = 0;
// }
 
// // Check to see if we have an interrupt on timer 0
// if (INTCONbits.TMR0IF) {
// DBG_PRINT_INT("INT: Timer 0\r\n");
// // Call the handler
// timer0_interrupt_handler();
//
// // Clear this interrupt flag
// INTCONbits.TMR0IF = 0;
// }
 
// Check to see if we have an interrupt on timer 1
if (PIR1bits.TMR1IF) {
// Call the interrupt handler
Timer1_Interrupt_Handler();
 
// Clear the interrupt flag
PIR1bits.TMR1IF = 0;
}
 
// // Check to see if we have an interrupt on timer 3
// if (PIR2bits.TMR3IF) {
// DBG_PRINT_INT("INT: Timer 3\r\n");
// timer3_interrupt_handler();
//
// PIR2bits.TMR3IF = 0;
// }
 
// // Check to see if we have an interrupt on ADC
// if (PIR1bits.ADIF) {
// // Call the interrupt handler
// ADC_Interrupt_Handler();
//
// // Clear the interrupt flag
// PIR1bits.ADIF = 0;
// }
}
/PIC Stuff/PICX_27J13/interrupts.h
0,0 → 1,16
#ifndef INTERRUPTS_H
#define INTERRUPTS_H
 
// Initialize the interrupts
void Interrupt_Init(void);
 
// Enable all interrupts (high and low priority)
void Interrupt_Enable(void);
 
// Disable all interrupts (high and low priority)
void Interrupt_Disable(void);
 
void interrupt InterruptHandlerHigh(void);
void interrupt low_priority InterruptHandlerLow(void);
 
#endif
/PIC Stuff/PICX_27J13/led_HT16K33.c
0,0 → 1,132
#include "led_HT16K33.h"
#include "i2c.h"
 
static const char numbertable[] = {
0x3F /* 0 */,
0x06 /* 1 */,
0x5B /* 2 */,
0x4F /* 3 */,
0x66 /* 4 */,
0x6D /* 5 */,
0x7D, /* 6 */
0x07, /* 7 */
0x7F, /* 8 */
0x6F, /* 9 */
};
 
static const char alphatable[] = {
0x77, /* a */
0x7C, /* b */
0x39, /* C */
0x5E, /* d */
0x79, /* E */
0x71, /* F */
};
 
static LED_DATA *led_data_p;
 
void LED_Init(LED_DATA *data) {
led_data_p = data;
led_data_p->i2c_address = HT16K33_ADDRESS;
}
 
void LED_Start() {
char c = 0x21; // Cmd to turn on oscillator
I2C_Master_Send(led_data_p->i2c_address, 1, &c);
char result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
 
LED_Blink_Rate(HT16K33_BLINK_OFF);
LED_Set_Brightness(15); // Max brightness
LED_Clear();
LED_Write_Display();
}
 
void LED_Set_Brightness(char c) {
if (c > 15) c = 15;
c |= 0xE0;
 
I2C_Master_Send(led_data_p->i2c_address, 1, &c);
char result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
}
 
void LED_Blink_Rate(char c) {
char buffer;
 
if (c > 3) c = 0;
 
buffer = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (c << 1);
 
I2C_Master_Send(led_data_p->i2c_address, 1, &buffer);
buffer = I2C_Get_Status();
while (!buffer) {
buffer = I2C_Get_Status();
}
}
 
void LED_Write_Display() {
led_data_p->display_buffer[0] = 0x00; // Start at address 0x00
I2C_Master_Send(led_data_p->i2c_address, 17, led_data_p->display_buffer);
 
char result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
}
 
void LED_Clear() {
for (char c = 0; c < 17; c++) {
led_data_p->display_buffer[c] = 0;
}
}
 
void LED_Draw_Colon(char c) {
if (c) {
led_data_p->display_buffer[5] = 0xFF;
} else {
led_data_p->display_buffer[5] = 0;
}
}
 
void LED_Write_Digit_Raw(char loc, char bitmask) {
if (loc > 4) return;
led_data_p->display_buffer[(loc<<1)+1] = bitmask;
}
 
void LED_Write_Digit_Num(char loc, char num, char dot) {
if (loc > 4) return;
if (loc > 1) loc++;
LED_Write_Digit_Raw(loc, numbertable[num] | dot << 7);
}
 
void LED_Write_Digit_Alpha(char loc, char alpha, char dot) {
if (loc > 4) return;
if (loc > 1) loc++;
LED_Write_Digit_Raw(loc, alphatable[alpha] | dot << 7);
}
 
void LED_Write_Num(int i) {
LED_Write_Digit_Num(0, (i%10000)/1000, 0);
LED_Write_Digit_Num(1, (i%1000)/100, 0);
LED_Write_Digit_Num(2, (i%100)/10, 0);
LED_Write_Digit_Num(3, i%10, 0);
 
if (i < 10) {
LED_Write_Digit_Raw(0, 0);
LED_Write_Digit_Raw(1, 0);
LED_Write_Digit_Raw(3, 0);
} else if (i < 100) {
LED_Write_Digit_Raw(0, 0);
LED_Write_Digit_Raw(1, 0);
} else if (i < 1000) {
LED_Write_Digit_Raw(0, 0);
}
LED_Write_Display();
}
/PIC Stuff/PICX_27J13/led_HT16K33.h
0,0 → 1,34
#ifndef LED_BACKPACK_H
#define LED_BACKPACK_H
 
#define HT16K33_ADDRESS 0x70
 
#define HT16K33_BLINK_CMD 0x80
#define HT16K33_BLINK_DISPLAYON 0x01
#define HT16K33_BLINK_OFF 0
#define HT16K33_BLINK_2HZ 1
#define HT16K33_BLINK_1HZ 2
#define HT16K33_BLINK_HALFHZ 3
 
#define HT16K33_CMD_BRIGHTNESS 0x0E
 
typedef struct {
char i2c_address;
char display_buffer[17];
} LED_DATA;
 
void LED_Init(LED_DATA *data);
void LED_Start(void);
void LED_Set_Brightness(char c);
void LED_Blink_Rate(char c);
void LED_Write_Display(void);
void LED_Clear(void);
void LED_Draw_Colon(char c);
void LED_Write_Digit_Raw(char loc, char bitmask);
void LED_Write_Digit_Num(char loc, char num, char dot);
void LED_Write_Digit_Alpha(char loc, char alpha, char dot);
void LED_Write_Num(int i);
 
 
#endif /* LED_BACKPACK_H */
 
/PIC Stuff/PICX_27J13/lux_TSL2561.c
0,0 → 1,233
#include "lux_TSL2561.h"
#include "defines.h"
#include "i2c.h"
#include <delays.h>
 
static TSL2561_DATA *tsl2561_data_p;
 
void LUX_Init(TSL2561_DATA *data, char address) {
tsl2561_data_p = data;
tsl2561_data_p->address = address;
tsl2561_data_p->integration = TSL2561_INTEGRATIONTIME_13MS;
tsl2561_data_p->gain = TSL2561_GAIN_16X;
}
 
void LUX_Begin(void) {
char result, buffer[2];
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);
char length = I2C_Read_Buffer(buffer);
DBG_PRINT_LUX("Received %d bytes: ", length);
for (char i = 0; i < length; i++) {
DBG_PRINT_LUX("%c ", buffer[i]);
}
DBG_PRINT_LUX("\r\n");
 
// Set default integration time and gain
LUX_Set_Timing(tsl2561_data_p->integration);
LUX_Set_Gain(tsl2561_data_p->gain);
 
// Start the chip in power-down mode
LUX_Disable();
}
 
void LUX_Enable() {
LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);
}
 
void LUX_Disable() {
LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF);
}
 
void LUX_Set_Gain(tsl2561Gain_t gain) {
LUX_Enable();
tsl2561_data_p->gain = gain;
LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
tsl2561_data_p->integration | tsl2561_data_p->gain);
LUX_Disable();
}
 
void LUX_Set_Timing(tsl2561IntegrationTime_t integration) {
LUX_Enable();
tsl2561_data_p->integration = integration;
LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
tsl2561_data_p->integration | tsl2561_data_p->gain);
LUX_Disable();
}
 
unsigned long LUX_Calculate_Lux(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)) {
if ((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));
// TODO: change this back once they fix compiler
temp = (channel0 * b);
temp -= (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_Get_Full_Luminosity() {
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_Read_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
x <<= 16;
x |= LUX_Read_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
 
LUX_Disable();
 
return x;
}
 
unsigned int LUX_Get_Luminosity(char channel) {
unsigned long x = LUX_Get_Full_Luminosity();
 
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_Write_2_Bytes(char reg, char value) {
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_Read_2_Bytes(char reg) {
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(buffer);
ret = buffer[1] << 8;
ret |= buffer[0];
 
return ret;
}
/PIC Stuff/PICX_27J13/lux_TSL2561.h
0,0 → 1,132
#ifndef LUX_TSL2561_H
#define LUX_TSL2561_H
 
#define TSL2561_VISIBLE 2 // channel 0 - channel 1
#define TSL2561_INFRARED 1 // channel 1
#define TSL2561_FULLSPECTRUM 0 // channel 0
 
// 3 i2c address options!
#define TSL2561_ADDR_LOW 0x29
#define TSL2561_ADDR_FLOAT 0x39
#define TSL2561_ADDR_HIGH 0x49
 
// Lux calculations differ slightly for CS package
//#define TSL2561_PACKAGE_CS
#define TSL2561_PACKAGE_T_FN_CL
 
#define TSL2561_READBIT (0x01)
 
#define TSL2561_COMMAND_BIT (0x80) // Must be 1
#define TSL2561_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear)
#define TSL2561_WORD_BIT (0x20) // 1 = read/write word (rather than byte)
#define TSL2561_BLOCK_BIT (0x10) // 1 = using block read/write
 
#define TSL2561_CONTROL_POWERON (0x03)
#define TSL2561_CONTROL_POWEROFF (0x00)
 
#define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14
#define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9
#define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10
#define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE
#define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE
 
// T, FN and CL package values
#define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE
#define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE
#define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE
#define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE
#define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE
#define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE
#define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE
#define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE
#define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE
#define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE
#define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE
#define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE
#define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE
#define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE
#define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE
#define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE
#define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE
#define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE
#define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE
#define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE
#define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE
#define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE
#define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE
#define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE
 
// CS package values
#define TSL2561_LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE
#define TSL2561_LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE
#define TSL2561_LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE
#define TSL2561_LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE
#define TSL2561_LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE
#define TSL2561_LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE
#define TSL2561_LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE
#define TSL2561_LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE
#define TSL2561_LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE
#define TSL2561_LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE
#define TSL2561_LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE
#define TSL2561_LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE
#define TSL2561_LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE
#define TSL2561_LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE
#define TSL2561_LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE
#define TSL2561_LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE
#define TSL2561_LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE
#define TSL2561_LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE
#define TSL2561_LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE
#define TSL2561_LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE
#define TSL2561_LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE
#define TSL2561_LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE
#define TSL2561_LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE
#define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE
 
enum {
TSL2561_REGISTER_CONTROL = 0x00,
TSL2561_REGISTER_TIMING = 0x01,
TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02,
TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03,
TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04,
TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05,
TSL2561_REGISTER_INTERRUPT = 0x06,
TSL2561_REGISTER_CRC = 0x08,
TSL2561_REGISTER_ID = 0x0A,
TSL2561_REGISTER_CHAN0_LOW = 0x0C,
TSL2561_REGISTER_CHAN0_HIGH = 0x0D,
TSL2561_REGISTER_CHAN1_LOW = 0x0E,
TSL2561_REGISTER_CHAN1_HIGH = 0x0F
};
 
typedef enum {
TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms
TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms
TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms
} tsl2561IntegrationTime_t;
 
typedef enum {
TSL2561_GAIN_0X = 0x00, // No gain
TSL2561_GAIN_16X = 0x10 // 16x gain
} tsl2561Gain_t;
 
typedef struct __TSL25611_DATA {
char address;
tsl2561IntegrationTime_t integration;
tsl2561Gain_t gain;
} TSL2561_DATA;
 
void LUX_Init(TSL2561_DATA *data, char address);
void LUX_Begin(void);
void LUX_Enable(void);
void LUX_Disable(void);
void LUX_Set_Timing(tsl2561IntegrationTime_t integration);
void LUX_Set_Gain(tsl2561Gain_t gain);
unsigned long LUX_Calculate_Lux(unsigned int ch0, unsigned int ch1);
unsigned long LUX_Get_Full_Luminosity(void);
unsigned int LUX_Get_Luminosity(char channel);
 
void LUX_Write_2_Bytes(char reg, char value);
unsigned int LUX_Read_2_Bytes(char reg);
 
#endif /* LUX_TSL2561_H */
 
/PIC Stuff/PICX_27J13/main.c
1,8 → 1,21
#include <xc.h>
#include <delays.h>
#include <stdio.h>
#include <string.h>
#include "defines.h"
#include "interrupts.h"
#include "uart.h"
#include "i2c.h"
#include "spi.h"
#include "nfc_PN532.h"
#include "led_HT16K33.h"
#include "oled_ssd1306.h"
#include "oled_ssd1331.h"
#include "timers.h"
#include "lux_TSL2561.h"
#include "oled_NHD-0216KZW-AB5.h"
#include "adc.h"
#include "temp_BMP085.h"
 
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
/* --------------------------- Configuration Bits --------------------------- */
51,11 → 64,1006
#if defined(_TEST_UART)
int main() {
char buffer[100];
buffer[0] = 1;
unsigned char length = 12345;
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
char output[] = "\r\nBegin Program\r\n";
DBG_PRINT_MAIN(output, strlen(output));
 
while (1) {
char length = UART1_Read_Buffer((char *) buffer);
if (length != 0) {
UART1_WriteS(buffer, length);
}
 
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#elif defined(_TEST_I2C_MASTER)
void main(void) {
char length = 0;
char result = 0;
char buffer[100];
char output[64];
 
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
I2C_DATA i2c_data;
I2C_Init(&i2c_data); // Initialize the I2C handler code
 
I2C_Configure_Master(I2C_100KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
while (1) {
buffer[0] = 0x8;
 
I2C_Master_Send(0x24, 1, buffer);
do {
result = I2C_Get_Status();
} while (!result);
sprintf(output, "S: %X ", result);
DBG_PRINT_MAIN(output, strlen(output));
 
I2C_Master_Recv(0x24, 2);
do {
result = I2C_Get_Status();
} while (!result);
sprintf(output, "S: %X ", result);
DBG_PRINT_MAIN(output, strlen(output));
length = I2C_Read_Buffer(buffer);
sprintf(output, "L: %d D: ", length);
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < length; i++) {
sprintf(output, "%c ", buffer[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
I2C_Master_Restart(0x30, 0xBB, 2);
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
sprintf(output, "S: %X ", result);
DBG_PRINT_MAIN(output, strlen(output));
length = I2C_Read_Buffer(buffer);
sprintf(output, "L: %d D: ", length);
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < length; i++) {
sprintf(output, "%c ", buffer[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#elif defined(_TEST_I2C_SLAVE)
void main(void) {
char length = 0;
char result = 0;
char buffer[100];
char output[64];
 
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
I2C_DATA i2c_data;
I2C_Init(&i2c_data); // Initialize the I2C handler code
 
I2C_Configure_Slave(0x24);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
while (1) {
 
result = I2C_Get_Status();
while (!result) {
result = I2C_Get_Status();
}
sprintf(output, "S: %X ", result);
DBG_PRINT_MAIN(output, strlen(output));
length = I2C_Read_Buffer(buffer);
sprintf(output, "L: %d D: ", length);
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < length; i++) {
sprintf(output, "%X ", buffer[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#elif defined(_TEST_SPI)
void main(void) {
char length = 0;
char result = 0;
char buffer[100];
char output[64];
char test[8] = "ASDF123";
 
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
SPI_DATA spi_data;
SPI2_Init(&spi_data, SPI2_FOSC_8); // Initialize the SPI module
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
while (1) {
 
SPI2_Write(test, 7);
while (result != 7) {
length = SPI2_Read_Buffer(buffer);
if (length) {
result += length;
}
}
result = 0;
 
for (char i = 0; i < result; i++) {
sprintf(output, "%X ", buffer[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#elif defined(_TEST_NFC)
void main(void) {
char length = 0;
char output[64];
 
// NFC stuff
NFC_FIRMWARE_VERSION version;
NFC_TargetDataMiFare cardData[2];
NFC_TargetDataMiFare cardData_prev[2];
 
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
I2C_DATA i2c_data;
I2C_Init(&i2c_data); // Initialize the I2C handler code
NFC_DATA nfc_data;
NFC_Init(&nfc_data); // Initialize the NFC chip (uses I2C)
 
I2C_Configure_Master(I2C_400KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
version = NFC_Get_Firmware_Version();
while (!version.IC) {
sprintf(output, "Waiting for NFC board..\r\n");
DBG_PRINT_MAIN(output, strlen(output));
Delay10KTCYx(3);
version = NFC_Get_Firmware_Version();
}
sprintf(output, "Found chip PN5%X\r\n", version.IC);
DBG_PRINT_MAIN(output, strlen(output));
sprintf(output, "Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
DBG_PRINT_MAIN(output, strlen(output));
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_Poll_Targets(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 {
sprintf(output, "UID: ");
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
sprintf(output, "%02X ", cardData[0].NFCID[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
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
sprintf(output, "UID2: ");
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < cardData[1].NFCID_LEN; i++) {
sprintf(output, "%02X ", cardData[1].NFCID[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
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
sprintf(output, "UID1: ");
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
sprintf(output, "%02X ", cardData[0].NFCID[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
} else {
// No match
sprintf(output, "UID1: ");
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
sprintf(output, "%02X ", cardData[0].NFCID[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
sprintf(output, "UID2: ");
DBG_PRINT_MAIN(output, strlen(output));
for (char i = 0; i < cardData[1].NFCID_LEN; i++) {
sprintf(output, "%02X ", cardData[1].NFCID[i]);
DBG_PRINT_MAIN(output, strlen(output));
}
sprintf(output, "\r\n");
DBG_PRINT_MAIN(output, strlen(output));
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
}
}
}
}
#elif defined(_TEST_LED_BACKPACK)
void main(void) {
unsigned int counter = 0;
char output[64];
 
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
I2C_DATA i2c_data;
I2C_Init(&i2c_data); // Initialize the I2C handler code
LED_DATA led_data;
LED_Init(&led_data); // Initialize the LED backpack (uses I2C);
 
I2C_Configure_Master(I2C_400KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
LED_Start();
LED_Write_Digit_Num(0, 1, 1);
LED_Write_Digit_Num(1, 2, 0);
LED_Write_Digit_Num(2, 3, 0);
LED_Write_Digit_Num(3, 4, 0);
LED_Write_Display();
for (char i = 0; i < 15; i++) {
LED_Set_Brightness(15 - i);
Delay10KTCYx(100);
}
for (char i = 0; i < 15; i++) {
LED_Set_Brightness(i);
Delay10KTCYx(100);
}
LED_Blink_Rate(HT16K33_BLINK_OFF);
 
while (1) {
LED_Write_Num(counter);
counter++;
if (counter > 9999)
counter = 0;
 
// Delay10KTCYx(255);
}
}
#elif defined(_TEST_SSD1306_OLED)
void main(void) {
char output[64];
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
SPI_DATA spi_data;
SPI2_Init(&spi_data, SPI2_FOSC_4); // Initialize the SPI module
SSD1306_DATA ssd1306_data;
SSD1306_Init(&ssd1306_data); // Initialize the OLED code
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
 
SSD1306_Display(); // Show splashscreen
 
while (1) {
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();
SSD1306_Fill_Circle(SSD1306_LCDWIDTH / 2, SSD1306_LCDHEIGHT / 2, 10, SSD1306_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();
SSD1306_Set_Text_Size(1);
SSD1306_Set_Text_Color(SSD1306_WHITE);
SSD1306_Set_Cursor(0, 0);
sprintf(output, "Hello World!\n");
SSD1306_Write_String(output, strlen(output));
// SSD1306_Set_Text_Color_BG(BLACK, WHITE);
unsigned int i = 65535;
sprintf(output, "%u %d\n", i, i);
SSD1306_Write_String(output, strlen(output));
// SSD1306_Set_Text_Size(2);
// SSD1306_Set_Text_Color(WHITE);
unsigned long l = 0xDEADBEEF;
sprintf(output, "0x%lX", (long) l);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
 
// SSD1306_Clear_Display();
// SSD1306_Set_Rotation(0);
// SSD1306_Set_Text_Size(1);
// SSD1306_Set_Text_Color(SSD1306_WHITE);
// SSD1306_Set_Cursor(0, 0);
// SSD1306_Write_String("%u", i);
// i++;
// SSD1306_Display();
 
}
}
#elif defined(_TEST_SSD1331_OLED)
void main(void) {
char output[128];
// Set all ports as digial I/O
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
SPI_DATA spi_data;
SPI2_Init(&spi_data, SPI2_FOSC_64); // Initialize the SPI module
SSD1331_DATA ssd1331_data;
SSD1331_Init(&ssd1331_data); // Initialize the OLED code
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
SSD1331_Begin();
 
while (1) {
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Test_Pattern();
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Clear_Display();
SSD1331_Set_Rotation(0);
SSD1331_Set_Cursor(0, 0);
// TODO: Figure out why this isnt working (probably a compiler issue)
sprintf(output, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabit adipiscing ante sed nibh tincidunt feugiat.");
DBG_PRINT_MAIN(output, strlen(output));
 
// Delay10KTCYx(255);
// Delay10KTCYx(255);
// SSD1331_Clear_Display();
// SSD1331_Set_Rotation(3);
// SSD1331_Set_Cursor(0, 0);
// SSD1331_Write_String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Test_DrawLines(SSD1331_YELLOW);
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(3);
SSD1331_Test_DrawLines(SSD1331_BLUE);
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Test_DrawRect(SSD1331_GREEN);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(1);
SSD1331_Test_DrawRect(SSD1331_RED);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(2);
SSD1331_Test_DrawRect(SSD1331_BLUE);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(3);
SSD1331_Test_DrawRect(SSD1331_YELLOW);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(3);
SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Clear_Display();
SSD1331_Test_FillCircle(10, SSD1331_BLUE);
SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(3);
SSD1331_Clear_Display();
SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Test_DrawTria();
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(3);
SSD1331_Test_DrawTria();
 
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(0);
SSD1331_Test_DrawRoundRect();
Delay10KTCYx(255);
Delay10KTCYx(255);
SSD1331_Set_Rotation(3);
SSD1331_Test_DrawRoundRect();
 
// SSD1331_Clear_Display();
// SSD1331_Set_Rotation(3);
// SSD1331_Set_Cursor(0,0);
// SSD1331_Set_Text_Color_BG(SSD1331_WHITE, SSD1331_BLACK);
// SSD1331_Write_String("%u", i);
// i++;
}
}
#elif defined(_TEST_TIMER1_RTC)
void main(void) {
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
Timer1_Init();
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
LED_BLUE_TRIS = 0;
LED_RED_TRIS = 0;
 
Timer1_Enable();
 
while (1) {
 
}
}
#elif defined(_TEST_LUX)
void main(void) {
char output[64];
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data);
I2C_DATA i2c_data;
I2C_Init(&i2c_data);
TSL2561_DATA lux_data;
LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
 
I2C_Configure_Master(I2C_100KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
LUX_Begin();
 
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
// LUX_Set_Gain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
LUX_Set_Gain(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_Set_Timing(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
LUX_Set_Timing(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
// LUX_Set_Timing(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
while (1) {
unsigned long lum = LUX_Get_Full_Luminosity();
unsigned int ir = lum >> 16;
unsigned int full = lum & 0xFFFF;
sprintf(output, "IR: %d\r\n", ir);
DBG_PRINT_MAIN(output, strlen(output));
sprintf(output, "Visible: %d\r\n", full - ir);
DBG_PRINT_MAIN(output, strlen(output));
sprintf(output, "Full: %d\r\n", full);
DBG_PRINT_MAIN(output, strlen(output));
sprintf(output, "Lux: %ld\r\n\r\n", LUX_Calculate_Lux(full, ir));
DBG_PRINT_MAIN(output, strlen(output));
 
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#elif defined(_TEST_OLED_CHAR)
void main(void) {
char output[64];
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xFF;
ANCON1 = 0x1F;
 
// UART1_Init();
OLED_CHAR_DATA oled_data;
NHD_Init(&oled_data);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
NHD_Begin(16, 2);
 
sprintf(output, "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do");
NHD_Write_String(output, strlen(output));
NHD_Set_Cursor(0,1);
sprintf(output, "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e");
NHD_Write_String(output, strlen(output));
 
while (1) {
Delay10KTCYx(150);
NHD_Scroll_Display_Left();
}
}
#elif defined(_TEST_NFC_TO_SSD1306_OLED)
void main(void) {
char output[64];
 
// NFC stuff
NFC_FIRMWARE_VERSION version;
NFC_TargetDataMiFare cardData[2];
NFC_TargetDataMiFare cardData_prev[2];
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data);
I2C_DATA i2c_data;
I2C_Init(&i2c_data);
NFC_DATA nfc_data;
NFC_Init(&nfc_data);
SPI_DATA spi_data;
SPI2_Init(&spi_data, SPI2_FOSC_4);
SSD1306_DATA ssd1306_data;
SSD1306_Init(&ssd1306_data);
 
I2C_Configure_Master(I2C_400KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(output, "\r\nBegin Program\r\n");
DBG_PRINT_MAIN(output, strlen(output));
 
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
memset(cardData, 0, 24);
memset(cardData_prev, 0, 24);
SSD1306_Clear_Display();
SSD1306_Set_Rotation(0);
SSD1306_Set_Cursor(0, 0);
 
version = NFC_Get_Firmware_Version();
while (!version.IC) {
sprintf(output, "Waiting for NFC board..\n");
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
Delay10KTCYx(255);
version = NFC_Get_Firmware_Version();
}
sprintf(output, "PN5%X Ver. %d.%d\n", version.IC, version.Ver, version.Rev);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
NFC_SAMConfig();
 
while (1) {
 
// This query will not wait for a detection before responding
char length = NFC_Poll_Targets(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 {
sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
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
sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
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
sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
} else {
// No match
sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
SSD1306_Write_String(output, strlen(output));
SSD1306_Display();
memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
}
}
}
}
#elif defined(_TEST_LUX_TO_CHAR_OLED)
void main(void) {
char output[64];
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
I2C_DATA i2c_data;
I2C_Init(&i2c_data);
OLED_CHAR_DATA oled_data;
NHD_Init(&oled_data);
TSL2561_DATA lux_data;
LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
 
I2C_Configure_Master(I2C_400KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
NHD_Begin(16, 2);
 
// 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_Set_Gain(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_Set_Timing(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light)
// LUX_SetTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light)
 
while (1) {
unsigned long lum = LUX_Get_Full_Luminosity();
unsigned int ir = lum >> 16;
unsigned int full = lum & 0xFFFF;
NHD_Set_Cursor(0, 0);
sprintf(output, "I: %d ", ir);
NHD_Write_String(output, strlen(output));
sprintf(output, "V: %d ", full - ir);
NHD_Write_String(output, strlen(output));
NHD_Set_Cursor(0, 1);
sprintf(output, "Lux: %ld ", LUX_Calculate_Lux(full, ir));
NHD_Write_String(output, strlen(output));
 
Delay10KTCYx(100);
}
}
#elif defined(_TEST_ADC)
void main(void) {
unsigned int x, y, z;
char buffer[60];
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data); // Initialize the UART handler code
SPI_DATA spi_data;
SPI2_Init(&spi_data, SPI2_FOSC_8); // Initialize the SPI module
SSD1306_DATA ssd1306_data;
SSD1306_Init(&ssd1306_data); // Initialize the SSD1331 OLED display (uses SPI2)
ADC_DATA adc_data;
ADC_Init(&adc_data, ADC_TAD_20, ADC_FOSC_64_);
 
SSD1306_Begin(SSD1306_SWITCHCAPVCC);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
sprintf(buffer, "\r\nBegin Program\r\n");
SSD1306_Write_String(buffer, strlen(buffer));
 
memset(buffer, 0, 60);
SSD1306_Clear_Display();
SSD1306_Display();
 
while (1) {
// ADC read from AN0-AN2 and prints to display
ADC_Start(ADC_CHANNEL_AN2);
// SSD1306_Fill_Rect(0, 0, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
SSD1306_Set_Cursor(0, 0);
while (!ADC_Get_Result(&x));
sprintf(buffer, "X: %u", x);
SSD1306_Write_String(buffer, strlen(buffer));
SSD1306_Display();
 
ADC_Start(ADC_CHANNEL_AN1);
// SSD1306_Fill_Rect(0, 8, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
SSD1306_Set_Cursor(0, 8);
while (!ADC_Get_Result(&y));
sprintf(buffer, "Y: %u", y);
SSD1306_Write_String(buffer, strlen(buffer));
SSD1306_Display();
 
ADC_Start(ADC_CHANNEL_AN0);
// SSD1306_Fill_Rect(0, 16, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
SSD1306_Set_Cursor(0, 16);
while (!ADC_Get_Result(&z));
sprintf(buffer, "Z: %u", z);
SSD1306_Write_String(buffer, strlen(buffer));
SSD1306_Display();
}
}
#elif defined(_TEST_BMP)
void main(void) {
char output[64];
 
// Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
ANCON0 = 0xF8;
ANCON1 = 0x1F;
 
UART_DATA uart_data;
UART1_Init(&uart_data);
I2C_DATA i2c_data;
I2C_Init(&i2c_data);
BMP085_DATA bmp_data;
BMP_Init(&bmp_data);
 
I2C_Configure_Master(I2C_100KHZ);
 
Interrupt_Init(); // Initialize the interrupt priorities
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
 
BMP_Begin(BMP085_ULTRAHIGHRES);
 
while (1) {
sprintf(output, "Temp: %f *C\r\n", BMP_Read_Temperature());
DBG_PRINT_MAIN(output, strlen(output));
sprintf(output, "Pressure: %ld Pa\r\n", BMP_Read_Pressure());
DBG_PRINT_MAIN(output, strlen(output));
sprintf(output, "Altitude: %f meters\r\n", BMP_Read_Altitude(101592));
DBG_PRINT_MAIN(output, strlen(output));
 
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
Delay10KTCYx(255);
}
}
#elif defined(_TEST_XBEE)
void main(void) {
unsigned int i, length = 0;
unsigned char buffer[100];
float f = 3.1415;
 
XBEE_RX_AT_COMMAND_RESPONSE_FRAME *rx_at_cmd_response_frame;
XBEE_RX_DATA_PACKET_FRAME *rx_data_frame;
XBEE_RX_DATA_TX_STATUS_FRAME *rx_tx_status_frame;
XBEE_RX_REMOTE_AT_COMMAND_FRAME *rx_remote_at_cmd_frame;
XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *rx_node_ident_frame;
XBEE_RX_MODEM_STATUS_FRAME *rx_modem_status_frame;
 
/* --------------------- Oscillator Configuration --------------------- */
// OSCTUNEbits.PLLEN = 1; // Enable 4x PLL
OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
67,75 → 1075,142
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
Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
Interrupt_Init(); // Initialize the interrupt priorities
 
// DBG_PRINT_MAIN("\r\nBegin Program\r\n");
// DBG_PRINT_MAIN("%d\r\n", length);
// printf("asf\r\n");
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);
//#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");
rx_at_cmd_response_frame = (void *) buffer;
DBG_PRINT_MAIN("Frame ID: %u\r\n", rx_at_cmd_response_frame->frame_id);
DBG_PRINT_MAIN("AT Command: %c%c Status: %02X\r\n", rx_at_cmd_response_frame->command[0], \\
rx_at_cmd_response_frame->command[1], rx_at_cmd_response_frame->command_status);
if (length > XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE) {
DBG_PRINT_MAIN("Command Data: ");
for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
DBG_PRINT_MAIN("%02X ", rx_at_cmd_response_frame->data[i]);
}
DBG_PRINT_MAIN("\r\n");
}
break;
case XBEE_RX_DATA_PACKET:
DBG_PRINT_MAIN("XBEE: parsing recieved data recieved frame\r\n");
rx_data_frame = (void *) buffer;
XBee_Convert_Endian_64(&(rx_data_frame->source_64));
XBee_Convert_Endian_16(&(rx_data_frame->source_16));
DBG_PRINT_MAIN("Source 64: %08lX %08lX Source 16: %04X Options: %02X\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");
rx_tx_status_frame = (void *) buffer;
XBee_Convert_Endian_16(&(rx_tx_status_frame->destination_16));
DBG_PRINT_MAIN("Frame ID: %u Destination 16: %04X\r\n", \\
rx_tx_status_frame->frame_id, rx_tx_status_frame->destination_16.INT_16.int_value);
DBG_PRINT_MAIN("Transmit Retry Count: %02X Delivery Status: %02X Discovery Status: %02X\r\n", \\
rx_tx_status_frame->transmit_retry_count, rx_tx_status_frame->delivery_status, \\
rx_tx_status_frame->discovery_status);
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");
rx_remote_at_cmd_frame = (void *) buffer;
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");
rx_node_ident_frame = (void *) buffer;
XBee_Convert_Endian_64(&(rx_node_ident_frame->source_64));
XBee_Convert_Endian_16(&(rx_node_ident_frame->source_16));
XBee_Convert_Endian_64(&(rx_node_ident_frame->remote_64));
XBee_Convert_Endian_16(&(rx_node_ident_frame->remote_16));
XBee_Convert_Endian_16(&(rx_node_ident_frame->parent_16));
DBG_PRINT_MAIN("Source 64: %08lX %08lX Source 16: %04X Options: %02X\r\n", \\
rx_node_ident_frame->source_64.UPPER_32.long_value, \\
rx_node_ident_frame->source_64.LOWER_32.long_value, \\
rx_node_ident_frame->source_16.INT_16.int_value, \\
rx_node_ident_frame->recieve_options);
DBG_PRINT_MAIN("Remote 64: %08lX %08lX Remote 16: %04X Parent 16: %04X\r\n", \\
rx_node_ident_frame->remote_64.UPPER_32.long_value, \\
rx_node_ident_frame->remote_64.LOWER_32.long_value, \\
rx_node_ident_frame->remote_16.INT_16.int_value, \\
rx_node_ident_frame->parent_16.INT_16.int_value);
DBG_PRINT_MAIN("Device Type: %02X Source Event: %02X\r\n", \\
rx_node_ident_frame->device_type, rx_node_ident_frame->source_event);
break;
case XBEE_RX_FRAME_MODEM_STATUS:
DBG_PRINT_MAIN("XBEE: parsing recieved modem status frame\r\n");
rx_modem_status_frame = (void *) buffer;
DBG_PRINT_MAIN("Status: %02X\r\n", rx_modem_status_frame->status);
break;
default:
DBG_PRINT_MAIN("??\r\n");
break;
}
}
#endif
 
}
}
#elif defined(_TEST_I2C_MASTER)
int main() {
char buffer[100];
buffer[0] = 1;
}
#else
int main() {
char buffer[255];
char buffer1[255];
char buffer2[255];
char buffer3[255];
char buffer4[255];
char buffer5[255];
char buffer6[255];
char buffer7[255];
char buffer8[255];
char buffer9[255];
char buffer10[255];
char buffer11[255];
char buffer12[100];
char buffer13[100];
char buffer14[50];
char buffer15[50];
char buffer16[50];
char buffer17[40];
char buffer18[20];
buffer[0] = 1;
buffer1[0] = 1;
buffer2[0] = 1;
buffer3[0] = 1;
buffer4[0] = 1;
buffer5[0] = 1;
buffer6[0] = 1;
buffer7[0] = 1;
buffer8[0] = 1;
buffer9[0] = 1;
buffer10[0] = 1;
buffer11[0] = 1;
buffer12[0] = 1;
buffer13[0] = 1;
buffer14[0] = 1;
buffer15[0] = 1;
buffer16[0] = 1;
buffer17[0] = 1;
buffer18[0] = 1;
 
}
#endif
 
void putch(char data) {
while (!TXIF)
continue;
TXREG = data;
}
/PIC Stuff/PICX_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.p1 ${OBJECTDIR}/uart.p1
POSSIBLE_DEPFILES=${OBJECTDIR}/main.p1.d ${OBJECTDIR}/uart.p1.d
OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/main.p1 ${OBJECTDIR}/uart.p1 ${OBJECTDIR}/i2c.p1 ${OBJECTDIR}/spi.p1 ${OBJECTDIR}/interrupts.p1 ${OBJECTDIR}/led_HT16K33.p1 ${OBJECTDIR}/nfc_PN532.p1 ${OBJECTDIR}/oled_ssd1306.p1 ${OBJECTDIR}/oled_ssd1331.p1 ${OBJECTDIR}/timers.p1 ${OBJECTDIR}/lux_TSL2561.p1 ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1 ${OBJECTDIR}/adc.p1 ${OBJECTDIR}/temp_BMP085.p1
POSSIBLE_DEPFILES=${OBJECTDIR}/main.p1.d ${OBJECTDIR}/uart.p1.d ${OBJECTDIR}/i2c.p1.d ${OBJECTDIR}/spi.p1.d ${OBJECTDIR}/interrupts.p1.d ${OBJECTDIR}/led_HT16K33.p1.d ${OBJECTDIR}/nfc_PN532.p1.d ${OBJECTDIR}/oled_ssd1306.p1.d ${OBJECTDIR}/oled_ssd1331.p1.d ${OBJECTDIR}/timers.p1.d ${OBJECTDIR}/lux_TSL2561.p1.d ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d ${OBJECTDIR}/adc.p1.d ${OBJECTDIR}/temp_BMP085.p1.d
 
# Object Files
OBJECTFILES=${OBJECTDIR}/main.p1 ${OBJECTDIR}/uart.p1
OBJECTFILES=${OBJECTDIR}/main.p1 ${OBJECTDIR}/uart.p1 ${OBJECTDIR}/i2c.p1 ${OBJECTDIR}/spi.p1 ${OBJECTDIR}/interrupts.p1 ${OBJECTDIR}/led_HT16K33.p1 ${OBJECTDIR}/nfc_PN532.p1 ${OBJECTDIR}/oled_ssd1306.p1 ${OBJECTDIR}/oled_ssd1331.p1 ${OBJECTDIR}/timers.p1 ${OBJECTDIR}/lux_TSL2561.p1 ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1 ${OBJECTDIR}/adc.p1 ${OBJECTDIR}/temp_BMP085.p1
 
 
CFLAGS=
86,6 → 86,90
@-${MV} ${OBJECTDIR}/uart.d ${OBJECTDIR}/uart.p1.d
@${FIXDEPS} ${OBJECTDIR}/uart.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/i2c.p1: i2c.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/i2c.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/i2c.p1 i2c.c
@-${MV} ${OBJECTDIR}/i2c.d ${OBJECTDIR}/i2c.p1.d
@${FIXDEPS} ${OBJECTDIR}/i2c.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/spi.p1: spi.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/spi.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/spi.p1 spi.c
@-${MV} ${OBJECTDIR}/spi.d ${OBJECTDIR}/spi.p1.d
@${FIXDEPS} ${OBJECTDIR}/spi.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/interrupts.p1: interrupts.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/interrupts.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/interrupts.p1 interrupts.c
@-${MV} ${OBJECTDIR}/interrupts.d ${OBJECTDIR}/interrupts.p1.d
@${FIXDEPS} ${OBJECTDIR}/interrupts.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/led_HT16K33.p1: led_HT16K33.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/led_HT16K33.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/led_HT16K33.p1 led_HT16K33.c
@-${MV} ${OBJECTDIR}/led_HT16K33.d ${OBJECTDIR}/led_HT16K33.p1.d
@${FIXDEPS} ${OBJECTDIR}/led_HT16K33.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/nfc_PN532.p1: nfc_PN532.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/nfc_PN532.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/nfc_PN532.p1 nfc_PN532.c
@-${MV} ${OBJECTDIR}/nfc_PN532.d ${OBJECTDIR}/nfc_PN532.p1.d
@${FIXDEPS} ${OBJECTDIR}/nfc_PN532.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/oled_ssd1306.p1: oled_ssd1306.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_ssd1306.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/oled_ssd1306.p1 oled_ssd1306.c
@-${MV} ${OBJECTDIR}/oled_ssd1306.d ${OBJECTDIR}/oled_ssd1306.p1.d
@${FIXDEPS} ${OBJECTDIR}/oled_ssd1306.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/oled_ssd1331.p1: oled_ssd1331.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_ssd1331.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/oled_ssd1331.p1 oled_ssd1331.c
@-${MV} ${OBJECTDIR}/oled_ssd1331.d ${OBJECTDIR}/oled_ssd1331.p1.d
@${FIXDEPS} ${OBJECTDIR}/oled_ssd1331.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/timers.p1: timers.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/timers.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/timers.p1 timers.c
@-${MV} ${OBJECTDIR}/timers.d ${OBJECTDIR}/timers.p1.d
@${FIXDEPS} ${OBJECTDIR}/timers.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/lux_TSL2561.p1: lux_TSL2561.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/lux_TSL2561.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/lux_TSL2561.p1 lux_TSL2561.c
@-${MV} ${OBJECTDIR}/lux_TSL2561.d ${OBJECTDIR}/lux_TSL2561.p1.d
@${FIXDEPS} ${OBJECTDIR}/lux_TSL2561.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1: oled_NHD-0216KZW-AB5.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1 oled_NHD-0216KZW-AB5.c
@-${MV} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.d ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d
@${FIXDEPS} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/adc.p1: adc.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/adc.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/adc.p1 adc.c
@-${MV} ${OBJECTDIR}/adc.d ${OBJECTDIR}/adc.p1.d
@${FIXDEPS} ${OBJECTDIR}/adc.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/temp_BMP085.p1: temp_BMP085.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/temp_BMP085.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist -D__DEBUG=1 --debugger=pickit3 --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/temp_BMP085.p1 temp_BMP085.c
@-${MV} ${OBJECTDIR}/temp_BMP085.d ${OBJECTDIR}/temp_BMP085.p1.d
@${FIXDEPS} ${OBJECTDIR}/temp_BMP085.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
else
${OBJECTDIR}/main.p1: main.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
101,6 → 185,90
@-${MV} ${OBJECTDIR}/uart.d ${OBJECTDIR}/uart.p1.d
@${FIXDEPS} ${OBJECTDIR}/uart.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/i2c.p1: i2c.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/i2c.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/i2c.p1 i2c.c
@-${MV} ${OBJECTDIR}/i2c.d ${OBJECTDIR}/i2c.p1.d
@${FIXDEPS} ${OBJECTDIR}/i2c.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/spi.p1: spi.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/spi.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/spi.p1 spi.c
@-${MV} ${OBJECTDIR}/spi.d ${OBJECTDIR}/spi.p1.d
@${FIXDEPS} ${OBJECTDIR}/spi.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/interrupts.p1: interrupts.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/interrupts.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/interrupts.p1 interrupts.c
@-${MV} ${OBJECTDIR}/interrupts.d ${OBJECTDIR}/interrupts.p1.d
@${FIXDEPS} ${OBJECTDIR}/interrupts.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/led_HT16K33.p1: led_HT16K33.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/led_HT16K33.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/led_HT16K33.p1 led_HT16K33.c
@-${MV} ${OBJECTDIR}/led_HT16K33.d ${OBJECTDIR}/led_HT16K33.p1.d
@${FIXDEPS} ${OBJECTDIR}/led_HT16K33.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/nfc_PN532.p1: nfc_PN532.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/nfc_PN532.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/nfc_PN532.p1 nfc_PN532.c
@-${MV} ${OBJECTDIR}/nfc_PN532.d ${OBJECTDIR}/nfc_PN532.p1.d
@${FIXDEPS} ${OBJECTDIR}/nfc_PN532.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/oled_ssd1306.p1: oled_ssd1306.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_ssd1306.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/oled_ssd1306.p1 oled_ssd1306.c
@-${MV} ${OBJECTDIR}/oled_ssd1306.d ${OBJECTDIR}/oled_ssd1306.p1.d
@${FIXDEPS} ${OBJECTDIR}/oled_ssd1306.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/oled_ssd1331.p1: oled_ssd1331.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_ssd1331.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/oled_ssd1331.p1 oled_ssd1331.c
@-${MV} ${OBJECTDIR}/oled_ssd1331.d ${OBJECTDIR}/oled_ssd1331.p1.d
@${FIXDEPS} ${OBJECTDIR}/oled_ssd1331.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/timers.p1: timers.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/timers.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/timers.p1 timers.c
@-${MV} ${OBJECTDIR}/timers.d ${OBJECTDIR}/timers.p1.d
@${FIXDEPS} ${OBJECTDIR}/timers.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/lux_TSL2561.p1: lux_TSL2561.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/lux_TSL2561.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/lux_TSL2561.p1 lux_TSL2561.c
@-${MV} ${OBJECTDIR}/lux_TSL2561.d ${OBJECTDIR}/lux_TSL2561.p1.d
@${FIXDEPS} ${OBJECTDIR}/lux_TSL2561.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1: oled_NHD-0216KZW-AB5.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1 oled_NHD-0216KZW-AB5.c
@-${MV} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.d ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d
@${FIXDEPS} ${OBJECTDIR}/oled_NHD-0216KZW-AB5.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/adc.p1: adc.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/adc.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/adc.p1 adc.c
@-${MV} ${OBJECTDIR}/adc.d ${OBJECTDIR}/adc.p1.d
@${FIXDEPS} ${OBJECTDIR}/adc.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
${OBJECTDIR}/temp_BMP085.p1: temp_BMP085.c nbproject/Makefile-${CND_CONF}.mk
@${MKDIR} ${OBJECTDIR}
@${RM} ${OBJECTDIR}/temp_BMP085.p1.d
${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G --asmlist --double=32 --float=32 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -o${OBJECTDIR}/temp_BMP085.p1 temp_BMP085.c
@-${MV} ${OBJECTDIR}/temp_BMP085.d ${OBJECTDIR}/temp_BMP085.p1.d
@${FIXDEPS} ${OBJECTDIR}/temp_BMP085.p1.d $(SILENT) -rsi ${MP_CC_DIR}../
endif
 
# ------------------------------------------------------------------------------------
/PIC Stuff/PICX_27J13/nbproject/Makefile-genesis.properties
1,5 → 1,5
#
#Sat Dec 22 14:37:03 EST 2012
#Sun Dec 23 06:07:58 EST 2012
default.languagetoolchain.dir=C\:\\Program Files (x86)\\Microchip\\xc8\\v1.12\\bin
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=d94e033fce233e60ccb9abf3a212a9b7
default.languagetoolchain.version=1.12
/PIC Stuff/PICX_27J13/nbproject/configurations.xml
6,6 → 6,18
projectFiles="true">
<itemPath>defines.h</itemPath>
<itemPath>uart.h</itemPath>
<itemPath>spi.h</itemPath>
<itemPath>i2c.h</itemPath>
<itemPath>interrupts.h</itemPath>
<itemPath>oled_ssd1306.h</itemPath>
<itemPath>nfc_PN532.h</itemPath>
<itemPath>led_HT16K33.h</itemPath>
<itemPath>oled_ssd1331.h</itemPath>
<itemPath>timers.h</itemPath>
<itemPath>lux_TSL2561.h</itemPath>
<itemPath>oled_NHD-0216KZW-AB5.h</itemPath>
<itemPath>adc.h</itemPath>
<itemPath>temp_BMP085.h</itemPath>
</logicalFolder>
<logicalFolder name="LinkerScript"
displayName="Linker Files"
16,6 → 28,18
projectFiles="true">
<itemPath>main.c</itemPath>
<itemPath>uart.c</itemPath>
<itemPath>i2c.c</itemPath>
<itemPath>spi.c</itemPath>
<itemPath>interrupts.c</itemPath>
<itemPath>led_HT16K33.c</itemPath>
<itemPath>nfc_PN532.c</itemPath>
<itemPath>oled_ssd1306.c</itemPath>
<itemPath>oled_ssd1331.c</itemPath>
<itemPath>timers.c</itemPath>
<itemPath>lux_TSL2561.c</itemPath>
<itemPath>oled_NHD-0216KZW-AB5.c</itemPath>
<itemPath>adc.c</itemPath>
<itemPath>temp_BMP085.c</itemPath>
</logicalFolder>
<logicalFolder name="ExternalFiles"
displayName="Important Files"
/PIC Stuff/PICX_27J13/nbproject/private/SuppressibleMessageMemo.properties
0,0 → 1,17
#
#Sat Dec 22 23:45:26 EST 2012
pk3/CHECK_CLOCK=false
pk3/DEVID_MISMATCH=false
pkobskde/CHECK_4_HIGH_VOLTAGE_VPP=false
pkoblicdbgr/DEVID_MISMATCH=false
pk3/CHECK_4_HIGH_VOLTAGE_VPP=false
pkobskde/CHECK_CLOCK=false
pkoblicdbgr/CHECK_CLOCK=false
icd3/CHECK_CLOCK=false
pkobskde/DEVID_MISMATCH=false
icd3/DEVID_MISMATCH=false
realice/CHECK_CLOCK=false
pkoblicdbgr/CHECK_4_HIGH_VOLTAGE_VPP=false
realice/DEVID_MISMATCH=false
icd3/CHECK_4_HIGH_VOLTAGE_VPP=false
realice/CHECK_4_HIGH_VOLTAGE_VPP=false
/PIC Stuff/PICX_27J13/nbproject/private/private.xml
1,10 → 1,4
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/1">
<file>file:/C:/Users/Kevin/Documents/Code/PICX_27J13.X/defines.h</file>
<file>file:/C:/Users/Kevin/Documents/Code/PICX_27J13.X/uart.h</file>
<file>file:/C:/Users/Kevin/Documents/Code/PICX_27J13.X/main.c</file>
<file>file:/C:/Users/Kevin/Documents/Code/PICX_27J13.X/uart.c</file>
</open-files>
</project-private>
/PIC Stuff/PICX_27J13/nfc_PN532.c
0,0 → 1,474
#include <xc.h>
#include <string.h>
#include <delays.h>
#include "defines.h"
#include "nfc_PN532.h"
#include "i2c.h"
 
static NFC_DATA *nfc_data_p;
 
// Const value arrays for comparison use
static const char pn532response_firmwarevers[] = {0x01, 0x00, 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
static const char pn532ack[] = {0x01, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
 
void NFC_Init(NFC_DATA *data) {
nfc_data_p = data;
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)
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_Send_Command_Check_Ack(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_Get_Firmware_Version(void) {
NFC_FIRMWARE_VERSION response = {0, 0, 0, 0};
 
// Create and send command
nfc_data_p->packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
 
if (!NFC_Send_Command_Check_Ack(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
char NFC_Send_Command_Check_Ack(char *cmd, 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
char NFC_Read_Passive_Target_ID(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_Send_Command_Check_Ack(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
char NFC_Poll_Targets(char number, 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_Send_Command_Check_Ack(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)
//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
//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
//char NFC_mifareclassic_AuthenticateBlock(char *uid, char uidLen, unsigned long blockNumber, char keyNumber, 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
//
// // 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 (char i = 0; i < 6; i++) {
// nfc_data_p->packetbuffer[4 + i] = keyData[i];
// }
// for (char i = 0; i < uidLen; i++) {
// nfc_data_p->packetbuffer[10 + i] = uid[i];
// }
//
// // Send frame and check for ACK
// if (!NFC_Send_Command_Check_Ack(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
//char NFC_mifareclassic_ReadDataBlock(char blockNumber, char *data) {
// // 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_Send_Command_Check_Ack(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 (char 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
//char NFC_mifareclassic_WriteDataBlock(char blockNumber, char *data) {
// // 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 (char i = 0; i < 16; i++) { /* Data Payload */
// nfc_data_p->packetbuffer[4 + i] = data[i];
// }
//
// // Send frame and check for ACK
// if (!NFC_Send_Command_Check_Ack(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
//char NFC_mifareclassic_FormatNDEF(void) {
// char sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
// char sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
// 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. */
//char NFC_mifareclassic_WriteNDEFURI(char sectorNumber, 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
// char len = strlen(url);
//
// char sectorbuffer1[16] = {0x00, 0x00, 0x03, len + 5, 0xD1, 0x01, len + 1, 0x55, uriIdentifier, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// char sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// char sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// 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
char NFC_I2C_Read_ACK() {
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
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(char *buffer, char length) {
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(char* cmd, char cmdlen) {
char checksum;
char buffer[PN532_PACKBUFFSIZ + 8];
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 (char 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/PICX_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 {
char IC;
char Ver;
char Rev;
char Support;
} NFC_FIRMWARE_VERSION;
 
typedef struct {
char TG;
char SENS_RES[2];
char SEL_RES;
char NFCID_LEN;
char NFCID[7];
} NFC_TargetDataMiFare;
// Size can be 9 or 12 bytes
 
typedef struct {
char packetbuffer[PN532_PACKBUFFSIZ];
} NFC_DATA;
 
void NFC_Init(NFC_DATA *data);
 
// Generic PN532 functions
char NFC_SAMConfig(void);
NFC_FIRMWARE_VERSION NFC_Get_Firmware_Version(void);
char NFC_Send_Command_Check_Ack(char *cmd, char cmdlen);
//char NFC_writeGPIO(char pinstate);
//char NFC_readGPIO(void);
 
// ISO14443A functions
char NFC_Read_Passive_Target_ID(NFC_TargetDataMiFare *uidData);
char NFC_Poll_Targets(char number, char period, NFC_TargetDataMiFare *uidData);
 
// Mifare Classic functions
//char NFC_mifareclassic_IsFirstBlock(unsigned long uiBlock);
//char NFC_mifareclassic_IsTrailerBlock(unsigned long uiBlock);
//char NFC_mifareclassic_AuthenticateBlock(char *uid, char uidLen, unsigned long blockNumber, char keyNumber, char *keyData);
//char NFC_mifareclassic_ReadDataBlock(char blockNumber, char *data);
//char NFC_mifareclassic_WriteDataBlock(char blockNumber, char *data);
//char NFC_mifareclassic_FormatNDEF(void);
//char NFC_mifareclassic_WriteNDEFURI(char sectorNumber, char uriIdentifier, const char * url);
 
// Mifare Ultralight functions
//char NFC_mifareultralight_ReadPage(char page, char * buffer);
 
// Low level SPI functions
char NFC_I2C_Read_ACK(void);
char NFC_I2C_Read_Status(void);
void NFC_I2C_Read_Data(char *buffer, char length);
void NFC_I2C_Write_Cmd(char *cmd, char cmdlen);
 
#endif
 
/PIC Stuff/PICX_27J13/oled_NHD-0216KZW-AB5.c
0,0 → 1,202
#include <xc.h>
#include <delays.h>
#include <string.h>
#include <stdio.h>
#include "oled_NHD-0216KZW-AB5.h"
#include "defines.h"
 
static OLED_CHAR_DATA *oled_char_data_p;
 
void NHD_Init(OLED_CHAR_DATA *data) {
oled_char_data_p = data;
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(char col, char row) {
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(char location, char *charmap) {
location &= 0x7;
NHD_Send_Command(LCD_SETCGRAMADDR | (location << 3));
for (char i = 0; i < 8; i++) {
NHD_Send_Data(charmap[i]);
}
}
 
void NHD_Send_Command(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(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(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(char* msg, char length) {
for (char i = 0; i < length; i++) {
NHD_Send_Data(msg[i]);
}
}
 
//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/PICX_27J13/oled_NHD-0216KZW-AB5.h
0,0 → 1,79
#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 {
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(OLED_CHAR_DATA *data);
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(char location, char *charmap);
void NHD_Set_Cursor(char col, char row);
 
void NHD_Send_Data(char value);
void NHD_Send_Command(char value);
 
void NHD_Pulse_Enable(void);
void NHD_Write_4_Bits(char value);
void NHD_Wait_For_Ready(void);
 
void NHD_Write_String(char *msg, char length);
//void NHD_Write_String(const rom char *fmt, ...);
 
#endif /* OLED_NHD_0216KZW_AB5_H */
 
/PIC Stuff/PICX_27J13/oled_ssd1306.c
0,0 → 1,835
#include <xc.h>
#include <delays.h>
#include <string.h>
#include <stdio.h>
#include "defines.h"
#include "spi.h"
#include "oled_ssd1306.h"
#include "glcdfont.c"
#include "uart.h"
 
static SSD1306_DATA *ssd1306_data_p;
 
// 512 (128x32) or 1024 (128x64) bytes allocated for LCD buffer
// See linker file for details
static char LCD_BUFFER[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
#if (SSD1306_LCDHEIGHT == 64)
0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#endif
};
 
int SSD1306_Abs(int i) {
if (i < 0)
return -i;
else
return i;
}
 
void SSD1306_Swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
 
void SSD1306_Init(SSD1306_DATA *data) {
ssd1306_data_p = data;
ssd1306_data_p->_width = ssd1306_data_p->WIDTH = SSD1306_LCDWIDTH;
ssd1306_data_p->_height = ssd1306_data_p->HEIGHT = SSD1306_LCDHEIGHT;
ssd1306_data_p->rotation = 0;
ssd1306_data_p->cursor_x = ssd1306_data_p->cursor_y = 0;
ssd1306_data_p->textsize = 1;
ssd1306_data_p->textcolor = SSD1306_WHITE;
ssd1306_data_p->textbgcolor = SSD1306_BLACK;
ssd1306_data_p->wrap = 1;
}
 
void SSD1306_Begin(char vccstate) {
// Toggle reset pin
SPI_RESET_LAT = 0;
Delay10KTCYx(1);
SPI_RESET_LAT = 1;
 
#if defined SSD1306_128_32
// Init sequence for 128x32 OLED module
SSD1306_Command(SSD1306_DISPLAYOFF); // 0xAE
SSD1306_Command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
SSD1306_Command(0x80); // The suggested ratio 0x80
SSD1306_Command(SSD1306_SETMULTIPLEX); // 0xA8
SSD1306_Command(0x1F);
SSD1306_Command(SSD1306_SETDISPLAYOFFSET); // 0xD3
SSD1306_Command(0x0); // No offset
SSD1306_Command(SSD1306_SETSTARTLINE | 0x0); // Line #0
SSD1306_Command(SSD1306_CHARGEPUMP); // 0x8D
if (vccstate == SSD1306_EXTERNALVCC) {
SSD1306_Command(0x10);
} else {
SSD1306_Command(0x14);
}
SSD1306_Command(SSD1306_MEMORYMODE); // 0x20
SSD1306_Command(0x00); // 0x0 act like ks0108
SSD1306_Command(SSD1306_SEGREMAP | 0x1);
SSD1306_Command(SSD1306_COMSCANDEC);
SSD1306_Command(SSD1306_SETCOMPINS); // 0xDA
SSD1306_Command(0x02);
SSD1306_Command(SSD1306_SETCONTRAST); // 0x81
SSD1306_Command(0x8F);
SSD1306_Command(SSD1306_SETPRECHARGE); // 0xd9
if (vccstate == SSD1306_EXTERNALVCC) {
SSD1306_Command(0x22);
} else {
SSD1306_Command(0xF1);
}
SSD1306_Command(SSD1306_SETVCOMDETECT); // 0xDB
SSD1306_Command(0x40);
SSD1306_Command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
SSD1306_Command(SSD1306_NORMALDISPLAY); // 0xA6
#endif
 
#if defined SSD1306_128_64
// Init sequence for 128x64 OLED module
SSD1306_Command(SSD1306_DISPLAYOFF); // 0xAE
SSD1306_Command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
SSD1306_Command(0x80); // The suggested ratio 0x80
SSD1306_Command(SSD1306_SETMULTIPLEX); // 0xA8
SSD1306_Command(0x3F);
SSD1306_Command(SSD1306_SETDISPLAYOFFSET); // 0xD3
SSD1306_Command(0x0); // No offset
SSD1306_Command(SSD1306_SETSTARTLINE | 0x0); // Line #0
SSD1306_Command(SSD1306_CHARGEPUMP); // 0x8D
if (vccstate == SSD1306_EXTERNALVCC) {
SSD1306_Command(0x10);
} else {
SSD1306_Command(0x14);
}
SSD1306_Command(SSD1306_MEMORYMODE); // 0x20
SSD1306_Command(0x00); // 0x0 act like ks0108
SSD1306_Command(SSD1306_SEGREMAP | 0x1);
SSD1306_Command(SSD1306_COMSCANDEC);
SSD1306_Command(SSD1306_SETCOMPINS); // 0xDA
SSD1306_Command(0x12);
SSD1306_Command(SSD1306_SETCONTRAST); // 0x81
if (vccstate == SSD1306_EXTERNALVCC) {
SSD1306_Command(0x9F);
} else {
SSD1306_Command(0xCF);
}
SSD1306_Command(SSD1306_SETPRECHARGE); // 0xd9
if (vccstate == SSD1306_EXTERNALVCC) {
SSD1306_Command(0x22);
} else {
SSD1306_Command(0xF1);
}
SSD1306_Command(SSD1306_SETVCOMDETECT); // 0xDB
SSD1306_Command(0x40);
SSD1306_Command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
SSD1306_Command(SSD1306_NORMALDISPLAY); // 0xA6
#endif
 
SSD1306_Command(SSD1306_DISPLAYON); // Turn on OLED panel
}
 
void SSD1306_Command(char cmd) {
char c = cmd;
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(&c, 1);
}
 
void SSD1306_Data(char data) {
char c = data;
SPI_DC_SELECT_LAT = 1; // D/C high (data)
SPI2_Write(&c, 1);
}
 
void SSD1306_Clear_Display() {
memset(LCD_BUFFER, 0, (SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8));
}
 
void SSD1306_Invert_Display(char c) {
if (c) {
SSD1306_Command(SSD1306_INVERTDISPLAY);
} else {
SSD1306_Command((SSD1306_NORMALDISPLAY));
}
}
 
void SSD1306_Display() {
SSD1306_Command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0
SSD1306_Command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0
SSD1306_Command(SSD1306_SETSTARTLINE | 0x0); // line #0
 
SPI_DC_SELECT_LAT = 1; // D/C high (data)
SPI2_Write(LCD_BUFFER, SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8);
 
// if (SSD1306_LCDHEIGHT == 32) {
// SPI2_Write_Repeat(0, SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8);
// }
}
 
void SSD1306_Draw_Pixel(int x, int y, unsigned int color) {
if ((x < 0) || (x >= ssd1306_data_p->_width) || (y < 0) || (y >= ssd1306_data_p->_height))
return;
 
// check rotation, move pixel around if necessary
switch (ssd1306_data_p->rotation) {
case 1:
SSD1306_Swap(&x, &y);
x = SSD1306_LCDWIDTH - x - 1;
break;
case 2:
x = SSD1306_LCDWIDTH - x - 1;
y = SSD1306_LCDHEIGHT - y - 1;
break;
case 3:
SSD1306_Swap(&x, &y);
y = SSD1306_LCDHEIGHT - y - 1;
break;
default:
break;
}
 
// Need to do this for some reason since x + (y / 8) * SSD1306_LCDWIDTH returns -128?!
// TODO: Change this back when they fix the interrupt routine code
int loc = (y / 8) * SSD1306_LCDWIDTH;
loc += x;
// x is which column
if (color == SSD1306_WHITE) {
LCD_BUFFER[loc] |= 1<<(y % 8);
} else {
LCD_BUFFER[loc] &= ~(1<<(y % 8));
}
}
 
void SSD1306_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color) {
int dx, dy, err, ystep;
int steep = SSD1306_Abs(y1 - y0) > SSD1306_Abs(x1 - x0);
if (steep) {
SSD1306_Swap(&x0, &y0);
SSD1306_Swap(&x1, &y1);
}
 
if (x0 > x1) {
SSD1306_Swap(&x0, &x1);
SSD1306_Swap(&y0, &y1);
}
 
dx = x1 - x0;
dy = SSD1306_Abs(y1 - y0);
 
err = dx / 2;
 
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
 
for (; x0 <= x1; x0++) {
 
if (steep) {
SSD1306_Draw_Pixel(y0, x0, color);
} else {
SSD1306_Draw_Pixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
 
void SSD1306_Draw_Fast_VLine(int x, int y, int h, unsigned int color) {
SSD1306_Draw_Line(x, y, x, y + h - 1, color);
}
 
void SSD1306_Draw_Fast_HLine(int x, int y, int w, unsigned int color) {
SSD1306_Draw_Line(x, y, x + w - 1, y, color);
}
 
void SSD1306_Draw_Rect(int x, int y, int w, int h, unsigned int color) {
SSD1306_Draw_Fast_HLine(x, y, w, color);
SSD1306_Draw_Fast_HLine(x, y + h, w, color);
SSD1306_Draw_Fast_VLine(x, y, h, color);
SSD1306_Draw_Fast_VLine(x + w, y, h, color);
}
 
void SSD1306_Fill_Rect(int x, int y, int w, int h, unsigned int color) {
int i;
for (i = x; i < x + w; i++) {
SSD1306_Draw_Fast_VLine(i, y, h, color);
}
}
 
void SSD1306_Draw_Circle(int x0, int y0, int r, unsigned int color) {
int f = 1 - r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
 
SSD1306_Draw_Pixel(x0, y0 + r, color);
SSD1306_Draw_Pixel(x0, y0 - r, color);
SSD1306_Draw_Pixel(x0 + r, y0, color);
SSD1306_Draw_Pixel(x0 - r, y0, color);
 
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
 
SSD1306_Draw_Pixel(x0 + x, y0 + y, color);
SSD1306_Draw_Pixel(x0 - x, y0 + y, color);
SSD1306_Draw_Pixel(x0 + x, y0 - y, color);
SSD1306_Draw_Pixel(x0 - x, y0 - y, color);
SSD1306_Draw_Pixel(x0 + y, y0 + x, color);
SSD1306_Draw_Pixel(x0 - y, y0 + x, color);
SSD1306_Draw_Pixel(x0 + y, y0 - x, color);
SSD1306_Draw_Pixel(x0 - y, y0 - x, color);
}
}
 
void SSD1306_Draw_Circle_Helper(int x0, int y0, int r, char cornername, unsigned int color) {
int f = 1 - r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
 
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
SSD1306_Draw_Pixel(x0 + x, y0 + y, color);
SSD1306_Draw_Pixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
SSD1306_Draw_Pixel(x0 + x, y0 - y, color);
SSD1306_Draw_Pixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
SSD1306_Draw_Pixel(x0 - y, y0 + x, color);
SSD1306_Draw_Pixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
SSD1306_Draw_Pixel(x0 - y, y0 - x, color);
SSD1306_Draw_Pixel(x0 - x, y0 - y, color);
}
}
}
 
void SSD1306_Fill_Circle(int x0, int y0, int r, unsigned int color) {
SSD1306_Draw_Fast_VLine(x0, y0 - r, 2 * r + 1, color);
SSD1306_Fill_Circle_Helper(x0, y0, r, 3, 0, color);
}
 
void SSD1306_Fill_Circle_Helper(int x0, int y0, int r, char cornername, int delta, unsigned int color) {
int f = 1 - r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
 
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
 
if (cornername & 0x1) {
SSD1306_Draw_Fast_VLine(x0 + x, y0 - y, 2 * y + 1 + delta, color);
SSD1306_Draw_Fast_VLine(x0 + y, y0 - x, 2 * x + 1 + delta, color);
}
if (cornername & 0x2) {
SSD1306_Draw_Fast_VLine(x0 - x, y0 - y, 2 * y + 1 + delta, color);
SSD1306_Draw_Fast_VLine(x0 - y, y0 - x, 2 * x + 1 + delta, color);
}
}
}
 
void SSD1306_Draw_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
SSD1306_Draw_Line(x0, y0, x1, y1, color);
SSD1306_Draw_Line(x1, y1, x2, y2, color);
SSD1306_Draw_Line(x2, y2, x0, y0, color);
}
 
void SSD1306_Fill_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
int a, b, y, last;
int dx01 = x1 - x0;
int dy01 = y1 - y0;
int dx02 = x2 - x0;
int dy02 = y2 - y0;
int dx12 = x2 - x1;
int dy12 = y2 - y1;
int sa = 0;
int sb = 0;
 
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
SSD1306_Swap(&y0, &y1);
SSD1306_Swap(&x0, &x1);
}
if (y1 > y2) {
SSD1306_Swap(&y2, &y1);
SSD1306_Swap(&x2, &x1);
}
if (y0 > y1) {
SSD1306_Swap(&y0, &y1);
SSD1306_Swap(&x0, &x1);
}
 
if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if (x1 < a) a = x1;
else if (x1 > b) b = x1;
if (x2 < a) a = x2;
else if (x2 > b) b = x2;
SSD1306_Draw_Fast_HLine(a, y0, b - a + 1, color);
return;
}
 
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if (y1 == y2) last = y1; // Include y1 scanline
else last = y1 - 1; // Skip it
 
for (y = y0; y <= last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if (a > b) SSD1306_Swap(&a, &b);
SSD1306_Draw_Fast_HLine(a, y, b - a + 1, color);
}
 
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for (; y <= y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if (a > b) SSD1306_Swap(&a, &b);
SSD1306_Draw_Fast_HLine(a, y, b - a + 1, color);
}
}
 
void SSD1306_Draw_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
// smarter version
SSD1306_Draw_Fast_HLine(x + r, y, w - 2 * r, color); // Top
SSD1306_Draw_Fast_HLine(x + r, y + h - 1, w - 2 * r, color); // Bottom
SSD1306_Draw_Fast_VLine(x, y + r, h - 2 * r, color); // Left
SSD1306_Draw_Fast_VLine(x + w - 1, y + r, h - 2 * r, color); // Right
 
// draw four corners
SSD1306_Draw_Circle_Helper(x + r, y + r, r, 1, color);
SSD1306_Draw_Circle_Helper(x + w - r - 1, y + r, r, 2, color);
SSD1306_Draw_Circle_Helper(x + w - r - 1, y + h - r - 1, r, 4, color);
SSD1306_Draw_Circle_Helper(x + r, y + h - r - 1, r, 8, color);
}
 
void SSD1306_Fill_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
// smarter version
SSD1306_Fill_Rect(x + r, y, w - 2 * r, h, color);
 
// draw four corners
SSD1306_Fill_Circle_Helper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1, color);
SSD1306_Fill_Circle_Helper(x + r, y + r, r, 2, h - 2 * r - 1, color);
}
 
void SSD1306_Draw_Bitmap(int x, int y, const char* bitmap, int w, int h, unsigned int color) {
int i, j;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (bitmap[i + (j / 8) * w] & (j % 8)) {
SSD1306_Draw_Pixel(x + i, y + j, color);
}
}
}
}
 
void SSD1306_Draw_Char(int x, int y, char c, unsigned int color, unsigned int bg, char size) {
int i, j;
unsigned int line;
 
if ((x >= ssd1306_data_p->_width) || // Clip right
(y >= ssd1306_data_p->_height) || // Clip bottom
((x + 5 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
 
for (i = 0; i < 6; i++) {
if (i == 5)
line = 0x0;
else
line = font[(c * 5) + i];
for (j = 0; j < 8; j++) {
if (line & 0x1) {
if (size == 1) {// default size
SSD1306_Draw_Pixel(x + i, y + j, color);
} else { // big size
SSD1306_Fill_Rect(x + (i * size), y + (j * size), size, size, color);
}
} else if (bg != color) {
if (size == 1) { // default size
SSD1306_Draw_Pixel(x + i, y + j, bg);
} else { // big size
SSD1306_Fill_Rect(x + i*size, y + j*size, size, size, bg);
}
}
line >>= 1;
}
}
}
 
void SSD1306_Write(char c) {
if (c == '\n' || c == '\r') {
ssd1306_data_p->cursor_y += ssd1306_data_p->textsize * 8;
ssd1306_data_p->cursor_x = 0;
// } else if (c == '\r') {
// // skip em
} else {
SSD1306_Draw_Char(ssd1306_data_p->cursor_x, ssd1306_data_p->cursor_y, c, ssd1306_data_p->textcolor, ssd1306_data_p->textbgcolor, ssd1306_data_p->textsize);
ssd1306_data_p->cursor_x += ssd1306_data_p->textsize * 6;
if (ssd1306_data_p->wrap && (ssd1306_data_p->cursor_x > (ssd1306_data_p->_width - ssd1306_data_p->textsize * 6))) {
ssd1306_data_p->cursor_y += ssd1306_data_p->textsize * 8;
ssd1306_data_p->cursor_x = 0;
}
}
}
 
void SSD1306_Write_String(char* msg, char length) {
for (char i = 0; i < length; i++) {
SSD1306_Write(msg[i]);
}
}
 
//void SSD1306_Write_String(const rom char *fmt, ...) {
// char i, len;
// char buffer[SSD1306_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 > SSD1306_STRING_BUFFER_SIZE)
// len = SSD1306_STRING_BUFFER_SIZE;
//
// // Print buffer to string
// for (i = 0; i < len; i++) {
// SSD1306_Write(buffer[i]);
// }
//}
 
//void SSD1306_Append_String(const rom char *fmt, ...) {
// char i, len;
// char buffer[SSD1306_STRING_BUFFER_SIZE];
//
// // Parse and create string
// va_list args;
// va_start(args, fmt);
// vsprintf((char *) buffer, fmt, args);
// va_end(args);
//
// // Make sure string to insert fits in buffer, truncate if necessary
// len = strlen((char *) buffer);
//
// if (len == 1) { // This will only occur on "\n"
// // Do nothing?
// return;
// }
//
// if (len > SSD1306_STRING_BUFFER_SIZE)
// len = SSD1306_STRING_BUFFER_SIZE;
//
// // Omit the newline if string fill entire line
// if (((len - 1)%(ssd1306_data_p->_width / 6)) == 0) { // 16 or 10
// len -= 1;
// }
//
// // Shift everything right and insert string at beginning
// for (i = 127; i > len - 1; i--) {
// ssd1306_data_p->lcd_buffer[i] = ssd1306_data_p->lcd_buffer[i - len];
// }
// memcpy((char *)ssd1306_data_p->lcd_buffer, (const char *) buffer, len);
//
// // Print full buffer to screen
// SSD1306_Clear_Display();
// SSD1306_Display();
//
// SSD1306_Set_Cursor(0,0);
// for (i = 0; i < SSD1306_LCD_BUFFER_SIZE-1; i++) {
// SSD1306_Write(ssd1306_data_p->lcd_buffer[i]);
// }
//}
 
void SSD1306_Set_Cursor(int x, int y) {
ssd1306_data_p->cursor_x = x;
ssd1306_data_p->cursor_y = y;
}
 
void SSD1306_Set_Text_Color(unsigned int c) {
// for 'transparent' background, we'll set the bg
// to the same as fg instead of using a flag
ssd1306_data_p->textcolor = c;
ssd1306_data_p->textbgcolor = c;
}
 
void SSD1306_Set_Text_Color_BG(unsigned int c, unsigned int bg) {
ssd1306_data_p->textcolor = c;
ssd1306_data_p->textbgcolor = bg;
}
 
void SSD1306_Set_Text_Size(char s) {
ssd1306_data_p->textsize = (s > 0) ? s : 1;
}
 
void SSD1306_Set_Text_Wrap(char w) {
ssd1306_data_p->wrap = w;
}
 
void SSD1306_Set_Rotation(char x) {
x %= 4; // cant be higher than 3
ssd1306_data_p->rotation = x;
switch (x) {
case 0:
case 2:
ssd1306_data_p->_width = ssd1306_data_p->WIDTH;
ssd1306_data_p->_height = ssd1306_data_p->HEIGHT;
break;
case 1:
case 3:
ssd1306_data_p->_width = ssd1306_data_p->HEIGHT;
ssd1306_data_p->_height = ssd1306_data_p->WIDTH;
break;
}
}
 
 
 
void SSD1306_Test_DrawChar() {
char i;
SSD1306_Set_Text_Size(1);
SSD1306_Set_Text_Color(SSD1306_WHITE);
SSD1306_Set_Cursor(0, 0);
 
for (i = 0; i < 168; i++) {
if (i == '\n') continue;
SSD1306_Write(i);
// if ((i > 0) && (i % 21 == 0))
// SSD1306_write('\n');
}
SSD1306_Display();
}
 
void SSD1306_Test_DrawCircle() {
int i;
for (i = 0; i < ssd1306_data_p->_height; i += 2) {
SSD1306_Draw_Circle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2, i, SSD1306_WHITE);
SSD1306_Display();
}
}
 
void SSD1306_Test_DrawRect(void) {
int i;
for (i = 0; i < ssd1306_data_p->_height / 2; i += 2) {
SSD1306_Draw_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, SSD1306_WHITE);
SSD1306_Display();
}
}
 
void SSD1306_Test_FillRect(void) {
char color = 1;
int i;
for (i = 0; i < ssd1306_data_p->_height / 2; i += 3) {
// alternate colors
SSD1306_Fill_Rect(i, i, ssd1306_data_p->_width - i * 2, ssd1306_data_p->_height - i * 2, color % 2);
SSD1306_Display();
color++;
}
}
 
void SSD1306_Test_DrawTriangle(void) {
int i;
int min = ssd1306_data_p->_width < ssd1306_data_p->_height ? ssd1306_data_p->_width : ssd1306_data_p->_height;
for (i = 0; i < min / 2; i += 5) {
SSD1306_Draw_Triangle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2 - i,
ssd1306_data_p->_width / 2 - i, ssd1306_data_p->_height / 2 + i,
ssd1306_data_p->_width / 2 + i, ssd1306_data_p->_height / 2 + i, SSD1306_WHITE);
SSD1306_Display();
}
}
 
void SSD1306_Test_FillTriangle(void) {
char color = SSD1306_WHITE;
int i;
int min = ssd1306_data_p->_width < ssd1306_data_p->_height ? ssd1306_data_p->_width : ssd1306_data_p->_height;
for (i = min / 2; i > 0; i -= 5) {
SSD1306_Fill_Triangle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2 - i,
ssd1306_data_p->_width / 2 - i, ssd1306_data_p->_height / 2 + i,
ssd1306_data_p->_width / 2 + i, ssd1306_data_p->_height / 2 + i, SSD1306_WHITE);
if (color == SSD1306_WHITE) color = SSD1306_BLACK;
else color = SSD1306_WHITE;
SSD1306_Display();
}
}
 
void SSD1306_Test_DrawRoundRect(void) {
int i;
for (i = 0; i < ssd1306_data_p->_height / 2 - 2; i += 2) {
SSD1306_Draw_Round_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, ssd1306_data_p->_height / 4, SSD1306_WHITE);
SSD1306_Display();
}
}
 
void SSD1306_Test_FillRoundRect(void) {
char color = SSD1306_WHITE;
int i;
for (i = 0; i < ssd1306_data_p->_height / 2 - 2; i += 2) {
SSD1306_Fill_Round_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, ssd1306_data_p->_height / 4, color);
if (color == SSD1306_WHITE) color = SSD1306_BLACK;
else color = SSD1306_WHITE;
SSD1306_Display();
}
}
 
void SSD1306_Test_DrawLine(void) {
int i;
for (i = 0; i < ssd1306_data_p->_width; i += 4) {
SSD1306_Draw_Line(0, 0, i, ssd1306_data_p->_height - 1, SSD1306_WHITE);
SSD1306_Display();
}
for (i = 0; i < ssd1306_data_p->_height; i += 4) {
SSD1306_Draw_Line(0, 0, ssd1306_data_p->_width - 1, i, SSD1306_WHITE);
SSD1306_Display();
}
Delay10KTCYx(255);
 
SSD1306_Clear_Display();
for (i = 0; i < ssd1306_data_p->_width; i += 4) {
SSD1306_Draw_Line(0, ssd1306_data_p->_height - 1, i, 0, SSD1306_WHITE);
SSD1306_Display();
}
for (i = ssd1306_data_p->_height - 1; i >= 0; i -= 4) {
SSD1306_Draw_Line(0, ssd1306_data_p->_height - 1, ssd1306_data_p->_width - 1, i, SSD1306_WHITE);
SSD1306_Display();
}
Delay10KTCYx(255);
 
SSD1306_Clear_Display();
for (i = ssd1306_data_p->_width - 1; i >= 0; i -= 4) {
SSD1306_Draw_Line(ssd1306_data_p->_width - 1, ssd1306_data_p->_height - 1, i, 0, SSD1306_WHITE);
SSD1306_Display();
}
for (i = ssd1306_data_p->_height - 1; i >= 0; i -= 4) {
SSD1306_Draw_Line(ssd1306_data_p->_width - 1, ssd1306_data_p->_height - 1, 0, i, SSD1306_WHITE);
SSD1306_Display();
}
Delay10KTCYx(255);
 
SSD1306_Clear_Display();
for (i = 0; i < ssd1306_data_p->_height; i += 4) {
SSD1306_Draw_Line(ssd1306_data_p->_width - 1, 0, 0, i, SSD1306_WHITE);
SSD1306_Display();
}
for (i = 0; i < ssd1306_data_p->_width; i += 4) {
SSD1306_Draw_Line(ssd1306_data_p->_width - 1, 0, i, ssd1306_data_p->_height - 1, SSD1306_WHITE);
SSD1306_Display();
}
Delay10KTCYx(255);
}
/PIC Stuff/PICX_27J13/oled_ssd1306.h
0,0 → 1,132
#ifndef OLED_SSD1306_H
#define OLED_SSD1306_H
 
/*=========================================================================
SSD1306 Displays
-----------------------------------------------------------------------
The driver is used in multiple displays (128x64, 128x32, etc.).
Select the appropriate display below to create an appropriately
sized framebuffer, etc.
 
SSD1306_128_64 128x64 pixel display
 
SSD1306_128_32 128x32 pixel display
 
You also need to set the LCDWIDTH and LCDHEIGHT defines to an
appropriate size
 
-----------------------------------------------------------------------*/
#define SSD1306_128_64
// #define SSD1306_128_32
/*=========================================================================*/
 
#if defined SSD1306_128_64
#define SSD1306_LCDWIDTH 128
#define SSD1306_LCDHEIGHT 64
#endif
#if defined SSD1306_128_32
#define SSD1306_LCDWIDTH 128
#define SSD1306_LCDHEIGHT 32
#endif
 
//#define SSD1306_STRING_BUFFER_SIZE 32
 
#define SSD1306_BLACK 0
#define SSD1306_WHITE 1
 
#define SSD1306_I2C_ADDRESS 0x3D // 011110+SA0+RW
 
#define SSD1306_SETCONTRAST 0x81
#define SSD1306_DISPLAYALLON_RESUME 0xA4
#define SSD1306_DISPLAYALLON 0xA5
#define SSD1306_NORMALDISPLAY 0xA6
#define SSD1306_INVERTDISPLAY 0xA7
#define SSD1306_DISPLAYOFF 0xAE
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_SETDISPLAYOFFSET 0xD3
#define SSD1306_SETCOMPINS 0xDA
#define SSD1306_SETVCOMDETECT 0xDB
#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
#define SSD1306_SETPRECHARGE 0xD9
#define SSD1306_SETMULTIPLEX 0xA8
#define SSD1306_SETLOWCOLUMN 0x00
#define SSD1306_SETHIGHCOLUMN 0x10
#define SSD1306_SETSTARTLINE 0x40
#define SSD1306_MEMORYMODE 0x20
#define SSD1306_COMSCANINC 0xC0
#define SSD1306_COMSCANDEC 0xC8
#define SSD1306_SEGREMAP 0xA0
#define SSD1306_CHARGEPUMP 0x8D
#define SSD1306_EXTERNALVCC 0x1
#define SSD1306_SWITCHCAPVCC 0x2
 
typedef struct {
int WIDTH, HEIGHT; // raw display size
int _width, _height; // size depending on rotation
int cursor_x, cursor_y;
unsigned int textcolor, textbgcolor;
char textsize;
char rotation;
char wrap; // If set, wrap text at right side
} SSD1306_DATA;
 
// Misc functions
int SSD1306_Abs(int i);
void SSD1306_Swap(int *a, int *b);
 
// Core functions
void SSD1306_Init(SSD1306_DATA *data);
void SSD1306_Begin(char vcc);
void SSD1306_Command(char cmd);
void SSD1306_Data(char data);
 
void SSD1306_Clear_Display(void);
void SSD1306_Invert_Display(char);
void SSD1306_Display(void);
 
// Drawing functions
void SSD1306_Draw_Pixel(int x, int y, unsigned int color);
void SSD1306_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color);
void SSD1306_Draw_Fast_VLine(int x, int y, int h, unsigned int color);
void SSD1306_Draw_Fast_HLine(int x, int y, int w, unsigned int color);
void SSD1306_Draw_Rect(int x, int y, int w, int h, unsigned int color);
void SSD1306_Fill_Rect(int x, int y, int w, int h, unsigned int color);
 
void SSD1306_Draw_Circle(int x0, int y0, int r, unsigned int color);
void SSD1306_Draw_Circle_Helper(int x0, int y0, int r, char cornername, unsigned int color);
void SSD1306_Fill_Circle(int x0, int y0, int r, unsigned int color);
void SSD1306_Fill_Circle_Helper(int x0, int y0, int r, char cornername, int delta, unsigned int color);
 
void SSD1306_Draw_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color);
void SSD1306_Fill_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color);
void SSD1306_Draw_Round_Rect(int x0, int y0, int w, int h, int radius, unsigned int color);
void SSD1306_Fill_Round_Rect(int x0, int y0, int w, int h, int radius, unsigned int color);
 
void SSD1306_Draw_Bitmap(int x, int y, const char *bitmap, int w, int h, unsigned int color);
void SSD1306_Draw_Char(int x, int y, char c, unsigned int color, unsigned int bg, char size);
 
void SSD1306_Write(char c);
void SSD1306_Write_String(char *msg, char length);
//void SSD1306_Write_String(const rom char *fmt, ...);
//void SSD1306_Append_String(const rom char *fmt, ...);
 
void SSD1306_Set_Cursor(int x, int y);
void SSD1306_Set_Text_Color(unsigned int c);
void SSD1306_Set_Text_Color_BG(unsigned int c, unsigned int bg);
void SSD1306_Set_Text_Size(char s);
void SSD1306_Set_Text_Wrap(char w);
void SSD1306_Set_Rotation(char r);
 
// Test functions
void SSD1306_Test_DrawChar(void);
void SSD1306_Test_DrawCircle(void);
void SSD1306_Test_DrawRect(void);
void SSD1306_Test_FillRect(void);
void SSD1306_Test_DrawTriangle(void);
void SSD1306_Test_FillTriangle(void);
void SSD1306_Test_DrawRoundRect(void);
void SSD1306_Test_FillRoundRect(void);
void SSD1306_Test_DrawLine(void);
 
#endif /* OLED_SSD1306_H */
 
/PIC Stuff/PICX_27J13/oled_ssd1331.c
0,0 → 1,902
#include <xc.h>
#include <delays.h>
#include <string.h>
#include <stdio.h>
#include "defines.h"
#include "oled_ssd1331.h"
#include "spi.h"
#include "string.h"
#include "glcdfont.c"
 
static SSD1331_DATA *ssd1331_data_p;
 
int SSD1331_Abs(int i) {
if (i < 0)
return -i;
else
return i;
}
 
void SSD1331_Swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
 
void SSD1331_Init(SSD1331_DATA *data) {
ssd1331_data_p = data;
ssd1331_data_p->_width = ssd1331_data_p->WIDTH = SSD1331_LCDWIDTH;
ssd1331_data_p->_height = ssd1331_data_p->HEIGHT = SSD1331_LCDHEIGHT;
ssd1331_data_p->rotation = 0;
ssd1331_data_p->cursor_x = ssd1331_data_p->cursor_y = 0;
ssd1331_data_p->textsize = 1;
ssd1331_data_p->textcolor = ssd1331_data_p->textbgcolor = 0xFFFF;
ssd1331_data_p->wrap = 1;
}
 
void SSD1331_Begin() {
char buffer[37];
 
// Toggle reset pin
SPI_RESET_LAT = 0;
Delay10KTCYx(1);
SPI_RESET_LAT = 1;
 
// Initialization Sequence
buffer[0] = SSD1331_CMD_DISPLAYOFF; // 0xAE
buffer[1] = SSD1331_CMD_SETREMAP; // 0xA0
#if defined SSD1331_COLORORDER_RGB
buffer[2] = 0x72; // RGB Color
#else
buffer[2] = 0x76; // BGR Color
#endif
buffer[3] = SSD1331_CMD_STARTLINE; // 0xA1
buffer[4] = 0x0;
buffer[5] = SSD1331_CMD_DISPLAYOFFSET; // 0xA2
buffer[6] = 0x0;
buffer[7] = SSD1331_CMD_NORMALDISPLAY; // 0xA4
buffer[8] = SSD1331_CMD_SETMULTIPLEX; // 0xA8
buffer[9] = 0x3F; // 0x3F 1/64 duty
buffer[10] = SSD1331_CMD_SETMASTER; // 0xAD
buffer[11] = 0x8E;
buffer[12] = SSD1331_CMD_POWERMODE; // 0xB0
buffer[13] = 0x0B;
buffer[14] = SSD1331_CMD_PRECHARGE; // 0xB1
buffer[15] = 0x31;
buffer[16] = SSD1331_CMD_CLOCKDIV; // 0xB3
buffer[17] = 0xF0; // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
buffer[18] = SSD1331_CMD_PRECHARGEA; // 0x8A
buffer[19] = 0x64;
buffer[20] = SSD1331_CMD_PRECHARGEB; // 0x8B
buffer[21] = 0x78;
buffer[22] = SSD1331_CMD_PRECHARGEA; // 0x8C
buffer[23] = 0x64;
buffer[24] = SSD1331_CMD_PRECHARGELEVEL; // 0xBB
buffer[25] = 0x3A;
buffer[26] = SSD1331_CMD_VCOMH; // 0xBE
buffer[27] = 0x3E;
buffer[28] = SSD1331_CMD_MASTERCURRENT; // 0x87
buffer[29] = 0x06;
buffer[30] = SSD1331_CMD_CONTRASTA; // 0x81
buffer[31] = 0x91;
buffer[32] = SSD1331_CMD_CONTRASTB; // 0x82
buffer[33] = 0x50;
buffer[34] = SSD1331_CMD_CONTRASTC; // 0x83
buffer[35] = 0x7D;
buffer[36] = SSD1331_CMD_DISPLAYON; //--turn on oled panel
 
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(buffer, 37);
}
 
void SSD1331_GoTo(int x, int y) {
char buffer[6];
if ((x >= SSD1331_LCDWIDTH) || (y >= SSD1331_LCDHEIGHT)) return;
 
// set x and y coordinate
buffer[0] = (SSD1331_CMD_SETCOLUMN);
buffer[1] = (x); // Start x address
buffer[2] = (SSD1331_LCDWIDTH - 1); // End x address
 
buffer[3] = (SSD1331_CMD_SETROW);
buffer[4] = (y); // Start y address
buffer[5] = (SSD1331_LCDHEIGHT - 1); // End y address
 
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(buffer, 6);
}
 
void SSD1331_Command(char cmd) {
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(&cmd, 1);
}
 
void SSD1331_Data(char data) {
SPI_DC_SELECT_LAT = 1; // D/C high (data)
SPI2_Write(&data, 1);
}
 
void SSD1331_Clear_Display() {
char buffer[5];
 
buffer[0] = SSD1331_CMD_CLEARWINDOW;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = SSD1331_LCDWIDTH-1;
buffer[4] = SSD1331_LCDHEIGHT-1;
 
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(buffer, 5);
 
Delay1KTCYx(4);
}
 
void SSD1331_Draw_Pixel(int x, int y, unsigned int color) {
char buffer[2];
buffer[0] = color >> 8;
buffer[1] = color;
if ((x < 0) || (x >= ssd1331_data_p->_width) || (y < 0) || (y >= ssd1331_data_p->_height)) return;
 
// check rotation, move pixel around if necessary
switch (ssd1331_data_p->rotation) {
case 1:
SSD1331_Swap(&x, &y);
x = SSD1331_LCDWIDTH - x - 1;
break;
case 2:
x = SSD1331_LCDWIDTH - x - 1;
y = SSD1331_LCDHEIGHT - y - 1;
break;
case 3:
SSD1331_Swap(&x, &y);
y = SSD1331_LCDHEIGHT - y - 1;
break;
}
 
SSD1331_GoTo(x, y);
 
// setup for data
SPI_DC_SELECT_LAT = 1; // D/C high (data)
 
SPI2_Write(buffer, 2);
}
 
void SSD1331_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color) {
char buffer[8];
 
// check rotation, move pixel around if necessary
switch (ssd1331_data_p->rotation) {
case 1:
SSD1331_Swap(&x0, &y0);
SSD1331_Swap(&x1, &y1);
x0 = SSD1331_LCDWIDTH - x0 - 1;
x1 = SSD1331_LCDWIDTH - x1 - 1;
break;
case 2:
x0 = SSD1331_LCDWIDTH - x0 - 1;
y0 = SSD1331_LCDHEIGHT - y0 - 1;
x1 = SSD1331_LCDWIDTH - x1 - 1;
y1 = SSD1331_LCDHEIGHT - y1 - 1;
break;
case 3:
SSD1331_Swap(&x0, &y0);
SSD1331_Swap(&x1, &y1);
y0 = SSD1331_LCDHEIGHT - y0 - 1;
y1 = SSD1331_LCDHEIGHT - y1 - 1;
break;
}
 
// Boundary check
if ((y0 >= SSD1331_LCDHEIGHT) && (y1 >= SSD1331_LCDHEIGHT))
return;
if ((x0 >= SSD1331_LCDWIDTH) && (x1 >= SSD1331_LCDWIDTH))
return;
if (x0 >= SSD1331_LCDWIDTH)
x0 = SSD1331_LCDWIDTH - 1;
if (y0 >= SSD1331_LCDHEIGHT)
y0 = SSD1331_LCDHEIGHT - 1;
if (x1 >= SSD1331_LCDWIDTH)
x1 = SSD1331_LCDWIDTH - 1;
if (y1 >= SSD1331_LCDHEIGHT)
y1 = SSD1331_LCDHEIGHT - 1;
if (x0 < 0)
x0 = 0;
if (y0 < 0)
y0 = 0;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
 
buffer[0] = SSD1331_CMD_DRAWLINE;
buffer[1] = x0;
buffer[2] = y0;
buffer[3] = x1;
buffer[4] = y1;
buffer[5] = (color >> 11) << 1;
buffer[6] = (color >> 5) & 0x3F;
buffer[7] = (color << 1) & 0x3F;
 
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(buffer, 8);
}
 
void SSD1331_Draw_Fast_VLine(int x, int y, int h, unsigned int color) {
SSD1331_Draw_Line(x, y, x, y + h - 1, color);
}
 
void SSD1331_Draw_Fast_HLine(int x, int y, int w, unsigned int color) {
SSD1331_Draw_Line(x, y, x + w - 1, y, color);
}
 
void SSD1331_Draw_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
char buffer[13];
int x0,y0,x1,y1;
// check rotation, move pixel around if necessary
switch (ssd1331_data_p->rotation) {
case 0:
x0 = tx0;
y0 = ty0;
x1 = tx1;
y1 = ty1;
break;
case 1:
x0 = SSD1331_LCDWIDTH - ty1 - 1;
y0 = tx0;
x1 = SSD1331_LCDWIDTH - ty0 - 1;
y1 = tx1;
break;
case 2:
x0 = SSD1331_LCDWIDTH - tx1 - 1;
y0 = SSD1331_LCDHEIGHT - ty1 - 1;
x1 = SSD1331_LCDWIDTH - tx0 - 1;
y1 = SSD1331_LCDHEIGHT - ty0 - 1;
break;
case 3:
x0 = ty0;
y0 = SSD1331_LCDHEIGHT - tx1 - 1;
x1 = ty1;
y1 = SSD1331_LCDHEIGHT - tx0 - 1;
break;
}
 
// Boundary check
if ((y0 >= SSD1331_LCDHEIGHT) && (y1 >= SSD1331_LCDHEIGHT))
return;
if ((x0 >= SSD1331_LCDWIDTH) && (x1 >= SSD1331_LCDWIDTH))
return;
if (x0 >= SSD1331_LCDWIDTH)
x0 = SSD1331_LCDWIDTH - 1;
if (y0 >= SSD1331_LCDHEIGHT)
y0 = SSD1331_LCDHEIGHT - 1;
if (x1 >= SSD1331_LCDWIDTH)
x1 = SSD1331_LCDWIDTH - 1;
if (y1 >= SSD1331_LCDHEIGHT)
y1 = SSD1331_LCDHEIGHT - 1;
if (x0 < 0)
x0 = 0;
if (y0 < 0)
y0 = 0;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
 
buffer[0] = SSD1331_CMD_FILL;
buffer[1] = 0;
buffer[2] = SSD1331_CMD_DRAWRECT;
buffer[3] = x0;
buffer[4] = y0;
buffer[5] = x1;
buffer[6] = y1;
buffer[7] = (color >> 11) << 1;
buffer[8] = (color >> 5) & 0x3F;
buffer[9] = (color << 1) & 0x3F;
buffer[10] = 0;
buffer[11] = 0;
buffer[12] = 0;
 
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(buffer, 13);
}
 
void SSD1331_Fill_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
char buffer[13];
int x0,y0,x1,y1;
// check rotation, move pixel around if necessary
switch (ssd1331_data_p->rotation) {
case 0:
x0 = tx0;
y0 = ty0;
x1 = tx1;
y1 = ty1;
break;
case 1:
x0 = SSD1331_LCDWIDTH - ty1 - 1;
y0 = tx0;
x1 = SSD1331_LCDWIDTH - ty0 - 1;
y1 = tx1;
break;
case 2:
x0 = SSD1331_LCDWIDTH - tx1 - 1;
y0 = SSD1331_LCDHEIGHT - ty1 - 1;
x1 = SSD1331_LCDWIDTH - tx0 - 1;
y1 = SSD1331_LCDHEIGHT - ty0 - 1;
break;
case 3:
x0 = ty0;
y0 = SSD1331_LCDHEIGHT - tx1 - 1;
x1 = ty1;
y1 = SSD1331_LCDHEIGHT - tx0 - 1;
break;
}
 
// Boundary check
if ((y0 >= SSD1331_LCDHEIGHT) && (y1 >= SSD1331_LCDHEIGHT))
return;
if ((x0 >= SSD1331_LCDWIDTH) && (x1 >= SSD1331_LCDWIDTH))
return;
if (x0 >= SSD1331_LCDWIDTH)
x0 = SSD1331_LCDWIDTH - 1;
if (y0 >= SSD1331_LCDHEIGHT)
y0 = SSD1331_LCDHEIGHT - 1;
if (x1 >= SSD1331_LCDWIDTH)
x1 = SSD1331_LCDWIDTH - 1;
if (y1 >= SSD1331_LCDHEIGHT)
y1 = SSD1331_LCDHEIGHT - 1;
if (x0 < 0)
x0 = 0;
if (y0 < 0)
y0 = 0;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
 
buffer[0] = SSD1331_CMD_FILL;
buffer[1] = 1;
buffer[2] = SSD1331_CMD_DRAWRECT;
buffer[3] = x0;
buffer[4] = y0;
buffer[5] = x1;
buffer[6] = y1;
buffer[7] = (color >> 11) << 1;
buffer[8] = (color >> 5) & 0x3F;
buffer[9] = (color << 1) & 0x3F;
buffer[10] = (color >> 11) << 1;
buffer[11] = (color >> 5) & 0x3F;
buffer[12] = (color << 1) & 0x3F;
 
SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
SPI2_Write(buffer, 13);
 
Delay1KTCYx(4);
}
 
void SSD1331_Draw_Circle(int x0, int y0, int r, unsigned int color) {
int f = 1 - r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
 
SSD1331_Draw_Pixel(x0, y0 + r, color);
SSD1331_Draw_Pixel(x0, y0 - r, color);
SSD1331_Draw_Pixel(x0 + r, y0, color);
SSD1331_Draw_Pixel(x0 - r, y0, color);
 
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
 
SSD1331_Draw_Pixel(x0 + x, y0 + y, color);
SSD1331_Draw_Pixel(x0 - x, y0 + y, color);
SSD1331_Draw_Pixel(x0 + x, y0 - y, color);
SSD1331_Draw_Pixel(x0 - x, y0 - y, color);
SSD1331_Draw_Pixel(x0 + y, y0 + x, color);
SSD1331_Draw_Pixel(x0 - y, y0 + x, color);
SSD1331_Draw_Pixel(x0 + y, y0 - x, color);
SSD1331_Draw_Pixel(x0 - y, y0 - x, color);
}
}
 
void SSD1331_Draw_Circle_Helper(int x0, int y0, int r, char cornername, unsigned int color) {
int f = 1 - r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
 
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
SSD1331_Draw_Pixel(x0 + x, y0 + y, color);
SSD1331_Draw_Pixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
SSD1331_Draw_Pixel(x0 + x, y0 - y, color);
SSD1331_Draw_Pixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
SSD1331_Draw_Pixel(x0 - y, y0 + x, color);
SSD1331_Draw_Pixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
SSD1331_Draw_Pixel(x0 - y, y0 - x, color);
SSD1331_Draw_Pixel(x0 - x, y0 - y, color);
}
}
}
 
void SSD1331_Fill_Circle(int x0, int y0, int r, unsigned int color) {
SSD1331_Draw_Fast_VLine(x0, y0 - r, 2 * r + 1, color);
SSD1331_Fill_Circle_Helper(x0, y0, r, 3, 0, color);
}
 
void SSD1331_Fill_Circle_Helper(int x0, int y0, int r, char cornername, int delta, unsigned int color) {
int f = 1 - r;
int ddF_x = 1;
int ddF_y = -2 * r;
int x = 0;
int y = r;
 
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
 
if (cornername & 0x1) {
SSD1331_Draw_Fast_VLine(x0 + x, y0 - y, 2 * y + 1 + delta, color);
SSD1331_Draw_Fast_VLine(x0 + y, y0 - x, 2 * x + 1 + delta, color);
}
if (cornername & 0x2) {
SSD1331_Draw_Fast_VLine(x0 - x, y0 - y, 2 * y + 1 + delta, color);
SSD1331_Draw_Fast_VLine(x0 - y, y0 - x, 2 * x + 1 + delta, color);
}
}
}
void SSD1331_Draw_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
SSD1331_Draw_Line(x0, y0, x1, y1, color);
SSD1331_Draw_Line(x1, y1, x2, y2, color);
SSD1331_Draw_Line(x2, y2, x0, y0, color);
}
 
void SSD1331_Fill_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
int a, b, y, last;
int dx01 = x1 - x0;
int dy01 = y1 - y0;
int dx02 = x2 - x0;
int dy02 = y2 - y0;
int dx12 = x2 - x1;
int dy12 = y2 - y1;
int sa = 0;
int sb = 0;
 
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
SSD1331_Swap(&y0, &y1);
SSD1331_Swap(&x0, &x1);
}
if (y1 > y2) {
SSD1331_Swap(&y2, &y1);
SSD1331_Swap(&x2, &x1);
}
if (y0 > y1) {
SSD1331_Swap(&y0, &y1);
SSD1331_Swap(&x0, &x1);
}
 
if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if (x1 < a) a = x1;
else if (x1 > b) b = x1;
if (x2 < a) a = x2;
else if (x2 > b) b = x2;
SSD1331_Draw_Fast_HLine(a, y0, b - a + 1, color);
return;
}
 
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if (y1 == y2) last = y1; // Include y1 scanline
else last = y1 - 1; // Skip it
 
for (y = y0; y <= last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if (a > b) SSD1331_Swap(&a, &b);
SSD1331_Draw_Fast_HLine(a, y, b - a + 1, color);
}
 
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for (; y <= y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if (a > b) SSD1331_Swap(&a, &b);
SSD1331_Draw_Fast_HLine(a, y, b - a + 1, color);
}
}
 
void SSD1331_Draw_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
// smarter version
SSD1331_Draw_Fast_HLine(x + r, y, w - 2 * r, color); // Top
SSD1331_Draw_Fast_HLine(x + r, y + h - 1, w - 2 * r, color); // Bottom
SSD1331_Draw_Fast_VLine(x, y + r, h - 2 * r, color); // Left
SSD1331_Draw_Fast_VLine(x + w - 1, y + r, h - 2 * r, color); // Right
 
// draw four corners
SSD1331_Draw_Circle_Helper(x + r, y + r, r, 1, color);
SSD1331_Draw_Circle_Helper(x + w - r - 1, y + r, r, 2, color);
SSD1331_Draw_Circle_Helper(x + w - r - 1, y + h - r - 1, r, 4, color);
SSD1331_Draw_Circle_Helper(x + r, y + h - r - 1, r, 8, color);
}
 
void SSD1331_Fill_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
// smarter version
SSD1331_Fill_Rect(x + r, y, w - 2 * r, h, color);
 
// draw four corners
SSD1331_Fill_Circle_Helper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1, color);
SSD1331_Fill_Circle_Helper(x + r, y + r, r, 2, h - 2 * r - 1, color);
}
 
void SSD1331_Draw_Bitmap(int x, int y, const char* bitmap, int w, int h, unsigned int color) {
int i, j;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (bitmap[i + (j / 8) * w] & (j % 8)) {
SSD1331_Draw_Pixel(x + i, y + j, color);
}
}
}
}
 
void SSD1331_Draw_Char(int x, int y, char c, unsigned int color, unsigned int bg, char size) {
int i, j;
unsigned int line;
 
if ((x >= ssd1331_data_p->_width) || // Clip right
(y >= ssd1331_data_p->_height) || // Clip bottom
((x + 5 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
 
for (i = 0; i < 6; i++) {
if (i == 5)
line = 0x0;
else
line = font[(c * 5) + i];
for (j = 0; j < 8; j++) {
if (line & 0x1) {
if (size == 1) {// default size
SSD1331_Draw_Pixel(x + i, y + j, color);
} else { // big size
SSD1331_Fill_Rect(x + (i * size), y + (j * size), size, size, color);
}
} else if (bg != color) {
if (size == 1) { // default size
SSD1331_Draw_Pixel(x + i, y + j, bg);
} else { // big size
SSD1331_Fill_Rect(x + i*size, y + j*size, size, size, bg);
}
}
line >>= 1;
}
}
}
 
void SSD1331_Write(char c) {
if (c == '\n' || c == '\r') {
ssd1331_data_p->cursor_y += ssd1331_data_p->textsize * 8;
ssd1331_data_p->cursor_x = 0;
// } else if (c == '\r') {
// // skip em
} else {
SSD1331_Draw_Char(ssd1331_data_p->cursor_x, ssd1331_data_p->cursor_y, c, ssd1331_data_p->textcolor, ssd1331_data_p->textbgcolor, ssd1331_data_p->textsize);
ssd1331_data_p->cursor_x += ssd1331_data_p->textsize * 6;
if (ssd1331_data_p->wrap && (ssd1331_data_p->cursor_x > (ssd1331_data_p->_width - ssd1331_data_p->textsize * 6))) {
ssd1331_data_p->cursor_y += ssd1331_data_p->textsize * 8;
ssd1331_data_p->cursor_x = 0;
}
}
}
 
void SSD1331_Write_String(char* msg, char length) {
for (char i = 0; i < length; i++) {
SSD1331_Write(msg[i]);
}
}
 
//void SSD1331_Write_String(const rom char *fmt, ...) {
// unsigned char i, len;
// unsigned char buffer[SSD1331_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 > SSD1331_STRING_BUFFER_SIZE)
// len = SSD1331_STRING_BUFFER_SIZE;
//
// // Print buffer to string
// for (i = 0; i < len; i++) {
// SSD1331_Write(buffer[i]);
// }
//}
 
void SSD1331_Set_Cursor(int x, int y) {
ssd1331_data_p->cursor_x = x;
ssd1331_data_p->cursor_y = y;
}
 
void SSD1331_Set_Text_Color(unsigned int c) {
// for 'transparent' background, we'll set the bg
// to the same as fg instead of using a flag
ssd1331_data_p->textcolor = c;
ssd1331_data_p->textbgcolor = c;
}
 
void SSD1331_Set_Text_Color_BG(unsigned int c, unsigned int bg) {
ssd1331_data_p->textcolor = c;
ssd1331_data_p->textbgcolor = bg;
}
 
void SSD1331_Set_Text_Size(char s) {
ssd1331_data_p->textsize = (s > 0) ? s : 1;
}
 
void SSD1331_Set_Text_Wrap(char w) {
ssd1331_data_p->wrap = w;
}
 
void SSD1331_Set_Rotation(char x) {
x %= 4; // cant be higher than 3
ssd1331_data_p->rotation = x;
switch (x) {
case 0:
case 2:
ssd1331_data_p->_width = ssd1331_data_p->WIDTH;
ssd1331_data_p->_height = ssd1331_data_p->HEIGHT;
break;
case 1:
case 3:
ssd1331_data_p->_width = ssd1331_data_p->HEIGHT;
ssd1331_data_p->_height = ssd1331_data_p->WIDTH;
break;
}
}
 
unsigned int SSD1331_Color565(char r, char g, char b) {
unsigned int c;
c = r >> 3;
c <<= 6;
c |= g >> 2;
c <<= 5;
c |= b >> 3;
 
return c;
}
 
void SSD1331_Test_DrawLines(unsigned int color) {
int x, y;
SSD1331_Clear_Display();
for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
SSD1331_Draw_Line(0, 0, x, ssd1331_data_p->_height - 1, color);
}
for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
SSD1331_Draw_Line(0, 0, ssd1331_data_p->_width - 1, y, color);
}
 
SSD1331_Clear_Display();
for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
SSD1331_Draw_Line(ssd1331_data_p->_width - 1, 0, x, ssd1331_data_p->_height - 1, color);
}
for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
SSD1331_Draw_Line(ssd1331_data_p->_width - 1, 0, 0, y, color);
}
 
SSD1331_Clear_Display();
for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
SSD1331_Draw_Line(0, ssd1331_data_p->_height - 1, x, 0, color);
}
for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
SSD1331_Draw_Line(0, ssd1331_data_p->_height - 1, ssd1331_data_p->_width - 1, y, color);
}
 
SSD1331_Clear_Display();
for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
SSD1331_Draw_Line(ssd1331_data_p->_width - 1, ssd1331_data_p->_height - 1, x, 0, color);
}
for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
SSD1331_Draw_Line(ssd1331_data_p->_width - 1, ssd1331_data_p->_height - 1, 0, y, color);
}
}
 
void SSD1331_Test_DrawRect(unsigned int color) {
int x;
SSD1331_Clear_Display();
if (ssd1331_data_p->_height < ssd1331_data_p->_width) {
for (x = 0; x < ssd1331_data_p->_height - 1; x += 6) {
SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color);
}
} else {
for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color);
}
}
}
 
void SSD1331_Test_FillRect(unsigned int color1, unsigned int color2) {
int x;
SSD1331_Clear_Display();
if (ssd1331_data_p->_height < ssd1331_data_p->_width) {
for (x = ssd1331_data_p->_height - 1; x > 6; x -= 6) {
SSD1331_Fill_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color1);
SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color2);
}
} else {
for (x = ssd1331_data_p->_width - 1; x > 6; x -= 6) {
SSD1331_Fill_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color1);
SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color2);
}
}
}
 
void SSD1331_Test_DrawCircle(unsigned int radius, unsigned int color) {
int x, y;
for (x = 0; x < ssd1331_data_p->_width - 1 + radius; x += radius * 2) {
for (y = 0; y < ssd1331_data_p->_height - 1 + radius; y += radius * 2) {
SSD1331_Draw_Circle(x, y, radius, color);
}
}
}
 
void SSD1331_Test_FillCircle(unsigned int radius, unsigned int color) {
char x, y;
for (x = radius; x < ssd1331_data_p->_width - 1; x += radius * 2) {
for (y = radius; y < ssd1331_data_p->_height - 1; y += radius * 2) {
SSD1331_Fill_Circle(x, y, radius, color);
}
}
}
 
void SSD1331_Test_DrawTria(void) {
int color = 0xF800;
int t;
int w = ssd1331_data_p->_width / 2;
int x = ssd1331_data_p->_height;
int y = 0;
int z = ssd1331_data_p->_width;
SSD1331_Clear_Display();
for (t = 0; t <= 15; t += 1) {
SSD1331_Draw_Triangle(w, y, y, x, z, x, color);
x -= 4;
y += 4;
z -= 4;
color += 100;
}
}
 
void SSD1331_Test_DrawRoundRect(void) {
int color = 100;
int i, t, x, y, w, h;
SSD1331_Clear_Display();
for (t = 0; t <= 4; t += 1) {
x = 0;
y = 0;
w = ssd1331_data_p->_width;
h = ssd1331_data_p->_height;
for (i = 0; i <= 24; i += 1) {
SSD1331_Draw_Round_Rect(x, y, w, h, 5, color);
x += 2;
y += 3;
w -= 4;
h -= 6;
color += 1100;
}
color += 100;
}
}
 
void SSD1331_Test_MediaButtons(void) {
// play
SSD1331_Clear_Display();
SSD1331_Fill_Round_Rect(25, 10, 78, 60, 8, SSD1331_WHITE);
SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_RED);
Delay10KTCYx(100);
// pause
SSD1331_Fill_Round_Rect(25, 90, 78, 60, 8, SSD1331_WHITE);
SSD1331_Fill_Round_Rect(39, 98, 20, 45, 5, SSD1331_GREEN);
SSD1331_Fill_Round_Rect(69, 98, 20, 45, 5, SSD1331_GREEN);
Delay10KTCYx(100);
// play color
SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_BLUE);
Delay10KTCYx(100);
// pause color
SSD1331_Fill_Round_Rect(39, 98, 20, 45, 5, SSD1331_RED);
SSD1331_Fill_Round_Rect(69, 98, 20, 45, 5, SSD1331_RED);
// play color
SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_GREEN);
}
 
void SSD1331_Test_Pattern(void) {
char buffer[2];
unsigned int i, j;
SSD1331_GoTo(0, 0);
 
for (i = 0; i < 64; i++) {
for (j = 0; j < 96; j++) {
if (i > 55) {
buffer[0] = (SSD1331_WHITE >> 8);
buffer[1] = (SSD1331_WHITE);
} else if (i > 47) {
buffer[0] = (SSD1331_BLUE >> 8);
buffer[1] = (SSD1331_BLUE);
} else if (i > 39) {
buffer[0] = (SSD1331_GREEN >> 8);
buffer[1] = (SSD1331_GREEN);
} else if (i > 31) {
buffer[0] = (SSD1331_CYAN >> 8);
buffer[1] = (SSD1331_CYAN);
} else if (i > 23) {
buffer[0] = (SSD1331_RED >> 8);
buffer[1] = (SSD1331_RED);
} else if (i > 15) {
buffer[0] = (SSD1331_MAGENTA >> 8);
buffer[1] = (SSD1331_MAGENTA);
} else if (i > 7) {
buffer[0] = (SSD1331_YELLOW >> 8);
buffer[1] = (SSD1331_YELLOW);
} else {
buffer[0] = (SSD1331_BLACK >> 8);
buffer[1] = (SSD1331_BLACK);
}
SPI_DC_SELECT_LAT = 1; // D/C high (data)
SPI2_Write(buffer, 2);
}
}
}
/PIC Stuff/PICX_27J13/oled_ssd1331.h
0,0 → 1,123
#ifndef OLED_SSD1331_H
#define OLED_SSD1331_H
 
#define SSD1331_LCDWIDTH 96
#define SSD1331_LCDHEIGHT 64
//#define SSD1331_STRING_BUFFER_SIZE 64
 
// Select one of these defines to set the pixel color order
#define SSD1331_COLORORDER_RGB
// #define SSD1331_COLORORDER_BGR
 
// SSD1331 Commands
#define SSD1331_CMD_DRAWLINE 0x21
#define SSD1331_CMD_DRAWRECT 0x22
#define SSD1331_CMD_CLEARWINDOW 0x25
#define SSD1331_CMD_FILL 0x26
#define SSD1331_CMD_SETCOLUMN 0x15
#define SSD1331_CMD_SETROW 0x75
#define SSD1331_CMD_CONTRASTA 0x81
#define SSD1331_CMD_CONTRASTB 0x82
#define SSD1331_CMD_CONTRASTC 0x83
#define SSD1331_CMD_MASTERCURRENT 0x87
#define SSD1331_CMD_SETREMAP 0xA0
#define SSD1331_CMD_STARTLINE 0xA1
#define SSD1331_CMD_DISPLAYOFFSET 0xA2
#define SSD1331_CMD_NORMALDISPLAY 0xA4
#define SSD1331_CMD_DISPLAYALLON 0xA5
#define SSD1331_CMD_DISPLAYALLOFF 0xA6
#define SSD1331_CMD_INVERTDISPLAY 0xA7
#define SSD1331_CMD_SETMULTIPLEX 0xA8
#define SSD1331_CMD_SETMASTER 0xAD
#define SSD1331_CMD_DISPLAYOFF 0xAE
#define SSD1331_CMD_DISPLAYON 0xAF
#define SSD1331_CMD_POWERMODE 0xB0
#define SSD1331_CMD_PRECHARGE 0xB1
#define SSD1331_CMD_CLOCKDIV 0xB3
#define SSD1331_CMD_PRECHARGEA 0x8A
#define SSD1331_CMD_PRECHARGEB 0x8B
#define SSD1331_CMD_PRECHARGEC 0x8C
#define SSD1331_CMD_PRECHARGELEVEL 0xBB
#define SSD1331_CMD_VCOMH 0xBE
 
// Color definitions
#define SSD1331_BLACK 0x0000
#define SSD1331_BLUE 0x001F
#define SSD1331_RED 0xF800
#define SSD1331_GREEN 0x07E0
#define SSD1331_CYAN 0x07FF
#define SSD1331_MAGENTA 0xF81F
#define SSD1331_YELLOW 0xFFE0
#define SSD1331_WHITE 0xFFFF
 
typedef struct {
int WIDTH, HEIGHT; // raw display size
int _width, _height; // size depending on rotation
int cursor_x, cursor_y;
unsigned int textcolor, textbgcolor;
char textsize;
char rotation;
char wrap; // If set, wrap text at right side
} SSD1331_DATA;
 
// Misc functions
int SSD1331_Abs(int i);
void SSD1331_Swap(int *a, int *b);
unsigned int SSD1331_Color565(char r, char g, char b);
 
// Core functions
void SSD1331_Init(SSD1331_DATA *data);
void SSD1331_Begin(void);
void SSD1331_GoTo(int x, int y);
 
void SSD1331_Command(char c);
void SSD1331_Data(char d);
 
// Display functions
void SSD1331_Clear_Display(void);
 
void SSD1331_Draw_Pixel(int x, int y, unsigned int color);
void SSD1331_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color);
void SSD1331_Draw_Fast_VLine(int x, int y, int h, unsigned int color);
void SSD1331_Draw_Fast_HLine(int x, int y, int w, unsigned int color);
void SSD1331_Draw_Rect(int x0, int y0, int x1, int y1, unsigned int color);
void SSD1331_Fill_Rect(int x0, int y0, int x1, int y1, unsigned int color);
 
void SSD1331_Draw_Circle(int x0, int y0, int r, unsigned int color);
void SSD1331_Draw_Circle_Helper(int x0, int y0, int r, char cornername, unsigned int color);
void SSD1331_Fill_Circle(int x0, int y0, int r, unsigned int color);
void SSD1331_Fill_Circle_Helper(int x0, int y0, int r, char cornername, int delta, unsigned int color);
 
void SSD1331_Draw_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color);
void SSD1331_Fill_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color);
void SSD1331_Draw_Round_Rect(int x0, int y0, int w, int h, int radius, unsigned int color);
void SSD1331_Fill_Round_Rect(int x0, int y0, int w, int h, int radius, unsigned int color);
 
void SSD1331_Draw_Bitmap(int x, int y, const char *bitmap, int w, int h, unsigned int color);
void SSD1331_Draw_Char(int x, int y, char c, unsigned int color, unsigned int bg, char size);
 
void SSD1331_Write(char c);
void SSD1331_Write_String(char *msg, char length);
//void SSD1331_Write_String(const rom char *fmt, ...);
//void SSD1331_Append_String(const rom char *fmt, ...);
 
void SSD1331_Set_Cursor(int x, int y);
void SSD1331_Set_Text_Color(unsigned int c);
void SSD1331_Set_Text_Color_BG(unsigned int c, unsigned int bg);
void SSD1331_Set_Text_Size(char s);
void SSD1331_Set_Text_Wrap(char w);
void SSD1331_Set_Rotation(char r);
 
// Test functions
void SSD1331_Test_DrawLines(unsigned int color);
void SSD1331_Test_DrawRect(unsigned int color);
void SSD1331_Test_FillRect(unsigned int color1, unsigned int color2);
void SSD1331_Test_DrawCircle(unsigned int radius, unsigned int color);
void SSD1331_Test_FillCircle(unsigned int radius, unsigned int color);
void SSD1331_Test_DrawTria(void);
void SSD1331_Test_DrawRoundRect(void);
void SSD1331_Test_MediaButtons(void);
void SSD1331_Test_Pattern(void);
 
#endif /* OLED_SSD1331_H */
 
/PIC Stuff/PICX_27J13/spi.c
0,0 → 1,199
#include <xc.h>
#include <stdio.h>
#include <string.h>
#include "defines.h"
#include "spi.h"
#include "uart.h"
 
static SPI_DATA *spi_data_p;
 
void SPI2_Init(SPI_DATA *data, char speed) {
spi_data_p = data;
 
// Set up SPI2 with specified pins
RPINR22 = PPS_SPI2_CLK_IN; // SPI2 CLK Input
PPS_SPI2_CLK_OUT = 11; // SPI2 CLK Output
 
#ifndef SPI2_WRITE_ONLY
RPINR21 = PPS_SPI2_MISO; // SPI2 Data Input
SPI_MISO_TRIS = 1; // SPI2 data in pin (MISO)
#endif
SPI_CLK_TRIS = 0; // SPI2 clock pin
PPS_SPI2_MOSI = 10; // SPI2 Data Output (MOSI)
SPI_MOSI_TRIS = 0; // SPI2 data out pin (MOSI)
 
SPI_SLAVE_SELECT_TRIS = 0; // SPI2 slave select
SPI_SLAVE_SELECT_LAT = 1; // SPI2 SS high (Idle)
 
SPI_RESET_TRIS = 0; // SPI2 reset
SPI_RESET_LAT = 1; // SPI2 reset active low
 
SPI_DC_SELECT_TRIS = 0; // SPI2 D/C select
SPI_DC_SELECT_LAT = 0;
 
SSP2STATbits.SMP = 0; // Input is sampled in the middle of data output time
SSP2STATbits.CKE = 0; // Transmit occurs on transition from Idle to active clock state
 
SSP2CON1bits.SSPM = speed;
 
SSP2CON1bits.CKP = 1; // Idle state for clock is a high level
SSP2CON1bits.SSPEN = 1; // Enable MSSP module
 
#ifdef SPI2_USE_INTERRUPT
PIE3bits.SSP2IE = 1; // Enable MSSP2 interrupt
#else
PIE3bits.SSP2IE = 0;
#endif
 
#ifndef SPI2_WRITE_ONLY
spi_data_p->buffer_in_len = 0;
spi_data_p->buffer_in_read_ind = 0;
spi_data_p->buffer_in_write_ind = 0;
#endif
spi_data_p->buffer_out_ind = 0;
spi_data_p->buffer_out_len = 0;
}
 
void SPI2_Write(char *msg, unsigned int length) {
unsigned int i = 0;
#ifdef SPI2_USE_INTERRUPT
spi_data_p->buffer_out_len = length;
spi_data_p->buffer_out_ind = 1;
for (i = 0; i < length; i++) {
spi_data_p->buffer_out[i] = msg[i];
}
SPI_SLAVE_SELECT_LAT = 0; // Bring SS line low
SSP2BUF = spi_data_p->buffer_out[0]; // Transmit first byte
#else
SPI_SLAVE_SELECT_LAT = 0;
while (i != length) {
SSP2BUF = msg[i];
i++;
while (!SSP2STATbits.BF);
 
#ifndef SPI2_WRITE_ONLY
spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
spi_data_p->buffer_in_write_ind = 0;
} else {
spi_data_p->buffer_in_write_ind++;
}
spi_data_p->buffer_in_len++;
#else
// Read data in buffer to clear it
char tmp = SSP2BUF;
#endif
}
SPI_SLAVE_SELECT_LAT = 1;
#endif
}
 
void SPI2_Write_Repeat(char c, unsigned int length) {
#ifdef SPI2_USE_INTERRUPT
// TODO Implement interrupts for SPI2
#else
unsigned int i = 0;
SPI_SLAVE_SELECT_LAT = 0;
while (i != length) {
SSP2BUF = c;
i++;
while (!SSP2STATbits.BF);
 
#ifndef SPI2_WRITE_ONLY
spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
spi_data_p->buffer_in_write_ind = 0;
} else {
spi_data_p->buffer_in_write_ind++;
}
spi_data_p->buffer_in_len++;
#else
// Read data in buffer to clear it
char tmp = SSP2BUF;
#endif
}
SPI_SLAVE_SELECT_LAT = 1;
#endif
}
 
#ifndef SPI2_WRITE_ONLY
void SPI2_Recv_Interrupt_Handler() {
char c;
 
if (SSP2STATbits.BF) { // Check if data receive flag is set
if (spi_data_p->buffer_in_len == MAXSPIBUF - 1) {
char output[64];
sprintf(output, "SPI2: (ERROR) buffer overflow\r\n");
DBG_PRINT_SPI(output, strlen(output));
c = SSP2BUF; // Read SSP2BUF to clear it
} else {
// Save received data into buffer
spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
spi_data_p->buffer_in_write_ind = 0;
} else {
spi_data_p->buffer_in_write_ind++;
}
spi_data_p->buffer_in_len++;
 
// Put next byte in SSP2BUF for transmit
if (spi_data_p->buffer_out_ind != spi_data_p->buffer_out_len) {
SSP2BUF = spi_data_p->buffer_out[spi_data_p->buffer_out_ind];
spi_data_p->buffer_out_ind++;
} else {
SPI_SLAVE_SELECT_LAT = 1; // Bring SS line high
spi_data_p->buffer_out_ind = 0;
spi_data_p->buffer_out_len = 0;
}
}
}
}
 
void SPI2_Read(char length) {
#ifdef SPI2_USE_INTERRUPT
spi_data_p->buffer_out_len = length;
spi_data_p->buffer_out_ind = 1;
for (char i = 0; i < length; i++) {
spi_data_p->buffer_out[i] = 0x0;
}
SPI_SLAVE_SELECT_LAT = 0; // Bring SS line low
SSP2BUF = spi_data_p->buffer_out[0]; // Transmit first byte
#else
SPI_SLAVE_SELECT_LAT = 0;
 
for (char i = 0; i < length; i++) {
SSP2BUF = 0x0;
while (!SSP2STATbits.BF);
 
spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
spi_data_p->buffer_in_write_ind = 0;
} else {
spi_data_p->buffer_in_write_ind++;
}
spi_data_p->buffer_in_len++;
}
SPI_SLAVE_SELECT_LAT = 1;
#endif
}
 
char SPI2_Buffer_Len() {
return spi_data_p->buffer_in_len;
}
 
char SPI2_Read_Buffer(char* buffer) {
char i = 0;
while (spi_data_p->buffer_in_len != 0) {
buffer[i] = spi_data_p->buffer_in[spi_data_p->buffer_in_read_ind];
i++;
if (spi_data_p->buffer_in_read_ind == MAXSPIBUF - 1) {
spi_data_p->buffer_in_read_ind = 0;
} else {
spi_data_p->buffer_in_read_ind++;
}
spi_data_p->buffer_in_len--;
}
return i;
}
#endif
/PIC Stuff/PICX_27J13/spi.h
0,0 → 1,42
#ifndef SPI_H
#define SPI_H
 
#define MAXSPIBUF 64
 
#define SPI2_WRITE_ONLY
 
// Option to use interrupt. If interrupt are used, SPI write does not block but
// there is a longer delay between reading/writing data
//#define SPI2_USE_INTERRUPT
 
// SPI speed selection
#define SPI2_FOSC_64 0b0010
#define SPI2_FOSC_16 0b0001
#define SPI2_FOSC_8 0b1010
#define SPI2_FOSC_4 0b0000
 
typedef struct {
#ifndef SPI2_WRITE_ONLY
char buffer_in[MAXSPIBUF];
char buffer_in_read_ind;
char buffer_in_write_ind;
char buffer_in_len;
#endif
 
char buffer_out[MAXSPIBUF];
char buffer_out_ind;
char buffer_out_len;
} SPI_DATA;
 
void SPI2_Init(SPI_DATA *data, char speed);
void SPI2_Write(char *msg, unsigned int length);
void SPI2_Write_Repeat(char c, unsigned int length);
#ifndef SPI2_WRITE_ONLY
void SPI2_Recv_Interrupt_Handler(void);
void SPI2_Read(char length);
char SPI2_Buffer_Len(void);
char SPI2_Read_Buffer(char *buffer);
#endif
 
#endif /* SPI_H */
 
/PIC Stuff/PICX_27J13/temp_BMP085.c
0,0 → 1,190
#include <delays.h>
#include <math.h>
#include "defines.h"
#include "temp_BMP085.h"
#include "i2c.h"
 
 
static BMP085_DATA *bmp085_data_p;
 
void BMP_Init(BMP085_DATA *data) {
bmp085_data_p = data;
}
 
void BMP_Begin(char mode) {
if (mode > BMP085_ULTRAHIGHRES)
mode = BMP085_ULTRAHIGHRES;
bmp085_data_p->oversampling = mode;
 
if (BMP_Read8(0xD0) != 0x55) {
DBG_PRINT_BMP("Error contacting BMP085!\r\n");
return;
}
 
bmp085_data_p->ac1 = BMP_Read16(BMP085_CAL_AC1);
bmp085_data_p->ac2 = BMP_Read16(BMP085_CAL_AC2);
bmp085_data_p->ac3 = BMP_Read16(BMP085_CAL_AC3);
bmp085_data_p->ac4 = BMP_Read16(BMP085_CAL_AC4);
bmp085_data_p->ac5 = BMP_Read16(BMP085_CAL_AC5);
bmp085_data_p->ac6 = BMP_Read16(BMP085_CAL_AC6);
 
bmp085_data_p->b1 = BMP_Read16(BMP085_CAL_B1);
bmp085_data_p->b2 = BMP_Read16(BMP085_CAL_B2);
 
bmp085_data_p->mb = BMP_Read16(BMP085_CAL_MB);
bmp085_data_p->mc = BMP_Read16(BMP085_CAL_MC);
bmp085_data_p->md = BMP_Read16(BMP085_CAL_MD);
 
DBG_PRINT_BMP("AC1 = %d\r\n", bmp085_data_p->ac1);
DBG_PRINT_BMP("AC2 = %d\r\n", bmp085_data_p->ac2);
DBG_PRINT_BMP("AC3 = %d\r\n", bmp085_data_p->ac3);
DBG_PRINT_BMP("AC4 = %u\r\n", bmp085_data_p->ac4);
DBG_PRINT_BMP("AC5 = %u\r\n", bmp085_data_p->ac5);
DBG_PRINT_BMP("AC6 = %u\r\n", bmp085_data_p->ac6);
 
DBG_PRINT_BMP("B1 = %d\r\n", bmp085_data_p->b1);
DBG_PRINT_BMP("B2 = %d\r\n", bmp085_data_p->b2);
 
DBG_PRINT_BMP("MB = %d\r\n", bmp085_data_p->mb);
DBG_PRINT_BMP("MC = %d\r\n", bmp085_data_p->mc);
DBG_PRINT_BMP("MD = %d\r\n", bmp085_data_p->md);
}
 
unsigned int BMP_Read_Raw_Temperature() {
unsigned int ret;
 
BMP_Write8(BMP085_CONTROL, BMP085_READTEMPCMD);
Delay10KTCYx(255);
ret = BMP_Read16(BMP085_TEMPDATA);
DBG_PRINT_BMP("Raw Temp: %d\r\n", ret);
return ret;
}
 
unsigned long BMP_Read_Raw_Pressure() {
unsigned long ret;
 
BMP_Write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (bmp085_data_p->oversampling << 6));
 
if (bmp085_data_p->oversampling == BMP085_ULTRALOWPOWER)
Delay10KTCYx(255);
else if (bmp085_data_p->oversampling == BMP085_STANDARD)
Delay10KTCYx(255);
else if (bmp085_data_p->oversampling == BMP085_HIGHRES)
Delay10KTCYx(255);
else
Delay10KTCYx(255);
 
ret = BMP_Read16(BMP085_PRESSUREDATA);
ret <<= 8;
ret |= BMP_Read8(BMP085_PRESSUREDATA+2);
ret >>= (8 - bmp085_data_p->oversampling);
 
DBG_PRINT_BMP("Raw Pressure: %ld\r\n", ret);
 
return ret;
}
 
long BMP_Read_Pressure() {
long UT, UP, B3, B5, B6, X1, X2, X3, p;
unsigned long B4, B7;
 
UT = BMP_Read_Raw_Temperature();
UP = BMP_Read_Raw_Pressure();
 
// Temperature calculations
X1 = ((UT - (long) bmp085_data_p->ac6) * (long) bmp085_data_p->ac5) >> 15;
X2 = ((long) bmp085_data_p->mc << 11) - (X1 + bmp085_data_p->md) / 2; // round up
X2 /= (X1 + bmp085_data_p->md);
B5 = X1 + X2;
 
// Pressure calcs
B6 = B5 - 4000;
X1 = ((long) bmp085_data_p->b2 * ((B6 * B6) >> 12)) >> 11;
X2 = ((long) bmp085_data_p->ac2 * B6) >> 11;
X3 = X1 + X2;
B3 = ((((long) bmp085_data_p->ac1 * 4 + X3) << bmp085_data_p->oversampling) + 2) / 4;
 
X1 = ((long) bmp085_data_p->ac3 * B6) >> 13;
X2 = ((long) bmp085_data_p->b1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = ((unsigned long) bmp085_data_p->ac4 * (unsigned long) (X3 + 32768)) >> 15;
B7 = ((unsigned long) UP - B3) * (unsigned long) (50000UL >> bmp085_data_p->oversampling);
 
if (B7 < 0x80000000) {
p = (B7 * 2) / B4;
} else {
p = (B7 / B4) * 2;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * p) >> 16;
 
p = p + ((X1 + X2 + (long)3791)>>4);
 
return p;
}
 
float BMP_Read_Temperature() {
long UT, X1, X2, B5;
float temp;
 
UT = BMP_Read_Raw_Temperature();
 
X1 = ((UT - (long) bmp085_data_p->ac6) * (long) bmp085_data_p->ac5) >> 15;
X2 = ((long) bmp085_data_p->mc << 11) / (X1 + (long) bmp085_data_p->md);
B5 = X1 + X2;
temp = (B5 + 8) >> 4;
temp /= 10;
 
return temp;
}
 
float BMP_Read_Altitude(float seaLevelPressure) {
float altitude;
float pressure = BMP_Read_Pressure();
altitude = 44330 * (1.0 - pow(pressure /seaLevelPressure,0.1903));
return altitude;
}
 
char BMP_Read8(char a) {
char buffer[6], result, length, ret = 0;
I2C_Master_Restart(BMP085_I2CADDR, a, 1);
do {
result = I2C_Get_Status();
} while (!result);
length = I2C_Read_Buffer((char *)buffer);
ret = buffer[0];
 
return ret;
}
 
unsigned int BMP_Read16(char a) {
char buffer[6], result, length;
unsigned int ret;
 
I2C_Master_Restart(BMP085_I2CADDR, a, 2);
do {
result = I2C_Get_Status();
} while (!result);
length = I2C_Read_Buffer((char *)buffer);
ret = buffer[0];
ret <<= 8;
ret |= buffer[1];
 
return ret;
}
 
void BMP_Write8(char a, char d) {
char buffer[2], result;
buffer[0] = a;
buffer[1] = d;
 
I2C_Master_Send(BMP085_I2CADDR, 2, buffer);
do {
result = I2C_Get_Status();
} while (!result);
}
/PIC Stuff/PICX_27J13/temp_BMP085.h
0,0 → 1,47
#ifndef TEMP_BMP085_H
#define TEMP_BMP085_H
 
#define BMP085_I2CADDR 0x77
 
#define BMP085_ULTRALOWPOWER 0
#define BMP085_STANDARD 1
#define BMP085_HIGHRES 2
#define BMP085_ULTRAHIGHRES 3
#define BMP085_CAL_AC1 0xAA // R Calibration data (16 bits)
#define BMP085_CAL_AC2 0xAC // R Calibration data (16 bits)
#define BMP085_CAL_AC3 0xAE // R Calibration data (16 bits)
#define BMP085_CAL_AC4 0xB0 // R Calibration data (16 bits)
#define BMP085_CAL_AC5 0xB2 // R Calibration data (16 bits)
#define BMP085_CAL_AC6 0xB4 // R Calibration data (16 bits)
#define BMP085_CAL_B1 0xB6 // R Calibration data (16 bits)
#define BMP085_CAL_B2 0xB8 // R Calibration data (16 bits)
#define BMP085_CAL_MB 0xBA // R Calibration data (16 bits)
#define BMP085_CAL_MC 0xBC // R Calibration data (16 bits)
#define BMP085_CAL_MD 0xBE // R Calibration data (16 bits)
 
#define BMP085_CONTROL 0xF4
#define BMP085_TEMPDATA 0xF6
#define BMP085_PRESSUREDATA 0xF6
#define BMP085_READTEMPCMD 0x2E
#define BMP085_READPRESSURECMD 0x34
 
typedef struct {
int ac1, ac2, ac3, b1, b2, mb, mc, md;
unsigned int ac4, ac5, ac6;
char oversampling;
} BMP085_DATA;
 
void BMP_Init(BMP085_DATA *data);
void BMP_Begin(char mode);
unsigned int BMP_Read_Raw_Temperature(void);
unsigned long BMP_Read_Raw_Pressure(void);
float BMP_Read_Temperature(void);
long BMP_Read_Pressure(void);
float BMP_Read_Altitude(float seaLevelPressure);
 
char BMP_Read8(char a);
unsigned int BMP_Read16(char a);
void BMP_Write8(char a, char d);
 
#endif /* TEMP_BMP085_H */
 
/PIC Stuff/PICX_27J13/timers.c
0,0 → 1,36
#include <xc.h>
#include <delays.h>
#include "defines.h"
#include "timers.h"
 
void Timer1_Init(void) {
T1CONbits.TMR1CS = 0x2; // Clock source using T1OSC and T1CLK pins
T1CONbits.RD16 = 0x1; // Configure for 16-bit read/writes
T1CONbits.T1OSCEN = 0x1; // Enable crystal driver
PIE1bits.TMR1IE = 0x1; // Enable interrupt
 
// Non-applicable settings
T1CONbits.T1CKPS = 0x0; // 1:1 prescale value
T1CONbits.NOT_T1SYNC = 0x1; // No external sync
T1GCONbits.TMR1GE = 0x0; // Disable gate control
}
 
void Timer1_Enable(void) {
T1CONbits.TMR1ON = 1;
}
 
void Timer1_Disable(void) {
T1CONbits.TMR1ON = 0;
}
 
void Timer1_Interrupt_Handler(void) {
#ifdef _TEST_TIMER1_RTC
TMR1H = 0x7F;
TMR1L = 0xFF;
LED_BLUE_LAT = 1;
LED_RED_LAT = 1;
Delay10KTCYx(255);
LED_BLUE_LAT = 0;
LED_RED_LAT = 0;
#endif
}
/PIC Stuff/PICX_27J13/timers.h
0,0 → 1,9
#ifndef TIMERS_H
#define TIMERS_H
 
void Timer1_Init(void);
void Timer1_Enable(void);
void Timer1_Disable(void);
void Timer1_Interrupt_Handler(void);
 
#endif
/PIC Stuff/PICX_27J13/uart.c
4,13 → 4,10
#include "defines.h"
#include "uart.h"
 
static UART_DATA uart_1_data;
static UART_DATA *uart_1_data_p = &uart_1_data;
static UART_DATA *uart_1_data_p;
 
void UART1_Init() {
// Configure the hardware USART device
// UART1 TX RC6
// UART1 RX RC7
void UART1_Init(UART_DATA *data) {
uart_1_data_p = data;
 
UART1_TX_TRIS = 0; // Tx pin set to output
UART1_RX_TRIS = 1; // Rx pin set to input
41,8 → 38,10
uart_1_data_p->buffer_in_len_tmp = 0;
}
 
#pragma interrupt_level 0
void UART1_Recv_Interrupt_Handler() {
unsigned char c;
char c;
if (PIR1bits.RC1IF) { // Check if data receive flag is set
c = RCREG1;
#ifdef UART1_RX_TO_BUFFER
80,7 → 79,9
// We've overrun the USART and must reset
RCSTA1bits.CREN = 0; // Reset UART1
RCSTA1bits.CREN = 1;
DBG_PRINT_UART("UART1: (ERROR) overrun\r\n");
char output[64];
sprintf(output, "UART1: (ERROR) Overrun Occurred\r\n");
DBG_PRINT_UART(output, strlen(output));
TXSTA1bits.TXEN = 0; // Kill anything currently sending
}
}
98,39 → 99,71
}
}
 
void UART1_WriteS(const char *fmt, ...) {
#ifdef _DEBUG
unsigned char i;
va_list args;
va_start(args, fmt);
//void UART1_WriteS(const char *fmt, ...) {
//#ifdef _DEBUG
// char i;
// va_list args;
// va_start(args, fmt);
// vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
vprintf(fmt, args);
va_end(args);
uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
uart_1_data_p->buffer_out_ind = 1;
for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
TXREG1 = uart_1_data_p->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_p->buffer_out, fmt, args);
va_end(args);
uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
uart_1_data_p->buffer_out_ind = 1;
TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
TXSTA1bits.TXEN = 1; // Begin transmission
#endif
}
// va_end(args);
// uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
// uart_1_data_p->buffer_out_ind = 1;
// for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
// TXREG1 = uart_1_data_p->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_p->buffer_out, fmt, args);
// va_end(args);
// uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
// uart_1_data_p->buffer_out_ind = 1;
// TXREG1 = uart_1_data_p->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;
//void UART1_WriteF(float f, char m) {
// long whole = 0;
// unsigned long decimal = 0;
// unsigned int multiplier = 1;
// char i;
//
// for (i = 0; i < m; i++)
// multiplier *= 10;
//
// whole = (long)((float)f);
// decimal = (long)((float)f*multiplier) - whole*multiplier;
// // Round up if necessary
// if ((long)((float)f*multiplier*10) % 10 >= 5)
// decimal += 1;
//#ifdef _DEBUG
// sprintf((char *) uart_1_data_p->buffer_out, "%ld.%ld", whole, decimal);
// uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
// uart_1_data_p->buffer_out_ind = 1;
// for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
// TXREG1 = uart_1_data_p->buffer_out[i];
// Nop();
// while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
// }
//#else
// while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
// sprintf((char *) uart_1_data_p->buffer_out, "%ld.%ld", whole, decimal);
// uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
// uart_1_data_p->buffer_out_ind = 1;
// TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
// TXSTA1bits.TXEN = 1; // Begin transmission
//#endif
//}
 
void UART1_WriteS(char *string, char length) {
char i;
#ifdef _DEBUG
for (i = 0; i < length; i++) {
TXREG1 = msg[i];
TXREG1 = string[i];
Nop();
while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
}
139,7 → 172,7
uart_1_data_p->buffer_out_len = length;
uart_1_data_p->buffer_out_ind = 1;
for (i = 0; i < length; i++) {
uart_1_data_p->buffer_out[i] = msg[i];
uart_1_data_p->buffer_out[i] = string[i];
}
TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
TXSTA1bits.TXEN = 1; // Begin transmission
146,7 → 179,7
#endif
}
 
void UART1_WriteC(const unsigned char c) {
void UART1_WriteC(const char c) {
#ifdef _DEBUG
TXREG1 = c;
Nop();
161,13 → 194,13
 
}
 
unsigned char UART1_Buffer_Len() {
char UART1_Buffer_Len() {
return uart_1_data_p->buffer_in_len;
}
 
/* Reader interface to the UART buffer, returns the number of bytes read */
unsigned char UART1_Read_Buffer(unsigned char *buffer) {
unsigned char i = 0;
char UART1_Read_Buffer(char *buffer) {
char i = 0;
while (uart_1_data_p->buffer_in_len != 0) {
buffer[i] = uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_read_ind];
i++;
/PIC Stuff/PICX_27J13/uart.h
1,5 → 1,5
#ifndef __uart_h
#define __uart_h
#ifndef UART_H
#define UART_H
 
#define MAXUARTBUF 125
 
8,24 → 8,25
#define UART1_RECV_BUFFER
//#define UART1_RECV_XBEE
 
typedef struct __UART_DATA {
unsigned char buffer_in[MAXUARTBUF];
unsigned char buffer_in_read_ind;
unsigned char buffer_in_write_ind;
unsigned char buffer_in_len;
unsigned char buffer_in_len_tmp;
typedef struct {
char buffer_in[MAXUARTBUF];
char buffer_in_read_ind;
char buffer_in_write_ind;
char buffer_in_len;
char buffer_in_len_tmp;
 
unsigned char buffer_out[MAXUARTBUF];
unsigned char buffer_out_ind;
unsigned char buffer_out_len;
char buffer_out[MAXUARTBUF];
char buffer_out_ind;
char buffer_out_len;
} UART_DATA;
 
void UART1_Init(void);
void UART1_Init(UART_DATA *data);
void UART1_Recv_Interrupt_Handler(void);
void UART1_Send_Interrupt_Handler(void);
void UART1_WriteS(const char *fmt, ...);
void UART1_WriteB(const char *msg, unsigned char length);
void UART1_WriteC(const unsigned char c);
unsigned char UART1_Buffer_Len(void);
unsigned char UART1_Read_Buffer(unsigned char *buffer);
//void UART1_WriteS(const char *fmt, ...);
//void UART1_WriteF(float f, char m);
void UART1_WriteS(char *string, char length);
void UART1_WriteC(const char c);
char UART1_Buffer_Len(void);
char UART1_Read_Buffer(char *buffer);
#endif
/PIC Stuff/PICX_27J13/xbee.c
0,0 → 1,237
#include "defines.h"
#include "xbee.h"
#include <string.h>
 
#pragma udata XBEE_BUFFER
static XBEE_DATA xbee_data;
#pragma udata
static XBEE_DATA *xbee_data_p = &xbee_data;
static void *xbee_data_frame;
static void *xbee_frame;
 
/* Initialize variables used by this library */
void XBee_Init() {
XBEE_CTS_TRIS = 1; // RB0 is CTS, set by XBee chip
XBEE_RTS_TRIS = 0; // RB1 is RTS, set by PIC
 
XBEE_CTS_LAT = 0; // Pin set high to signal stop sending data to XBee
XBEE_RTS_LAT = 0; // Pin set high to indicate stop sending data to PIC
 
xbee_data_p->dataind = 0;
xbee_data_p->checksum_sum = 0;
xbee_data_p->frame_rdy = 0;
xbee_data_p->escape_flag = 0;
xbee_data_p->read_state = XBEE_STATE_READ_START;
 
// memset(&xbee_data, 0, 32);
// Grab a pointer to where the unique frame array starts
xbee_data_frame = &(xbee_data_p->rcv_frame.FRAME);
xbee_frame = &(xbee_data_p->rcv_frame);
}
 
/* Here we handle the serial input from the UART interrupt */
void XBee_Serial_In(unsigned char c) {
// For some reason writing the length straight to xbee_data doesnt seem to work
// so we work around it by pointing to the length bytes directly
XBEE_ADDRESS_16 *length = xbee_frame + 1;
#ifdef XBEE_USE_ESCAPE_CHAR
if (c == XBEE_ESCAPE_CHAR) {
// Next byte needs is an escaped char
xbee_data_p->escape_flag = 1;
return;
}
 
if (xbee_data_p->escape_flag) {
// XOR byte with 0x20 to get escaped char
c ^= XBEE_ESCAPE_VAL;
xbee_data_p->escape_flag = 0;
}
#endif
// Reset on start bit and start saving data
if (c == XBEE_START_DELIMITER) {
// On detect start delimiter, clear out initial array
xbee_data_p->dataind = 0;
xbee_data_p->checksum_sum = 0;
xbee_data_p->frame_rdy = 0;
xbee_data_p->read_state = XBEE_STATE_READ_LENGTH_HIGH;
*((unsigned char *)xbee_frame) = XBEE_START_DELIMITER;
} else {
switch (xbee_data_p->read_state) {
case XBEE_STATE_READ_START:
// Do nothing and wait till start bit is read
break;
case XBEE_STATE_READ_LENGTH_HIGH:
// Read length (MSB)
length->INT_16.char_value[1] = c;
xbee_data_p->read_state = XBEE_STATE_READ_LENGTH_LOW;
break;
case XBEE_STATE_READ_LENGTH_LOW:
// Read length (LSB)
length->INT_16.char_value[0] = c;
xbee_data_p->read_state = XBEE_STATE_READ_FRAME_DATA;
break;
case XBEE_STATE_READ_FRAME_DATA:
// Read unique frame data
if (xbee_data_p->dataind < xbee_data_p->rcv_frame.length.INT_16.int_value) {
*((char*) xbee_data_frame + xbee_data_p->dataind) = c;
xbee_data_p->checksum_sum += c;
xbee_data_p->dataind++;
}
// If total length is read, the next byte is the expected checksum
if (xbee_data_p->dataind == xbee_data_p->rcv_frame.length.INT_16.int_value) {
xbee_data_p->read_state = XBEE_STATE_READ_CHECKSUM;
}
break;
case XBEE_STATE_READ_CHECKSUM:
// Calculate and compare checksum
if (0xFF - xbee_data_p->checksum_sum == c) {
// Frame was recieved successfully
xbee_data_p->frame_rdy = 1;
// XBee_Process_Received_Frame();
} else {
// If checksum does not match, drop frame
DBG_PRINT_XBEE("XBEE: checksum mismatch\r\n");
}
xbee_data_p->read_state = XBEE_STATE_READ_START;
break;
}
}
}
 
/* This processes the frame data within the interrupt. Dont use this. */
void XBee_Process_Received_Frame() {
// DBG_PRINT_XBEE("Length: %d\r\n", xbee_data_p->rcv_frame.length.INT_16.int_value);
// Here we process the received frame depending on the frame type
switch (*((unsigned char *) xbee_data_frame)) {
case XBEE_RX_AT_COMMAND_RESPONSE:
DBG_PRINT_XBEE("XBEE: parsing recieved AT command response frame\r\n");
break;
case XBEE_RX_DATA_PACKET:
DBG_PRINT_XBEE("XBEE: parsing recieved data frame\r\n");
break;
case XBEE_RX_DATA_TX_STATUS:
DBG_PRINT_XBEE("XBEE: parsing recieved TX status frame\r\n");
break;
case XBEE_RX_IO_DATA_SAMPLE:
DBG_PRINT_XBEE("XBEE: parsing recieved IO data sample frame\r\n");
break;
case XBEE_RX_EXPLICIT_COMMAND:
DBG_PRINT_XBEE("XBEE: parsing recieved explicit command frame\r\n");
break;
case XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
DBG_PRINT_XBEE("XBEE: parsing recieved remote AT command frame\r\n");
break;
case XBEE_RX_ROUTE_RECORD:
DBG_PRINT_XBEE("XBEE: parsing recieved route record frame\r\n");
break;
case XBEE_RX_NODE_IDENTIFICATION:
DBG_PRINT_XBEE("XBEE: parsing recieved node identification frame\r\n");
break;
case XBEE_RX_FRAME_MODEM_STATUS:
DBG_PRINT_XBEE("XBEE: parsing recieved modem status frame\r\n");
break;
default:
DBG_PRINT_XBEE("XBEE: (ERROR) unrecognized frame type\r\n");
}
}
 
unsigned int XBee_Get_Received_Frame(unsigned char *frame) {
if (!xbee_data_p->frame_rdy) {
return 0;
} else {
memcpy(frame, xbee_data_frame, xbee_data_p->rcv_frame.length.INT_16.int_value);
xbee_data_p->frame_rdy = 0; // Reset frame ready status
return xbee_data_p->rcv_frame.length.INT_16.int_value;
}
}
 
void XBee_Process_Transmit_Frame(unsigned char *data, unsigned char length) {
#ifdef XBEE_USE_ESCAPE_CHAR
unsigned int i = 0;
unsigned char chksum = 0;
 
// Write the start bit and length
UART1_WriteC(XBEE_START_DELIMITER);
UART1_WriteC(0);
UART1_WriteC(length);
 
// Write the frame data
for (i = 0; i < length; i++) {
chksum += data[i];
if (data[i] == XBEE_START_DELIMITER || \
data[i] == XBEE_ESCAPE_CHAR || \
data[i] == XBEE_XON || \
data[i] == XBEE_XOFF) {
UART1_WriteC(XBEE_ESCAPE_CHAR);
UART1_WriteC(data[i] ^ XBEE_ESCAPE_VAL);
} else {
UART1_WriteC(data[i]);
}
}
// Write the checksum
if (chksum == XBEE_START_DELIMITER || \
chksum == XBEE_ESCAPE_CHAR || \
chksum == XBEE_XON || \
chksum == XBEE_XOFF) {
UART1_WriteC(XBEE_ESCAPE_CHAR);
UART1_WriteC(chksum ^ XBEE_ESCAPE_VAL);
} else {
UART1_WriteC(0xFF - chksum);
}
#else
unsigned int i = 0;
unsigned char chksum = 0;
 
UART1_WriteC(XBEE_START_DELIMITER);
UART1_WriteC(0);
UART1_WriteC(length);
for (i = 0; i < length; i++) {
chksum += data[i];
UART1_WriteC(data[i]);
}
UART1_WriteC(0xFF - chksum);
#endif
}
 
void XBee_Set_RTS(unsigned char c) {
if (c) {
XBEE_RTS_LAT = 1; // Set high to stop receiving data
} else {
XBEE_RTS_LAT = 0; // Set low to resume receiving data
}
}
 
unsigned char XBee_Read_CTS() {
unsigned char c = XBEE_CTS_PORT;
if (c) {
return 0x1; // High indicates stop sending data
} else {
return 0x0; // Low indicates ok to send data
}
}
 
void XBee_Convert_Endian_64(XBEE_ADDRESS_64 *src) {
char tmp[2];
tmp[0] = src->UPPER_32.char_value[3];
tmp[1] = src->UPPER_32.char_value[2];
src->UPPER_32.char_value[3] = src->UPPER_32.char_value[0];
src->UPPER_32.char_value[2] = src->UPPER_32.char_value[1];
src->UPPER_32.char_value[1] = tmp[1];
src->UPPER_32.char_value[0] = tmp[0];
 
tmp[0] = src->LOWER_32.char_value[3];
tmp[1] = src->LOWER_32.char_value[2];
src->LOWER_32.char_value[3] = src->LOWER_32.char_value[0];
src->LOWER_32.char_value[2] = src->LOWER_32.char_value[1];
src->LOWER_32.char_value[1] = tmp[1];
src->LOWER_32.char_value[0] = tmp[0];
}
 
void XBee_Convert_Endian_16(XBEE_ADDRESS_16 *src) {
char tmp;
tmp = src->INT_16.char_value[0];
src->INT_16.char_value[0] = src->INT_16.char_value[1];
src->INT_16.char_value[1] = tmp;
}
/PIC Stuff/PICX_27J13/xbee.h
0,0 → 1,269
#ifndef XBEE_H
#define XBEE_H
 
#define XBEE_BUFFER_SIZE 227
 
// If API mode = 2 is enabled
#define XBEE_USE_ESCAPE_CHAR
 
#define XBEE_ESCAPE_VAL 0x20
#define XBEE_START_DELIMITER 0x7E
#define XBEE_ESCAPE_CHAR 0x7D
#define XBEE_XON 0x11
#define XBEE_XOFF 0x13
 
// Expected 'next' state
#define XBEE_STATE_READ_START 10
#define XBEE_STATE_READ_LENGTH_HIGH 11
#define XBEE_STATE_READ_LENGTH_LOW 12
#define XBEE_STATE_READ_FRAME_DATA 13
#define XBEE_STATE_READ_CHECKSUM 14
 
// Command Frame Type
#define XBEE_TX_AT_COMMAND 0x08
#define XBEE_TX_AT_COMMAND_QUEUE 0x09
#define XBEE_RX_AT_COMMAND_RESPONSE 0x88
 
#define XBEE_TX_DATA_PACKET 0x10
#define XBEE_RX_DATA_PACKET 0x90
#define XBEE_RX_DATA_TX_STATUS 0x8B
#define XBEE_RX_IO_DATA_SAMPLE 0x92
#define XBEE_TX_EXPLICIT_COMMAND 0x11
#define XBEE_RX_EXPLICIT_COMMAND 0x91
 
#define XBEE_TX_REMOTE_AT_COMMAND 0x17
#define XBEE_RX_REMOTE_AT_COMMAND_RESPONSE 0x97
 
#define XBEE_TX_CREATE_SOURCE_ROUTE 0x21
#define XBEE_RX_ROUTE_RECORD 0xA1
#define XBEE_RX_NODE_IDENTIFICATION 0x95
#define XBEE_RX_FRAME_MODEM_STATUS 0x8A
 
typedef struct {
union {
unsigned long long_value;
unsigned char char_value[4]; // Little Endian!!
} UPPER_32;
union {
unsigned long long_value;
unsigned char char_value[4]; // Little Endian!!
} LOWER_32;
} XBEE_ADDRESS_64;
 
typedef struct {
union {
unsigned int int_value;
unsigned char char_value[2]; // Little Endian!!
} INT_16;
} XBEE_ADDRESS_16;
 
// Unique Frame Components
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
unsigned char command[2];
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_TX_AT_COMMAND_FRAME;
#define XBEE_TX_AT_COMMAND_FRAME_SIZE 4
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
unsigned char command[2];
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_TX_AT_COMMAND_QUEUE_FRAME;
#define XBEE_TX_AT_COMMAND_QUEUE_FRAME_SIZE 4
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
unsigned char command[2];
unsigned char command_status;
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_RX_AT_COMMAND_RESPONSE_FRAME;
#define XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE 5
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
XBEE_ADDRESS_64 destination_64;
XBEE_ADDRESS_16 destination_16;
unsigned char broadcast_radius;
unsigned char options;
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_TX_DATA_PACKET_FRAME;
#define XBEE_TX_DATA_PACKET_FRAME_SIZE 14
 
typedef struct {
unsigned char frame_type;
XBEE_ADDRESS_64 source_64;
XBEE_ADDRESS_16 source_16;
unsigned char recieve_options;
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_RX_DATA_PACKET_FRAME;
#define XBEE_RX_DATA_PACKET_FRAME_SIZE 12
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
XBEE_ADDRESS_16 destination_16;
unsigned char transmit_retry_count;
unsigned char delivery_status;
unsigned char discovery_status;
} XBEE_RX_DATA_TX_STATUS_FRAME;
#define XBEE_RX_DATA_TX_STATUS_FRAME_SIZE 7
 
typedef struct {
unsigned char frame_type;
XBEE_ADDRESS_64 source_64;
XBEE_ADDRESS_16 source_16;
unsigned char recieve_options;
unsigned char number_of_samples;
unsigned char digital_ch_mask[2];
unsigned char analog_ch_mask;
unsigned char digital_samples[2];
unsigned char analog_samples[8];
} XBEE_RX_IO_DATA_SAMPLE_FRAME;
#define XBEE_RX_IO_DATA_SAMPLE_FRAME_SIZE 26
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
XBEE_ADDRESS_64 destination_64;
XBEE_ADDRESS_16 destination_16;
unsigned char source_endpoint;
unsigned char destination_endpoint;
unsigned char cluster_id[2];
unsigned char profile_id[2];
unsigned char broadcast_radius;
unsigned char transmit_options;
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_TX_EXPLICIT_COMMAND_FRAME;
#define XBEE_TX_EXPLICIT_COMMAND_FRAME_SIZE 20
 
typedef struct {
unsigned char frame_type;
XBEE_ADDRESS_64 source_64;
XBEE_ADDRESS_16 source_16;
unsigned char source_endpoint;
unsigned char destination_endpoint;
unsigned char cluster_id[2];
unsigned char profile_id[2];
unsigned char recieve_options;
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_RX_EXPLICIT_COMMAND_FRAME;
#define XBEE_RX_EXPLICIT_COMMAND_FRAME_SIZE 18
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
XBEE_ADDRESS_64 destination_64;
XBEE_ADDRESS_16 destination_16;
unsigned char remote_options;
unsigned char command[2];
unsigned char data[XBEE_BUFFER_SIZE];
} XBEE_TX_REMOTE_AT_COMMAND_FRAME;
#define XBEE_TX_REMOTE_AT_COMMAND_FRAME_SIZE 15
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
XBEE_ADDRESS_64 source_64;
XBEE_ADDRESS_16 source_16;
unsigned char command[2];
unsigned char command_status;
unsigned char command_data[4];
} XBEE_RX_REMOTE_AT_COMMAND_FRAME;
#define XBEE_RX_REMOTE_AT_COMMAND_FRAME_SIZE 19
 
typedef struct {
unsigned char frame_type;
unsigned char frame_id;
XBEE_ADDRESS_64 destination_64;
XBEE_ADDRESS_16 destination_16;
unsigned char route_options;
unsigned char num_of_addresses;
unsigned char addresses[XBEE_BUFFER_SIZE];
} XBEE_TX_CREATE_SOURCE_ROUTE_FRAME;
#define XBEE_TX_CREATE_SOURCE_ROUTE_FRAME_SIZE 14
 
typedef struct {
unsigned char frame_type;
XBEE_ADDRESS_64 source_64;
XBEE_ADDRESS_16 source_16;
unsigned char recieve_options;
unsigned char num_of_addresses;
unsigned char addresses[XBEE_BUFFER_SIZE];
} XBEE_RX_ROUTE_RECORD_FRAME;
#define XBEE_RX_ROUTE_RECORD_FRAME_SIZE 13
 
typedef struct {
unsigned char frame_type;
XBEE_ADDRESS_64 source_64;
XBEE_ADDRESS_16 source_16;
unsigned char recieve_options;
XBEE_ADDRESS_16 remote_16;
XBEE_ADDRESS_64 remote_64;
unsigned char NI_string[2];
XBEE_ADDRESS_16 parent_16;
unsigned char device_type;
unsigned char source_event;
unsigned char profile_id[2];
unsigned char manufacturer_id[2];
} XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME;
#define XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME_SIZE 32
 
typedef struct {
unsigned char frame_type;
unsigned char status;
} XBEE_RX_MODEM_STATUS_FRAME;
#define XBEE_RX_MODEM_STATUS_FRAME_SIZE 2
 
// Common Frame Components
typedef struct __XBEE_FRAME {
unsigned char start_delimiter;
XBEE_ADDRESS_16 length;
union {
XBEE_TX_AT_COMMAND_FRAME TX_AT_COMMAND;
XBEE_TX_AT_COMMAND_QUEUE_FRAME TX_AT_COMMAND_QUEUE;
XBEE_RX_AT_COMMAND_RESPONSE_FRAME RX_AT_COMMAND_RESPONSE;
XBEE_TX_DATA_PACKET_FRAME TX_DATA_PACKET;
XBEE_RX_DATA_PACKET_FRAME RX_DATA_PACKET;
XBEE_RX_DATA_TX_STATUS_FRAME RX_DATA_TX_STATUS;
XBEE_RX_IO_DATA_SAMPLE_FRAME RX_IO_DATA_SAMPLE;
XBEE_TX_EXPLICIT_COMMAND_FRAME TX_EXPLICIT_COMMAND;
XBEE_RX_EXPLICIT_COMMAND_FRAME RX_EXPLICIT_COMMAND;
XBEE_TX_REMOTE_AT_COMMAND_FRAME TX_REMOTE_AT_COMMAND;
XBEE_RX_REMOTE_AT_COMMAND_FRAME RX_REMOTE_AT_COMMAND;
XBEE_TX_CREATE_SOURCE_ROUTE_FRAME TX_CREATE_SOURCE_ROUTE;
XBEE_RX_ROUTE_RECORD_FRAME RX_ROUTE_RECORD;
XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME RX_NODE_IDENTIFICATION;
XBEE_RX_MODEM_STATUS_FRAME RX_MODEM_STATUS;
} FRAME;
} XBEE_FRAME;
 
// Overall Data Structure
typedef struct __xbee_data {
XBEE_FRAME rcv_frame;
unsigned int dataind;
unsigned char checksum_sum;
unsigned char read_state;
unsigned char frame_rdy;
unsigned char escape_flag;
} XBEE_DATA;
 
 
void XBee_Init(void);
void XBee_Serial_In(unsigned char);
void XBee_Process_Received_Frame(void);
void XBee_Process_Transmit_Frame(unsigned char *data, unsigned char length);
 
unsigned int XBee_Get_Received_Frame(unsigned char *frame);
 
void XBee_Set_RTS(unsigned char);
unsigned char XBee_Read_CTS(void);
 
void XBee_Convert_Endian_64(XBEE_ADDRESS_64 *src);
void XBee_Convert_Endian_16(XBEE_ADDRESS_16 *src);
 
#endif