Rev 260 | Go to most recent revision | Blame | Last modification | View Log | 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/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;
}
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();
MCP23009_Init();
BTN_STATUS btns;
LED_VALUES leds = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
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;
}
}
TLC59116_Write_All(&leds);
}
}
void Idle_Animation(void) {
LED_VALUES leds = {0};
uint8_t led_direction_bar[8] = {1,0,0,0,0,0,0,0};
uint8_t led_direction_dir[8] = {1,0,0,0};
uint8_t led_direction_ind[8] = {1,0,0,0};
uint8_t led_8_high_thres = 0x80; // Max brightness of the middle section
uint8_t led_8_next_thresh = 0x40; // Threshold to start the next LED
uint8_t led_4_high_thres = 0x80; // Max brightness of the side sections
uint8_t led_4_next_thresh = 0x74; // Threshold to start the next LED
uint8_t i, next_led;
for (i = 0; i < 16; i++) leds.w[i] = 0x00;
while (1) {
// Update the LEDs in the middle section
for (i = 0; i < 8; i++) {
// Change the LED brightness depending on its direction
if (led_direction_bar[i] == 1) {
leds.w[i]++;
} else if (led_direction_bar[i] == 2) {
leds.w[i]--;
}
// Change direction if peak brightness is reached
// When the brightness reaches a middle threshold, start
// increasing the brightness of the next LED
if (led_direction_bar[i] == 1 && leds.w[i] == led_8_high_thres) {
led_direction_bar[i] = 2;
} else if (led_direction_bar[i] == 1 && leds.w[i] == led_8_next_thresh) {
next_led = (i == 7) ? 0 : i + 1;
led_direction_bar[next_led] = 1;
} else if (led_direction_bar[i] == 2 && leds.w[i] == 0x00) {
led_direction_bar[i] = 0;
}
}
// Update the LEDs in the right section
for (i = 0; i < 4; i++) {
// Change the LED brightness depending on its direction
if (led_direction_dir[i] == 1) {
leds.w[i+8]++;
} else if (led_direction_dir[i] == 2) {
leds.w[i+8]--;
}
// Change direction if peak brightness is reached
// When the brightness reaches a middle threshold, start
// increasing the brightness of the next LED
if (led_direction_dir[i] == 1 && leds.w[i+8] == led_4_high_thres) {
led_direction_dir[i] = 2;
} else if (led_direction_dir[i] == 1 && leds.w[i+8] == led_4_next_thresh) {
next_led = (i == 3) ? 0 : i + 1;
led_direction_dir[next_led] = 1;
} else if (led_direction_dir[i] == 2 && leds.w[i+8] == 0x00) {
led_direction_dir[i] = 0;
}
}
// Update the LEDs in the left section
for (i = 0; i < 4; i++) {
// Change the LED brightness depending on its direction
if (led_direction_ind[i] == 1) {
leds.w[i+12]++;
} else if (led_direction_ind[i] == 2) {
leds.w[i+12]--;
}
// Change direction if peak brightness is reached
// When the brightness reaches a middle threshold, start
// increasing the brightness of the next LED
if (led_direction_ind[i] == 1 && leds.w[i+12] == led_4_high_thres) {
led_direction_ind[i] = 2;
} else if (led_direction_ind[i] == 1 && leds.w[i+12] == led_4_next_thresh) {
next_led = (i == 3) ? 0 : i + 1;
led_direction_ind[next_led] = 1;
} else if (led_direction_ind[i] == 2 && leds.w[i+12] == 0x00) {
led_direction_ind[i] = 0;
}
}
// Write the LED values to the controller
TLC59116_Write_All(&leds);
// Delay a bit to slow down the animation
__delay_ms(1);
}
}