Subversion Repositories Code-Repo

Rev

Rev 230 | 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 = 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"

__persistent int temp;

void Pins_Init(void) {
    // Set all pins to digital I/O
    ANSELA = 0x0;
    ANSELC = 0x0;

    // Enable weak pull-up if WPU bit is set
    OPTION_REGbits.nWPUEN = 0;

    // Set buttons as inputs and enable weak pull-ups
    BTN_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 off
    LED_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 structure
void 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 structure
void 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;
}

int main(void) {
    char buffer[32];
    char result, length;

    // 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

    // Initialize I/O
    Pins_Init();

    // Initialize I2C in slave mode
    I2C_DATA i2c_data;
    I2C_Init(&i2c_data);
    I2C_Configure_Slave(0x25);

    // Initialize interrupts
    Interrupt_Init();
    Interrupt_Enable();

    while (1) {
        do {
            result = I2C_Get_Status();
        } while (!result);
        length = I2C_Read_Buffer(buffer);
        if (length == 2 && buffer[0] == CMD_SET_LEDS) {
            LED_STATUS leds;
            leds.value = buffer[1];
            Leds_Write(&leds);
        }
    }
}