SHOW:
|
|
- or go back to the newest paste.
1 | /* | |
2 | Copyright 2012 D Westaby | |
3 | ||
4 | ---------------------------------------------------------------------- | |
5 | Generic Pattern for Attiny2313 | |
6 | ---------------------------------------------------------------------- | |
7 | Title: Generic_Lighting.c | |
8 | Author: Dustin Westaby | |
9 | Date Created: September 8, 2012 | |
10 | Date Modified: March 21, 2013 | |
11 | ||
12 | Compiled with AVR-GCC WinAVR | |
13 | ||
14 | Connection reference by Alex Weber: | |
15 | http://tinkerlog.com/2009/06/18/microcontroller-cheat-sheet/ | |
16 | ||
17 | ---------------------------------------------------------------------- | |
18 | Fuses: | |
19 | ---------------------------------------------------------------------- | |
20 | BrownOut Disabled | |
21 | CKDIV8 | |
22 | Int RC Osc 8Mhz + 64ms | |
23 | ||
24 | ---------------------------------------------------------------------- | |
25 | Inputs: | |
26 | ---------------------------------------------------------------------- | |
27 | pin i/o port circuit trace notes | |
28 | ---------------------------------------------------------------------- | |
29 | 1 0 PA2 = RESET Momentary Switch to ground | |
30 | 10 GND = Ground | |
31 | 20 VCC = +V Battery | |
32 | 12 0 PB0 = B0 Momentary Switch to ground | |
33 | ||
34 | Note: Momentary switches need pullup 10k resisters | |
35 | ||
36 | ---------------------------------------------------------------------- | |
37 | Ouputs: | |
38 | ---------------------------------------------------------------------- | |
39 | pin i/o port circuit trace notes | |
40 | ---------------------------------------------------------------------- | |
41 | 2 1 PD0 = LED1 *1 | |
42 | 3 1 PD1 = LED2 *1 | |
43 | 4 1 PA1 = LED3 *1 | |
44 | 5 1 PA0 = LED4 *1 | |
45 | 6 1 PD2 = LED5 | |
46 | 7 1 PD3 = LED6 | |
47 | - | 8 1 PD4 = LED7 CODE |
47 | + | 8 1 PD4 = LED7 |
48 | - | 9 1 PD5 = LED8 CODE |
48 | + | 9 1 PD5 = LED8 |
49 | - | 11 1 PD6 = LED9 CODE |
49 | + | 11 1 PD6 = LED9 |
50 | - | 13 1 PB1 = LED10 CODE |
50 | + | 13 1 PB1 = LED10 |
51 | 14 1 PB2 = LED11 | |
52 | 17 1 PB5 = LED12 *1 | |
53 | 18 1 PB6 = LED13 *1 | |
54 | 19 1 PB7 = LED14 *1 | |
55 | ||
56 | Note *1 - LED connected will disable pin's alternate functions | |
57 | = external osc | |
58 | = serial | |
59 | = programming | |
60 | Use a resister to isolate these. | |
61 | ||
62 | */ | |
63 | ||
64 | //-------------------------------------- | |
65 | // Global Variables | | |
66 | //-------------------------------------- | |
67 | // 8 MHz Internal Oscillator DIV8 (used for delay subroutines) | |
68 | // One CPU Cycle = 1us | |
69 | #define F_CPU 8000000UL/8 | |
70 | ||
71 | #define LED1_PD0 (0) | |
72 | #define LED2_PD1 (1) | |
73 | #define LED3_PA1 (1) | |
74 | #define LED4_PA0 (0) | |
75 | #define LED5_PD2 (2) | |
76 | #define LED6_PD3 (3) | |
77 | ||
78 | #define LED7_PD4 (4) //These 4 are new to the program | |
79 | #define LED8_PD5 (5) | |
80 | #define LED9_PD6 (6) | |
81 | #define LED10_PB1 (1) | |
82 | ||
83 | #define LED11_PB2 (2) | |
84 | #define LED12_PB5 (5) | |
85 | #define LED13_PB6 (6) | |
86 | #define LED14_PB7 (7) | |
87 | ||
88 | #define INPUT_PB0 (0) | |
89 | ||
90 | #define CONST_DIM_DELAY_ON (200) | |
91 | #define CONST_DIM_DELAY_OFF (4000) | |
92 | ||
93 | /* This value is used to count us of delay and convert to ms | |
94 | Due to other cpu processing: "1ms of delay" < "1000us of delay" */ | |
95 | //#define TIME_CONVERSION_VAL (50) //actual number should be 150 (am running faster) | |
96 | #define TIME_CONVERSION_VAL (150) | |
97 | ||
98 | #define OFF (0) | |
99 | #define ON (1) | |
100 | ||
101 | #define LED_ON(led_position) (ON<<led_position) | |
102 | #define LED_OFF(led_position) (OFF<<led_position) | |
103 | ||
104 | #define INPUT_SW (0) | |
105 | #define OUTPUT_LED (1) | |
106 | ||
107 | int program_ms_counter; | |
108 | int program_counter_us; //used for tracking time elapsed | |
109 | ||
110 | //-------------------------------------- | |
111 | // Includes | | |
112 | //-------------------------------------- | |
113 | #include <avr/io.h> | |
114 | #include <util/delay.h> | |
115 | ||
116 | //-------------------------------------- | |
117 | // Delay Subroutines | | |
118 | //-------------------------------------- | |
119 | //These functions are from the delay.h include, the calls to delay functions | |
120 | //are re-written here to allow for longer waits. | |
121 | void delay_ms(uint16_t ms) { | |
122 | program_ms_counter+=ms; | |
123 | while ( ms ) | |
124 | { | |
125 | _delay_ms(1); | |
126 | ms--; | |
127 | } | |
128 | } | |
129 | void delay_us(uint16_t us) { | |
130 | program_counter_us+=us; | |
131 | while ( us ) | |
132 | { | |
133 | _delay_us(1); | |
134 | us--; | |
135 | } | |
136 | ||
137 | while(program_counter_us>=TIME_CONVERSION_VAL) | |
138 | { | |
139 | program_ms_counter++; | |
140 | program_counter_us-=TIME_CONVERSION_VAL; | |
141 | } | |
142 | } | |
143 | ||
144 | //-------------------------------------- | |
145 | // Common Subroutines | | |
146 | //-------------------------------------- | |
147 | ||
148 | void reset_ms_us_counters(void) { | |
149 | program_ms_counter=0; | |
150 | program_counter_us=0; | |
151 | } | |
152 | ||
153 | void all_LEDs_ON(void) { | |
154 | PORTA = 0xFF; | |
155 | PORTB = 0xFF; | |
156 | PORTD = 0xFF; | |
157 | } | |
158 | ||
159 | void all_LEDs_OFF(void) { | |
160 | PORTA = ~(0x03); | |
161 | PORTB = ~(0xFF); | |
162 | PORTD = ~(0xFF); | |
163 | } | |
164 | ||
165 | //-------------------------------------- | |
166 | // Switch Debounce | | |
167 | //-------------------------------------- | |
168 | int button_is_pressed() | |
169 | { | |
170 | // the button is pressed when bit is clear | |
171 | if (bit_is_clear(PINB, PB0)) | |
172 | { | |
173 | delay_ms(25); | |
174 | if (bit_is_clear(PINB, PB0)) return 1; | |
175 | } | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | //-------------------------------------- | |
181 | // Specific Subroutines | | |
182 | //-------------------------------------- | |
183 | ||
184 | void program_left_turn_mode(void) { | |
185 | /* Opposite '~', actually turns the following LEDs OFF */ | |
186 | PORTA &= ~(LED_ON(LED3_PA1) | LED_ON(LED4_PA0)); | |
187 | ||
188 | PORTA &= 0xFF & (LED_OFF(LED3_PA1) | LED_OFF(LED4_PA0)); | |
189 | ||
190 | PORTB &= ~(LED_ON(LED14_PB7) | LED_ON(LED12_PB5)); | |
191 | PORTD &= ~(LED_ON(LED6_PD3) | LED_ON(LED5_PD2) | LED_ON(LED2_PD1) | LED_ON(LED1_PD0)); | |
192 | } | |
193 | ||
194 | void program_brake_mode(void) { | |
195 | /* Opposite '~', actually turns the following LEDs OFF */ | |
196 | PORTA &= ~(LED_ON(LED3_PA1) | LED_ON(LED4_PA0)); | |
197 | } | |
198 | ||
199 | void program_right_turn_mode (void) { | |
200 | /* Opposite '~', actually turns the following LEDs OFF */ | |
201 | PORTA &= ~(LED_ON(LED3_PA1) | LED_ON(LED4_PA0)); | |
202 | PORTB &= ~(LED_ON(LED14_PB7) | LED_ON(LED13_PB6) | LED_ON(LED12_PB5) | LED_ON(LED11_PB2)); | |
203 | PORTD &= ~(LED_ON(LED6_PD3) | LED_ON(LED2_PD1)); | |
204 | } | |
205 | ||
206 | void program_all_dim_loop (int timer_val) { | |
207 | ||
208 | while(program_ms_counter < timer_val) | |
209 | { | |
210 | all_LEDs_ON(); | |
211 | delay_us(CONST_DIM_DELAY_ON); | |
212 | all_LEDs_OFF(); | |
213 | delay_us(CONST_DIM_DELAY_OFF); | |
214 | } | |
215 | ||
216 | } | |
217 | ||
218 | void program_break_loop (int timer_val) { | |
219 | ||
220 | while(program_ms_counter < timer_val) | |
221 | { | |
222 | all_LEDs_ON(); | |
223 | delay_us(CONST_DIM_DELAY_ON); | |
224 | program_brake_mode(); | |
225 | delay_us(CONST_DIM_DELAY_OFF); | |
226 | } | |
227 | ||
228 | } | |
229 | ||
230 | void program_left_turn_loop (int timer_val) { | |
231 | ||
232 | while(program_ms_counter < timer_val) | |
233 | { | |
234 | all_LEDs_ON(); | |
235 | delay_us(CONST_DIM_DELAY_ON); | |
236 | program_left_turn_mode(); | |
237 | delay_us(CONST_DIM_DELAY_OFF); | |
238 | } | |
239 | ||
240 | } | |
241 | ||
242 | void program_right_turn_loop (int timer_val) { | |
243 | ||
244 | while(program_ms_counter < timer_val) | |
245 | { | |
246 | all_LEDs_ON(); | |
247 | delay_us(CONST_DIM_DELAY_ON); | |
248 | program_right_turn_mode(); | |
249 | delay_us(CONST_DIM_DELAY_OFF); | |
250 | } | |
251 | ||
252 | } | |
253 | ||
254 | ||
255 | //-------------------------------------- | |
256 | // Main | | |
257 | //-------------------------------------- | |
258 | int main (void) | |
259 | { | |
260 | ||
261 | /* ---------------------------------------------------------------- */ | |
262 | /* Initialization */ | |
263 | /* ---------------------------------------------------------------- */ | |
264 | ||
265 | //Initialize all ports | |
266 | // DDRA = 0b00000011; //A0, A1 | |
267 | // DDRB = 0b11111111; | |
268 | // DDRD = 0b11111111; | |
269 | DDRA = (OUTPUT_LED<<LED4_PA0) | (OUTPUT_LED<<LED3_PA1); | |
270 | DDRB = (INPUT_SW<<INPUT_PB0) | (OUTPUT_LED<<LED10_PB1) | (OUTPUT_LED<<LED11_PB2) | (OUTPUT_LED<<LED12_PB5) | (OUTPUT_LED<<LED13_PB6) | (OUTPUT_LED<<LED14_PB7); | |
271 | DDRD = (OUTPUT_LED<<LED1_PD0) | (OUTPUT_LED<<LED2_PD1) | (OUTPUT_LED<<LED5_PD2) | (OUTPUT_LED<<LED6_PD3) | (OUTPUT_LED<<LED7_PD4) | (OUTPUT_LED<<LED8_PD5) | (OUTPUT_LED<<LED9_PD6); | |
272 | ||
273 | all_LEDs_OFF(); | |
274 | ||
275 | /* ---------------------------------------------------------------- */ | |
276 | /* Main Loop */ | |
277 | /* ---------------------------------------------------------------- */ | |
278 | ||
279 | while(1) | |
280 | { | |
281 | ||
282 | //1) Dim | |
283 | //1 - All dim nothing happens (resting natural state) | |
284 | reset_ms_us_counters(); | |
285 | program_all_dim_loop(6000); | |
286 | ||
287 | //2) Left Turn | |
288 | //2 - left turn, all dim, 2 will blink at ½ sec for 10 sec. | |
289 | reset_ms_us_counters(); | |
290 | while(program_ms_counter < 10000) | |
291 | { | |
292 | program_left_turn_loop(program_ms_counter+500); | |
293 | program_all_dim_loop (program_ms_counter+500); | |
294 | } | |
295 | //then dim for 1 sec | |
296 | program_all_dim_loop(11000); | |
297 | ||
298 | //3) Dim (4 sec) | |
299 | //4 - All dim nothing happens (resting natural state) | |
300 | reset_ms_us_counters(); | |
301 | program_all_dim_loop(4000); | |
302 | ||
303 | //4) Brakes | |
304 | //3 – brakes get bright (8 LED’s) for 4 sec then dim for 1sec for 12 seconds | |
305 | reset_ms_us_counters(); | |
306 | while(program_ms_counter < 12000) | |
307 | { | |
308 | program_break_loop (program_ms_counter+4000); | |
309 | program_all_dim_loop(program_ms_counter+1000); | |
310 | } | |
311 | ||
312 | //5) Dim (4 Sec) | |
313 | //4 - All dim nothing happens (resting natural state) | |
314 | reset_ms_us_counters(); | |
315 | program_all_dim_loop(4000); | |
316 | ||
317 | //6) Right Turn | |
318 | //5 – right turn, all dim, 2 will blink at ½ sec for 10 sec. | |
319 | reset_ms_us_counters(); | |
320 | while(program_ms_counter < 10000) | |
321 | { | |
322 | program_right_turn_loop(program_ms_counter+500); | |
323 | program_all_dim_loop (program_ms_counter+500); | |
324 | } | |
325 | //then dim for 1 sec. | |
326 | program_all_dim_loop(11000); | |
327 | ||
328 | //7) Dim (4 sec) | |
329 | //4 - All dim nothing happens (resting natural state) | |
330 | reset_ms_us_counters(); | |
331 | program_all_dim_loop(4000); | |
332 | ||
333 | //8) Brakes | |
334 | //6 – brakes get bright (8 LED’s) for 4 sec then dim for 1sec for 12 seconds | |
335 | reset_ms_us_counters(); | |
336 | while(program_ms_counter < 12000) | |
337 | { | |
338 | program_break_loop (program_ms_counter+4000); | |
339 | program_all_dim_loop(program_ms_counter+1000); | |
340 | } | |
341 | ||
342 | //9) Loop | |
343 | ||
344 | } | |
345 | ||
346 | while(1); // Ending infinite loop (just in case) | |
347 | ||
348 | } |