Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
228 Kevin 1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
2
// PIC16F1825 Configuration Bit Settings
3
 
4
// CONFIG1
5
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
6
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
7
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
8
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
9
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
10
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
11
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
12
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
13
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
14
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
15
 
16
// CONFIG2
17
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
18
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
19
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
20
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
21
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
22
// </editor-fold>
23
 
24
#include "defines.h"
25
#include "base_INTERRUPTS.h"
26
#include "base_I2C.h"
27
 
28
void Pins_Init(void) {
29
    // Set all pins to digital I/O
30
    ANSELA = 0x0;
31
    ANSELC = 0x0;
32
 
33
    // Enable weak pull-up if WPU bit is set
34
    OPTION_REGbits.nWPUEN = 0;
35
 
36
    // Set buttons as inputs and enable weak pull-ups
37
    BTN_L_N_TRIS = 1;
233 Kevin 38
    BTN_L_N_WPU  = 1;
228 Kevin 39
    BTN_L_S_TRIS = 1;
233 Kevin 40
    BTN_L_S_WPU  = 1;
228 Kevin 41
 
42
    BTN_R_N_TRIS = 1;
233 Kevin 43
    BTN_R_N_WPU  = 1;
228 Kevin 44
    BTN_R_E_TRIS = 1;
233 Kevin 45
    BTN_R_E_WPU  = 1;
228 Kevin 46
    BTN_R_S_TRIS = 1;
233 Kevin 47
    BTN_R_S_WPU  = 1;
228 Kevin 48
    BTN_R_W_TRIS = 1;
233 Kevin 49
    BTN_R_W_WPU  = 1;
228 Kevin 50
 
51
    // Set leds as outputs and initialize to off
52
    LED_1_TRIS = 0;
233 Kevin 53
    LED_1_LAT  = 0;
228 Kevin 54
    LED_2_TRIS = 0;
233 Kevin 55
    LED_2_LAT  = 0;
228 Kevin 56
    LED_3_TRIS = 0;
233 Kevin 57
    LED_3_LAT  = 0;
228 Kevin 58
    LED_4_TRIS = 0;
233 Kevin 59
    LED_4_LAT  = 0;
228 Kevin 60
}
61
 
62
// Function to read button status into a data structure
63
void Pins_Read(BTN_STATUS *btns) {
64
    btns->BTN_L_N = BTN_L_N_PORT;
65
    btns->BTN_L_S = BTN_L_S_PORT;
66
 
67
    btns->BTN_R_N = BTN_R_N_PORT;
68
    btns->BTN_R_E = BTN_R_E_PORT;
69
    btns->BTN_R_S = BTN_R_S_PORT;
70
    btns->BTN_R_W = BTN_R_W_PORT;
71
}
72
 
73
// Function to write led values from the data structure
74
void Leds_Write(LED_STATUS *leds) {
75
    LED_1_LAT = leds->LED_1;
76
    LED_2_LAT = leds->LED_2;
77
    LED_3_LAT = leds->LED_3;
78
    LED_4_LAT = leds->LED_4;
79
}
80
 
81
int main(void) {
233 Kevin 82
    uint8_t buffer[32];
83
    uint8_t result, length;
228 Kevin 84
 
85
    // Set internal oscillator speed to 32MHz
86
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
87
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
88
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
89
 
90
    // Initialize I/O
91
    Pins_Init();
92
 
93
    // Initialize I2C in slave mode
94
    I2C_DATA i2c_data;
95
    I2C_Init(&i2c_data);
230 Kevin 96
    I2C_Configure_Slave(0x25);
228 Kevin 97
 
98
    // Initialize interrupts
99
    Interrupt_Init();
100
    Interrupt_Enable();
101
 
233 Kevin 102
    // Check for received data over I2C
228 Kevin 103
    while (1) {
104
        do {
105
            result = I2C_Get_Status();
106
        } while (!result);
107
        length = I2C_Read_Buffer(buffer);
108
        if (length == 2 && buffer[0] == CMD_SET_LEDS) {
109
            LED_STATUS leds;
110
            leds.value = buffer[1];
111
            Leds_Write(&leds);
112
        }
113
    }
114
}
115