3,7 → 3,7 |
|
// 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 WDTE = OFF // Watchdog Timer Enable (WDT software controlled) |
#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) |
23,61 → 23,65 |
|
#include "defines.h" |
#include "INTERRUPTS.h" |
#include "IO.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; |
persistent uint8_t op_state; |
|
// Enable weak pull-up if WPU bit is set |
OPTION_REGbits.nWPUEN = 0; |
void Reset_Board(uint8_t next_state) { |
op_state = next_state; |
RESET(); |
} |
|
// Initialize interrupt inputs |
LSM303_INT_TRIS = 1; |
L3GD20_INT_TRIS = 1; |
BTN_INT_TRIS = 1; |
uint8_t Get_Last_Reset(void) { |
uint8_t ret; |
if (PCONbits.nPOR == 0) { |
ret = RESET_POR; |
} else if (PCONbits.nBOR == 0) { |
ret = RESET_BOR; |
} else if (STATUSbits.nTO == 0) { |
ret = RESET_WDT; |
} else if (PCONbits.nRMCLR == 0) { |
ret = RESET_MCLR; |
} else if (PCONbits.STKOVF || PCONbits.STKUNF) { |
ret = RESET_STK; |
} else if (PCONbits.nRI == 0) { |
ret = RESET_RST; |
} else { |
ret = RESET_POR; |
} |
|
// 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; |
PCON = 0x0F; |
STATUSbits.nPD = 1; |
STATUSbits.nTO = 1; |
return ret; |
} |
|
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; |
|
uint8_t ret = I2C1_SLAVE_PREFIX; |
if (!I2C_ADDR_3_PORT) |
ret |= 0x08; |
if (!I2C_ADDR_2_PORT) |
ret |= 0x04; |
if (!I2C_ADDR_1_PORT) |
ret |= 0x02; |
if (!I2C_ADDR_0_PORT) |
ret |= 0x01; |
|
return ret; |
} |
|
BTN_STATUS btns; |
|
int main(void) { |
uint8_t buffer[32]; |
uint8_t result, length; |
uint8_t i2c_slave_addr; |
// uint8_t op_state; |
uint8_t i; |
|
// Set internal oscillator speed to 32MHz |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits) |
84,15 → 88,24 |
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(); |
IO_Init(); |
|
uint8_t last_reset = Get_Last_Reset(); |
if (last_reset == RESET_POR || last_reset == RESET_BOR || |
last_reset == RESET_MCLR || last_reset == RESET_WDT || |
last_reset == RESET_STK) { |
op_state = OP_STATE_IDLE; |
} |
// if (last_reset == RESET_RST) { |
// op_state = OP_STATE_ACTIVE; |
// } |
|
i2c_slave_addr = Read_Address(); |
|
// Delay a bit to allow I2C lines to stabilize |
__delay_ms(1); |
|
// Initialize I2C1 in slave mode |
I2C1_DATA i2c1_data; |
I2C1_Init(&i2c1_data); |
107,31 → 120,36 |
Interrupt_Init(); |
Interrupt_Enable(); |
|
MCP23009_Init(&btns); |
MCP23009_Query(); |
|
LED_VALUES leds = {0x00}; |
TLC59116_Init(); |
MCP23009_Init(); |
// TLC59116_Write_All(&leds); |
|
BTN_STATUS btns; |
LED_VALUES leds = {0x00, 0x00, 0x00, 0x00, |
0x00, 0x00, 0x00, 0x00, |
0x00, 0x00, 0x00, 0x00, |
0x00, 0x00, 0x00, 0x00}; |
if (op_state == OP_STATE_IDLE) { |
IO_IOC_Enable(); |
Idle_Animation(); |
} else if (op_state == OP_STATE_ACTIVE) { |
while (1) { |
__delay_ms(1); |
// Read value of buttons |
MCP23009_Query(); |
|
TLC59116_Write_All(&leds); |
|
Idle_Animation(); |
|
// Check for received data over I2C1 |
while (1) { |
MCP23009_Query(&btns); |
uint8_t i; |
for (i = 0; i < 8; i++) { |
if ((btns.w >> i) & 0x1) { |
leds.w[i] = 0x00; |
} else { |
leds.w[i] = 0x20; |
// Check if an I2C message was received |
result = I2C1_Get_Status(); |
if (result) { |
length = I2C1_Read_Buffer(buffer); |
if (length == 1 && buffer[0] == CMD_RESET) { |
Reset_Board(OP_STATE_IDLE); |
} else if (length == 17 && buffer[0] == CMD_SET_LEDS) { |
for (i = 0; i < 16; i++) { |
leds.w[i] = buffer[i+1]; |
} |
TLC59116_Write_All(&leds); |
} |
} |
} |
TLC59116_Write_All(&leds); |
} |
} |
|