| 260 |
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 = 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)
|
|
|
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 "INTERRUPTS.h"
|
|
|
26 |
#include "I2C1.h"
|
|
|
27 |
#include "I2C2.h"
|
|
|
28 |
#include "TLC59116.h"
|
|
|
29 |
#include "MCP23009.h"
|
|
|
30 |
|
|
|
31 |
void Pins_Init(void) {
|
|
|
32 |
// Set all pins to digital I/O
|
|
|
33 |
ANSELA = 0x0;
|
|
|
34 |
ANSELB = 0x0;
|
|
|
35 |
ANSELC = 0x0;
|
|
|
36 |
|
|
|
37 |
// Enable weak pull-up if WPU bit is set
|
|
|
38 |
OPTION_REGbits.nWPUEN = 0;
|
|
|
39 |
|
|
|
40 |
// Initialize interrupt inputs
|
|
|
41 |
LSM303_INT_TRIS = 1;
|
|
|
42 |
L3GD20_INT_TRIS = 1;
|
|
|
43 |
BTN_INT_TRIS = 1;
|
|
|
44 |
|
|
|
45 |
// Initialize UART pins
|
|
|
46 |
UART_RX_TRIS = 1;
|
|
|
47 |
UART_TX_TRIS = 0;
|
|
|
48 |
|
|
|
49 |
// Initialize I2C address pins
|
|
|
50 |
I2C_ADDR_0_TRIS = 1;
|
|
|
51 |
I2C_ADDR_1_TRIS = 1;
|
|
|
52 |
I2C_ADDR_2_TRIS = 1;
|
|
|
53 |
I2C_ADDR_3_TRIS = 1;
|
|
|
54 |
// Enable the weak-pullup on the address pins
|
|
|
55 |
I2C_ADDR_0_WPU = 1;
|
|
|
56 |
I2C_ADDR_1_WPU = 1;
|
|
|
57 |
I2C_ADDR_2_WPU = 1;
|
|
|
58 |
I2C_ADDR_3_WPU = 1;
|
|
|
59 |
|
|
|
60 |
// Initialize I2C pins (dont really need to as the I2C code does it)
|
|
|
61 |
I2C_1_CLK_TRIS = 1;
|
|
|
62 |
I2C_1_DAT_TRIS = 1;
|
|
|
63 |
I2C_2_CLK_TRIS = 1;
|
|
|
64 |
I2C_2_DAT_TRIS = 1;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
uint8_t Read_Address(void) {
|
|
|
68 |
uint8_t ret = 0;
|
|
|
69 |
ret |= I2C_ADDR_3_LAT << 3;
|
|
|
70 |
ret |= I2C_ADDR_2_LAT << 2;
|
|
|
71 |
ret |= I2C_ADDR_1_LAT << 1;
|
|
|
72 |
ret |= I2C_ADDR_0_LAT;
|
|
|
73 |
|
|
|
74 |
return ret;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Function to read button status into a data structure
|
|
|
78 |
void Pins_Read(BTN_STATUS *btns) {
|
|
|
79 |
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Function to write led values from the data structure
|
|
|
83 |
void Leds_Write(LED_STATUS *leds) {
|
|
|
84 |
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
int main(void) {
|
|
|
88 |
uint8_t buffer[32];
|
|
|
89 |
uint8_t result, length;
|
|
|
90 |
uint8_t i2c_slave_addr;
|
|
|
91 |
|
|
|
92 |
// Set internal oscillator speed to 32MHz
|
|
|
93 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
94 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
95 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
96 |
|
|
|
97 |
// Set watchdog timer to reset device every 1s
|
|
|
98 |
// CLRWDT is issued upon receiving data over I2C
|
|
|
99 |
// WDTCON = 0x0A;
|
|
|
100 |
|
|
|
101 |
// Initialize I/O
|
|
|
102 |
Pins_Init();
|
|
|
103 |
|
|
|
104 |
i2c_slave_addr = Read_Address();
|
|
|
105 |
|
|
|
106 |
// Initialize I2C1 in slave mode
|
|
|
107 |
I2C1_DATA i2c1_data;
|
|
|
108 |
I2C1_Init(&i2c1_data);
|
|
|
109 |
I2C1_Configure_Slave(i2c_slave_addr);
|
|
|
110 |
|
|
|
111 |
// Initialize I2C2 in master mode
|
|
|
112 |
I2C2_DATA i2c2_data;
|
|
|
113 |
I2C2_Init(&i2c2_data);
|
|
|
114 |
I2C2_Configure_Master(I2C_400KHZ);
|
|
|
115 |
|
|
|
116 |
// Initialize interrupts
|
|
|
117 |
Interrupt_Init();
|
|
|
118 |
Interrupt_Enable();
|
|
|
119 |
|
|
|
120 |
TLC59116_Init();
|
|
|
121 |
|
|
|
122 |
uint8_t leds[16] = {0x00, 0x00, 0x00, 0x00,
|
|
|
123 |
0x00, 0x00, 0x00, 0x00,
|
|
|
124 |
0x10, 0x10, 0x10, 0x10,
|
|
|
125 |
0x10, 0x10, 0x10, 0x10};
|
|
|
126 |
TLC59116_Write_All(leds);
|
|
|
127 |
|
|
|
128 |
MCP23009_Init();
|
|
|
129 |
|
|
|
130 |
// Check for received data over I2C
|
|
|
131 |
while (1) {
|
|
|
132 |
uint8_t btn_value = MCP23009_Query();
|
|
|
133 |
uint8_t i;
|
|
|
134 |
for (i = 0; i < 8; i++) {
|
|
|
135 |
if ((btn_value >> i) & 0x1) {
|
|
|
136 |
leds[i] = 0x00;
|
|
|
137 |
} else {
|
|
|
138 |
leds[i] = 0x10;
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
TLC59116_Write_All(leds);
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|