Subversion Repositories Code-Repo

Rev

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 <xc.h>
25
#include "defines.h"
26
#include "base_INTERRUPTS.h"
27
#include "base_I2C.h"
28
 
29
__persistent int temp;
30
 
31
void Pins_Init(void) {
32
    // Set all pins to digital I/O
33
    ANSELA = 0x0;
34
    ANSELC = 0x0;
35
 
36
    // Enable weak pull-up if WPU bit is set
37
    OPTION_REGbits.nWPUEN = 0;
38
 
39
    // Set buttons as inputs and enable weak pull-ups
40
    BTN_L_N_TRIS = 1;
41
    BTN_L_N_WPU = 1;
42
    BTN_L_S_TRIS = 1;
43
    BTN_L_S_WPU = 1;
44
 
45
    BTN_R_N_TRIS = 1;
46
    BTN_R_N_WPU = 1;
47
    BTN_R_E_TRIS = 1;
48
    BTN_R_E_WPU = 1;
49
    BTN_R_S_TRIS = 1;
50
    BTN_R_S_WPU = 1;
51
    BTN_R_W_TRIS = 1;
52
    BTN_R_W_WPU = 1;
53
 
54
    // Set leds as outputs and initialize to off
55
    LED_1_TRIS = 0;
56
    LED_1_LAT = 0;
57
    LED_2_TRIS = 0;
58
    LED_2_LAT = 0;
59
    LED_3_TRIS = 0;
60
    LED_3_LAT = 0;
61
    LED_4_TRIS = 0;
62
    LED_4_LAT = 0;
63
}
64
 
65
// Function to read button status into a data structure
66
void Pins_Read(BTN_STATUS *btns) {
67
    btns->BTN_L_N = BTN_L_N_PORT;
68
    btns->BTN_L_S = BTN_L_S_PORT;
69
 
70
    btns->BTN_R_N = BTN_R_N_PORT;
71
    btns->BTN_R_E = BTN_R_E_PORT;
72
    btns->BTN_R_S = BTN_R_S_PORT;
73
    btns->BTN_R_W = BTN_R_W_PORT;
74
}
75
 
76
// Function to write led values from the data structure
77
void Leds_Write(LED_STATUS *leds) {
78
    LED_1_LAT = leds->LED_1;
79
    LED_2_LAT = leds->LED_2;
80
    LED_3_LAT = leds->LED_3;
81
    LED_4_LAT = leds->LED_4;
82
}
83
 
84
int main(void) {
85
    char buffer[32];
86
    char result, length;
87
 
88
    // Set internal oscillator speed to 32MHz
89
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
90
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
91
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
92
 
93
    // Initialize I/O
94
    Pins_Init();
95
 
96
    // Initialize I2C in slave mode
97
    I2C_DATA i2c_data;
98
    I2C_Init(&i2c_data);
230 Kevin 99
    I2C_Configure_Slave(0x25);
228 Kevin 100
 
101
    // Initialize interrupts
102
    Interrupt_Init();
103
    Interrupt_Enable();
104
 
105
    while (1) {
106
        do {
107
            result = I2C_Get_Status();
108
        } while (!result);
109
        length = I2C_Read_Buffer(buffer);
110
        if (length == 2 && buffer[0] == CMD_SET_LEDS) {
111
            LED_STATUS leds;
112
            leds.value = buffer[1];
113
            Leds_Write(&leds);
114
        }
115
    }
116
}
117