| 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 |
int main(void) {
|
|
|
78 |
uint8_t buffer[32];
|
|
|
79 |
uint8_t result, length;
|
|
|
80 |
uint8_t i2c_slave_addr;
|
|
|
81 |
|
|
|
82 |
// Set internal oscillator speed to 32MHz
|
|
|
83 |
OSCCONbits.SPLLEN = 1; // 4x PLL enable (overwritten by config bits)
|
|
|
84 |
OSCCONbits.IRCF = 0xE; // Base frequency @ 8MHz
|
|
|
85 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
86 |
|
|
|
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
|
|
|
92 |
Pins_Init();
|
|
|
93 |
|
|
|
94 |
i2c_slave_addr = Read_Address();
|
|
|
95 |
|
|
|
96 |
// Initialize I2C1 in slave mode
|
|
|
97 |
I2C1_DATA i2c1_data;
|
|
|
98 |
I2C1_Init(&i2c1_data);
|
|
|
99 |
I2C1_Configure_Slave(i2c_slave_addr);
|
|
|
100 |
|
|
|
101 |
// Initialize I2C2 in master mode
|
|
|
102 |
I2C2_DATA i2c2_data;
|
|
|
103 |
I2C2_Init(&i2c2_data);
|
|
|
104 |
I2C2_Configure_Master(I2C_400KHZ);
|
|
|
105 |
|
|
|
106 |
// Initialize interrupts
|
|
|
107 |
Interrupt_Init();
|
|
|
108 |
Interrupt_Enable();
|
|
|
109 |
|
|
|
110 |
TLC59116_Init();
|
| 270 |
Kevin |
111 |
MCP23009_Init();
|
| 260 |
Kevin |
112 |
|
| 270 |
Kevin |
113 |
BTN_STATUS btns;
|
|
|
114 |
LED_VALUES leds = {0x00, 0x00, 0x00, 0x00,
|
|
|
115 |
0x00, 0x00, 0x00, 0x00,
|
|
|
116 |
0x00, 0x00, 0x00, 0x00,
|
|
|
117 |
0x00, 0x00, 0x00, 0x00};
|
| 260 |
Kevin |
118 |
|
| 270 |
Kevin |
119 |
TLC59116_Write_All(&leds);
|
| 260 |
Kevin |
120 |
|
| 270 |
Kevin |
121 |
Idle_Animation();
|
|
|
122 |
|
|
|
123 |
// Check for received data over I2C1
|
| 260 |
Kevin |
124 |
while (1) {
|
| 270 |
Kevin |
125 |
MCP23009_Query(&btns);
|
| 260 |
Kevin |
126 |
uint8_t i;
|
|
|
127 |
for (i = 0; i < 8; i++) {
|
| 270 |
Kevin |
128 |
if ((btns.w >> i) & 0x1) {
|
|
|
129 |
leds.w[i] = 0x00;
|
| 260 |
Kevin |
130 |
} else {
|
| 270 |
Kevin |
131 |
leds.w[i] = 0x20;
|
| 260 |
Kevin |
132 |
}
|
|
|
133 |
}
|
| 270 |
Kevin |
134 |
TLC59116_Write_All(&leds);
|
|
|
135 |
}
|
|
|
136 |
}
|
| 260 |
Kevin |
137 |
|
| 270 |
Kevin |
138 |
void Idle_Animation(void) {
|
|
|
139 |
LED_VALUES leds = {0};
|
|
|
140 |
uint8_t led_direction_bar[8] = {1,0,0,0,0,0,0,0};
|
|
|
141 |
uint8_t led_direction_dir[8] = {1,0,0,0};
|
|
|
142 |
uint8_t led_direction_ind[8] = {1,0,0,0};
|
|
|
143 |
uint8_t led_8_high_thres = 0x80; // Max brightness of the middle section
|
|
|
144 |
uint8_t led_8_next_thresh = 0x40; // Threshold to start the next LED
|
|
|
145 |
uint8_t led_4_high_thres = 0x80; // Max brightness of the side sections
|
|
|
146 |
uint8_t led_4_next_thresh = 0x74; // Threshold to start the next LED
|
|
|
147 |
uint8_t i, next_led;
|
| 260 |
Kevin |
148 |
|
| 270 |
Kevin |
149 |
for (i = 0; i < 16; i++) leds.w[i] = 0x00;
|
|
|
150 |
|
|
|
151 |
while (1) {
|
|
|
152 |
|
|
|
153 |
// Update the LEDs in the middle section
|
|
|
154 |
for (i = 0; i < 8; i++) {
|
|
|
155 |
// Change the LED brightness depending on its direction
|
|
|
156 |
if (led_direction_bar[i] == 1) {
|
|
|
157 |
leds.w[i]++;
|
|
|
158 |
} else if (led_direction_bar[i] == 2) {
|
|
|
159 |
leds.w[i]--;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
// Change direction if peak brightness is reached
|
|
|
163 |
// When the brightness reaches a middle threshold, start
|
|
|
164 |
// increasing the brightness of the next LED
|
|
|
165 |
if (led_direction_bar[i] == 1 && leds.w[i] == led_8_high_thres) {
|
|
|
166 |
led_direction_bar[i] = 2;
|
|
|
167 |
} else if (led_direction_bar[i] == 1 && leds.w[i] == led_8_next_thresh) {
|
|
|
168 |
next_led = (i == 7) ? 0 : i + 1;
|
|
|
169 |
led_direction_bar[next_led] = 1;
|
|
|
170 |
} else if (led_direction_bar[i] == 2 && leds.w[i] == 0x00) {
|
|
|
171 |
led_direction_bar[i] = 0;
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
// Update the LEDs in the right section
|
|
|
176 |
for (i = 0; i < 4; i++) {
|
|
|
177 |
// Change the LED brightness depending on its direction
|
|
|
178 |
if (led_direction_dir[i] == 1) {
|
|
|
179 |
leds.w[i+8]++;
|
|
|
180 |
} else if (led_direction_dir[i] == 2) {
|
|
|
181 |
leds.w[i+8]--;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
// Change direction if peak brightness is reached
|
|
|
185 |
// When the brightness reaches a middle threshold, start
|
|
|
186 |
// increasing the brightness of the next LED
|
|
|
187 |
if (led_direction_dir[i] == 1 && leds.w[i+8] == led_4_high_thres) {
|
|
|
188 |
led_direction_dir[i] = 2;
|
|
|
189 |
} else if (led_direction_dir[i] == 1 && leds.w[i+8] == led_4_next_thresh) {
|
|
|
190 |
next_led = (i == 3) ? 0 : i + 1;
|
|
|
191 |
led_direction_dir[next_led] = 1;
|
|
|
192 |
} else if (led_direction_dir[i] == 2 && leds.w[i+8] == 0x00) {
|
|
|
193 |
led_direction_dir[i] = 0;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
// Update the LEDs in the left section
|
|
|
198 |
for (i = 0; i < 4; i++) {
|
|
|
199 |
// Change the LED brightness depending on its direction
|
|
|
200 |
if (led_direction_ind[i] == 1) {
|
|
|
201 |
leds.w[i+12]++;
|
|
|
202 |
} else if (led_direction_ind[i] == 2) {
|
|
|
203 |
leds.w[i+12]--;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
// Change direction if peak brightness is reached
|
|
|
207 |
// When the brightness reaches a middle threshold, start
|
|
|
208 |
// increasing the brightness of the next LED
|
|
|
209 |
if (led_direction_ind[i] == 1 && leds.w[i+12] == led_4_high_thres) {
|
|
|
210 |
led_direction_ind[i] = 2;
|
|
|
211 |
} else if (led_direction_ind[i] == 1 && leds.w[i+12] == led_4_next_thresh) {
|
|
|
212 |
next_led = (i == 3) ? 0 : i + 1;
|
|
|
213 |
led_direction_ind[next_led] = 1;
|
|
|
214 |
} else if (led_direction_ind[i] == 2 && leds.w[i+12] == 0x00) {
|
|
|
215 |
led_direction_ind[i] = 0;
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
// Write the LED values to the controller
|
|
|
220 |
TLC59116_Write_All(&leds);
|
|
|
221 |
|
|
|
222 |
// Delay a bit to slow down the animation
|
|
|
223 |
__delay_ms(1);
|
| 260 |
Kevin |
224 |
}
|
|
|
225 |
|
| 270 |
Kevin |
226 |
}
|