Rev 236 | Blame | Compare with Previous | 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 = ON // Watchdog Timer Enable (WDT enabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = OFF // 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 "base_INTERRUPTS.h"#include "base_I2C.h"void Pins_Init(void) {// Set all pins to digital I/OANSELA = 0x0;ANSELC = 0x0;// Enable weak pull-up if WPU bit is setOPTION_REGbits.nWPUEN = 0;// Set buttons as inputs and enable weak pull-upsBTN_L_N_TRIS = 1;BTN_L_N_WPU = 1;BTN_L_S_TRIS = 1;BTN_L_S_WPU = 1;BTN_R_N_TRIS = 1;BTN_R_N_WPU = 1;BTN_R_E_TRIS = 1;BTN_R_E_WPU = 1;BTN_R_S_TRIS = 1;BTN_R_S_WPU = 1;BTN_R_W_TRIS = 1;BTN_R_W_WPU = 1;// Set leds as outputs and initialize to offLED_1_TRIS = 0;LED_1_LAT = 0;LED_2_TRIS = 0;LED_2_LAT = 0;LED_3_TRIS = 0;LED_3_LAT = 0;LED_4_TRIS = 0;LED_4_LAT = 0;}// Function to read button status into a data structurevoid Pins_Read(BTN_STATUS *btns) {btns->BTN_L_N = BTN_L_N_PORT;btns->BTN_L_S = BTN_L_S_PORT;btns->BTN_R_N = BTN_R_N_PORT;btns->BTN_R_E = BTN_R_E_PORT;btns->BTN_R_S = BTN_R_S_PORT;btns->BTN_R_W = BTN_R_W_PORT;}// Function to write led values from the data structurevoid Leds_Write(LED_STATUS *leds) {LED_1_LAT = leds->LED_1;LED_2_LAT = leds->LED_2;LED_3_LAT = leds->LED_3;LED_4_LAT = leds->LED_4;}// TODO: Set watchdog timer to reset device if no I2C messages are received every x seconds// TODO: Use a timer to manually PWM the LEDsint main(void) {uint8_t buffer[32];uint8_t result, length;// 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 I2CWDTCON = 0x0A;// Initialize I/OPins_Init();// Initialize I2C in slave modeI2C_DATA i2c_data;I2C_Init(&i2c_data);I2C_Configure_Slave(0x25);// Initialize interruptsInterrupt_Init();Interrupt_Enable();// Check for received data over I2Cwhile (1) {do {result = I2C_Get_Status();} while (!result);CLRWDT();length = I2C_Read_Buffer(buffer);if (length == 2 && buffer[0] == CMD_SET_LEDS) {LED_STATUS leds;leds.value = buffer[1];Leds_Write(&leds);}}}