Subversion Repositories Code-Repo

Rev

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

Rev 270 Rev 273
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 = OFF       // Watchdog Timer Enable (WDT software controlled)
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 = ON       // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
8
#pragma config MCLRE = ON       // 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 21... Line 21...
21
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
21
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
22
// </editor-fold>
22
// </editor-fold>
23
 
23
 
24
#include "defines.h"
24
#include "defines.h"
25
#include "INTERRUPTS.h"
25
#include "INTERRUPTS.h"
-
 
26
#include "IO.h"
26
#include "I2C1.h"
27
#include "I2C1.h"
27
#include "I2C2.h"
28
#include "I2C2.h"
28
#include "TLC59116.h"
29
#include "TLC59116.h"
29
#include "MCP23009.h"
30
#include "MCP23009.h"
30
 
31
 
31
void Pins_Init(void) {
32
persistent uint8_t op_state;
-
 
33
 
32
    // Set all pins to digital I/O
34
void Reset_Board(uint8_t next_state) {
33
    ANSELA = 0x0;
-
 
34
    ANSELB = 0x0;
35
    op_state = next_state;
35
    ANSELC = 0x0;
36
    RESET();
36
 
37
}
37
    // Enable weak pull-up if WPU bit is set
-
 
38
    OPTION_REGbits.nWPUEN = 0;
-
 
39
 
38
 
40
    // Initialize interrupt inputs
39
uint8_t Get_Last_Reset(void) {
41
    LSM303_INT_TRIS = 1;
40
    uint8_t ret;
42
    L3GD20_INT_TRIS = 1;
41
    if (PCONbits.nPOR == 0) {
43
    BTN_INT_TRIS = 1;
42
        ret = RESET_POR;
44
 
-
 
45
    // Initialize UART pins
43
    } else if (PCONbits.nBOR == 0) {
46
    UART_RX_TRIS = 1;
44
        ret = RESET_BOR;
47
    UART_TX_TRIS = 0;
45
    } else if (STATUSbits.nTO == 0) {
48
 
-
 
49
    // Initialize I2C address pins
46
        ret = RESET_WDT;
50
    I2C_ADDR_0_TRIS = 1;
47
    } else if (PCONbits.nRMCLR == 0) {
51
    I2C_ADDR_1_TRIS = 1;
48
        ret = RESET_MCLR;
52
    I2C_ADDR_2_TRIS = 1;
49
    } else if (PCONbits.STKOVF || PCONbits.STKUNF) {
53
    I2C_ADDR_3_TRIS = 1;
50
        ret = RESET_STK;
54
    // Enable the weak-pullup on the address pins
51
    } else if (PCONbits.nRI == 0) {
55
    I2C_ADDR_0_WPU = 1;
52
        ret = RESET_RST;
56
    I2C_ADDR_1_WPU = 1;
53
    } else {
57
    I2C_ADDR_2_WPU = 1;
54
        ret = RESET_POR;
58
    I2C_ADDR_3_WPU = 1;
55
    }
59
 
56
 
60
    // Initialize I2C pins (dont really need to as the I2C code does it)
-
 
61
    I2C_1_CLK_TRIS = 1;
57
    PCON = 0x0F;
62
    I2C_1_DAT_TRIS = 1;
58
    STATUSbits.nPD = 1;
63
    I2C_2_CLK_TRIS = 1;
59
    STATUSbits.nTO = 1;
64
    I2C_2_DAT_TRIS = 1;
60
    return ret;
65
}
61
}
66
 
62
 
67
uint8_t Read_Address(void) {
63
uint8_t Read_Address(void) {
68
    uint8_t ret = 0;
64
    uint8_t ret = I2C1_SLAVE_PREFIX;
69
    ret |= I2C_ADDR_3_LAT << 3;
65
    if (!I2C_ADDR_3_PORT)
-
 
66
        ret |= 0x08;
70
    ret |= I2C_ADDR_2_LAT << 2;
67
    if (!I2C_ADDR_2_PORT)
-
 
68
        ret |= 0x04;
71
    ret |= I2C_ADDR_1_LAT << 1;
69
    if (!I2C_ADDR_1_PORT)
-
 
70
        ret |= 0x02;
72
    ret |= I2C_ADDR_0_LAT;
71
    if (!I2C_ADDR_0_PORT)
-
 
72
        ret |= 0x01;
73
    
73
 
74
    return ret;
74
    return ret;
75
}
75
}
76
 
76
 
-
 
77
BTN_STATUS btns;
-
 
78
 
