Subversion Repositories Code-Repo

Rev

Rev 233 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 233 Rev 236
Line 1... Line 1...
1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
2
// PIC16F1825 Configuration Bit Settings
2
// PIC16F1825 Configuration Bit Settings
3
 
3
 
4
// CONFIG1
4
// CONFIG1
5
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
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)
6
#pragma config WDTE = ON        // Watchdog Timer Enable (WDT enabled)
7
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT 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)
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)
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)
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)
11
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
Line 24... Line 24...
24
#include "defines.h"
24
#include "defines.h"
25
#include "base_INTERRUPTS.h"
25
#include "base_INTERRUPTS.h"
26
#include "base_I2C.h"
26
#include "base_I2C.h"
27
 
27
 
28
void Pins_Init(void) {
28
void Pins_Init(void) {
-
 
29
 
-
 
30
 
29
    // Set all pins to digital I/O
31
    // Set all pins to digital I/O
30
    ANSELA = 0x0;
32
    ANSELA = 0x0;
31
    ANSELC = 0x0;
33
    ANSELC = 0x0;
32
 
34
 
33
    // Enable weak pull-up if WPU bit is set
35
    // Enable weak pull-up if WPU bit is set
Line 76... Line 78...
76
    LED_2_LAT = leds->LED_2;
78
    LED_2_LAT = leds->LED_2;
77
    LED_3_LAT = leds->LED_3;
79
    LED_3_LAT = leds->LED_3;
78
    LED_4_LAT = leds->LED_4;
80
    LED_4_LAT = leds->LED_4;
79
}
81
}
80
 
82
 
-
 
83
// TODO: Set watchdog timer to reset device if no I2C messages are received every x seconds
-
 
84
// TODO: Use a timer to manually PWM the LEDs
-
 
85
 
81
int main(void) {
86
int main(void) {
82
    uint8_t buffer[32];
87
    uint8_t buffer[32];
83
    uint8_t result, length;
88
    uint8_t result, length;
84
 
89
 
85
    // Set internal oscillator speed to 32MHz
90
    // Set internal oscillator speed to 32MHz
86
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
91
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
87
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
92
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
88
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
93
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
89
 
94
 
-
 
95
    // Set watchdog timer to reset device every 1s
-
 
96
    // CLRWDT is issued upon receiving data over I2C
-
 
97
    WDTCON = 0x0A;
-
 
98
 
90
    // Initialize I/O
99
    // Initialize I/O
91
    Pins_Init();
100
    Pins_Init();
92
 
101
 
93
    // Initialize I2C in slave mode
102
    // Initialize I2C in slave mode
94
    I2C_DATA i2c_data;
103
    I2C_DATA i2c_data;
Line 102... Line 111...
102
    // Check for received data over I2C
111
    // Check for received data over I2C
103
    while (1) {
112
    while (1) {
104
        do {
113
        do {
105
            result = I2C_Get_Status();
114
            result = I2C_Get_Status();
106
        } while (!result);
115
        } while (!result);
-
 
116
        CLRWDT();
107
        length = I2C_Read_Buffer(buffer);
117
        length = I2C_Read_Buffer(buffer);
108
        if (length == 2 && buffer[0] == CMD_SET_LEDS) {
118
        if (length == 2 && buffer[0] == CMD_SET_LEDS) {
109
            LED_STATUS leds;
119
            LED_STATUS leds;
110
            leds.value = buffer[1];
120
            leds.value = buffer[1];
111
            Leds_Write(&leds);
121
            Leds_Write(&leds);