0,0 → 1,146 |
// <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/O |
ANSELA = 0x0; |
ANSELB = 0x0; |
ANSELC = 0x0; |
|
// Enable weak pull-up if WPU bit is set |
OPTION_REGbits.nWPUEN = 0; |
|
// Initialize interrupt inputs |
LSM303_INT_TRIS = 1; |
L3GD20_INT_TRIS = 1; |
BTN_INT_TRIS = 1; |
|
// Initialize UART pins |
UART_RX_TRIS = 1; |
UART_TX_TRIS = 0; |
|
// Initialize I2C address pins |
I2C_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 pins |
I2C_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 structure |
void Pins_Read(BTN_STATUS *btns) { |
|
} |
|
// Function to write led values from the data structure |
void 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 32MHz |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits) |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz |
OSCCONbits.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/O |
Pins_Init(); |
|
i2c_slave_addr = Read_Address(); |
|
// Initialize I2C1 in slave mode |
I2C1_DATA i2c1_data; |
I2C1_Init(&i2c1_data); |
I2C1_Configure_Slave(i2c_slave_addr); |
|
// Initialize I2C2 in master mode |
I2C2_DATA i2c2_data; |
I2C2_Init(&i2c2_data); |
I2C2_Configure_Master(I2C_400KHZ); |
|
// Initialize interrupts |
Interrupt_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 I2C |
while (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); |
|
|
} |
} |
|