77
int main(void) {
79
int main(void) {
78
    uint8_t buffer[32];
80
    uint8_t buffer[32];
79
    uint8_t result, length;
81
    uint8_t result, length;
80
    uint8_t i2c_slave_addr;
82
    uint8_t i2c_slave_addr;
-
 
83
//    uint8_t op_state;
-
 
84
    uint8_t i;
81
 
85
 
82
    // Set internal oscillator speed to 32MHz
86
    // Set internal oscillator speed to 32MHz
83
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
87
    OSCCONbits.SPLLEN = 1;  // 4x PLL enable (overwritten by config bits)
84
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
88
    OSCCONbits.IRCF = 0xE;  // Base frequency @ 8MHz
85
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
89
    OSCCONbits.SCS = 0b00;  // System clock determined by config bits
86
 
90
 
87
    // Set watchdog timer to reset device every 1s
-
 
88
    // CLRWDT is issued upon receiving data over I2C
-
 
89
//    WDTCON = 0x0A;
-
 
90
 
-
 
91
    // Initialize I/O
91
    // Initialize I/O
92
    Pins_Init();
92
    IO_Init();
-
 
93
 
-
 
94
    uint8_t last_reset = Get_Last_Reset();
-
 
95
    if (last_reset == RESET_POR || last_reset == RESET_BOR ||
-
 
96
            last_reset == RESET_MCLR || last_reset == RESET_WDT ||
-
 
97
            last_reset == RESET_STK) {
-
 
98
        op_state = OP_STATE_IDLE;
-
 
99
    }
-
 
100
//    if (last_reset == RESET_RST) {
-
 
101
//        op_state = OP_STATE_ACTIVE;
-
 
102
//    }
93
 
103
 
94
    i2c_slave_addr = Read_Address();
104
    i2c_slave_addr = Read_Address();
95
 
105
 
-
 
106
    // Delay a bit to allow I2C lines to stabilize
-
 
107
    __delay_ms(1);
-
 
108
 
96
    // Initialize I2C1 in slave mode
109
    // Initialize I2C1 in slave mode
97
    I2C1_DATA i2c1_data;
110
    I2C1_DATA i2c1_data;
98
    I2C1_Init(&i2c1_data);
111
    I2C1_Init(&i2c1_data);
99
    I2C1_Configure_Slave(i2c_slave_addr);
112
    I2C1_Configure_Slave(i2c_slave_addr);
100
 
113
 
Line 105... Line 118...
105
 
118
 
106
    // Initialize interrupts
119
    // Initialize interrupts
107
    Interrupt_Init();
120
    Interrupt_Init();
108
    Interrupt_Enable();
121
    Interrupt_Enable();
109
 
122
 
-
 
123
    MCP23009_Init(&btns);
-
 
124
    MCP23009_Query();
-
 
125
 
-
 
126
    LED_VALUES leds = {0x00};
110
    TLC59116_Init();
127
    TLC59116_Init();
111
    MCP23009_Init();
128
//    TLC59116_Write_All(&leds);
112
 
129
 
-
 
130
    if (op_state == OP_STATE_IDLE) {
113
    BTN_STATUS btns;
131
        IO_IOC_Enable();
114
    LED_VALUES leds = {0x00, 0x00, 0x00, 0x00,
132
        Idle_Animation();
115
                       0x00, 0x00, 0x00, 0x00,
133
    } else if (op_state == OP_STATE_ACTIVE) {
-
 
134
        while (1) {
116
                       0x00, 0x00, 0x00, 0x00,
135
            __delay_ms(1);
117
                       0x00, 0x00, 0x00, 0x00};
136
            // Read value of buttons
118
 
-
 
119
    TLC59116_Write_All(&leds);
137
            MCP23009_Query();
120
 
138
 
121
    Idle_Animation();
139
            // Check if an I2C message was received
122
 
-
 
123
    // Check for received data over I2C1
140
            result = I2C1_Get_Status();
124
    while (1) {
141
            if (result) {
125
        MCP23009_Query(&btns);
142
                length = I2C1_Read_Buffer(buffer);
-
 
143
                if (length == 1 && buffer[0] == CMD_RESET) {
126
        uint8_t i;
144
                    Reset_Board(OP_STATE_IDLE);
127
        for (i = 0; i < 8; i++) {
145
                } else if (length == 17 && buffer[0] == CMD_SET_LEDS) {
128
            if ((btns.w >> i) & 0x1) {
146
                    for (i = 0; i < 16; i++) {
129
                leds.w[i] = 0x00;
147
                        leds.w[i] = buffer[i+1];
130
            } else {
148
                    }
-
 
149
                    TLC59116_Write_All(&leds);
131
                leds.w[i] = 0x20;
150
                }
132
            }
151
            }
133
        }
152
        }
134
        TLC59116_Write_All(&leds);
-
 
135
    }
153
    }
136
}
154
}
137
 
155
 
138
void Idle_Animation(void) {
156
void Idle_Animation(void) {
139
    LED_VALUES leds = {0};
157
    LED_VALUES leds = {0};