Blame | Last modification | View Log | Download | RSS feed
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">// PIC16F1825 Configuration Bit Settings// CONFIG1#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is digital input)#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)// CONFIG2#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)// </editor-fold>#include "defines.h"#include "INTERRUPTS.h"#include "I2C1.h"#include "I2C2.h"#include "TLC59116.h"#include "MCP23009.h"void Pins_Init(void) {// Set all pins to digital I/OANSELA = 0x0;ANSELB = 0x0;ANSELC = 0x0;// Enable weak pull-up if WPU bit is setOPTION_REGbits.nWPUEN = 0;// Initialize interrupt inputsLSM303_INT_TRIS = 1;L3GD20_INT_TRIS = 1;BTN_INT_TRIS = 1;// Initialize UART pinsUART_RX_TRIS = 1;UART_TX_TRIS = 0;// Initialize I2C address pinsI2C_ADDR_0_TRIS = 1;I2C_ADDR_1_TRIS = 1;I2C_ADDR_2_TRIS = 1;I2C_ADDR_3_TRIS = 1;// Enable the weak-pullup on the address pinsI2C_ADDR_0_WPU = 1;I2C_ADDR_1_WPU = 1;I2C_ADDR_2_WPU = 1;I2C_ADDR_3_WPU = 1;// Initialize I2C pins (dont really need to as the I2C code does it)I2C_1_CLK_TRIS = 1;I2C_1_DAT_TRIS = 1;I2C_2_CLK_TRIS = 1;I2C_2_DAT_TRIS = 1;}uint8_t Read_Address(void) {uint8_t ret = 0;ret |= I2C_ADDR_3_LAT << 3;ret |= I2C_ADDR_2_LAT << 2;ret |= I2C_ADDR_1_LAT << 1;ret |= I2C_ADDR_0_LAT;return ret;}// Function to read button status into a data structurevoid Pins_Read(BTN_STATUS *btns) {}// Function to write led values from the data structurevoid Leds_Write(LED_STATUS *leds) {}int main(void) {uint8_t buffer[32];uint8_t result, length;uint8_t i2c_slave_addr;// Set internal oscillator speed to 32MHzOSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHzOSCCONbits.SCS = 0b00; // System clock determined by config bits// Set watchdog timer to reset device every 1s// CLRWDT is issued upon receiving data over I2C// WDTCON = 0x0A;// Initialize I/OPins_Init();i2c_slave_addr = Read_Address();// Initialize I2C1 in slave modeI2C1_DATA i2c1_data;I2C1_Init(&i2c1_data);I2C1_Configure_Slave(i2c_slave_addr);// Initialize I2C2 in master modeI2C2_DATA i2c2_data;I2C2_Init(&i2c2_data);I2C2_Configure_Master(I2C_400KHZ);// Initialize interruptsInterrupt_Init();Interrupt_Enable();TLC59116_Init();uint8_t leds[16] = {0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,0x10, 0x10, 0x10, 0x10,0x10, 0x10, 0x10, 0x10};TLC59116_Write_All(leds);MCP23009_Init();// Check for received data over I2Cwhile (1) {uint8_t btn_value = MCP23009_Query();uint8_t i;for (i = 0; i < 8; i++) {if ((btn_value >> i) & 0x1) {leds[i] = 0x00;} else {leds[i] = 0x10;}}TLC59116_Write_All(leds);}}