Tue, 06 Dec 2011 16:45:52 +0100
added 16bit response to track switch
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
1 | #include <avr/interrupt.h> |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
2 | #include <avr/pgmspace.h> |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
3 | #include <util/delay.h> |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include "stdint.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include "main.h" |
3 | 6 | #include "driver/adc.h" |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
7 | #include "driver/rs232.h" |
3 | 8 | #include "lowlevel.h" |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | |
34 | 10 | void blinkdelay(void) { |
11 | _delay_ms(50); | |
12 | } | |
13 | ||
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | void LED(uint8_t num, uint8_t state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | switch (num) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | case 1: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | case 0: LED1_PORT &= ~_BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | case 1: LED1_PORT |= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
19 | case 2: LED1_PORT ^= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | case 2: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | case 0: LED2_PORT &= ~_BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | case 1: LED2_PORT |= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | case 2: LED2_PORT ^= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | case 3: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | case 0: LED3_PORT &= ~_BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | case 1: LED3_PORT |= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | case 2: LED3_PORT ^= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | case 4: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
35 | case 0: LED4_PORT &= ~_BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
36 | case 1: LED4_PORT |= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
37 | case 2: LED4_PORT ^= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
38 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
39 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
40 | case 5: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
41 | case 0: LED5_PORT &= ~_BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
42 | case 1: LED5_PORT |= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
43 | case 2: LED5_PORT ^= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
46 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
47 | |
37
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
48 | void LEDS_OFF(void) { |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
49 | LED1_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
50 | LED2_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
51 | LED3_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
52 | LED4_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
53 | LED5_PORT &= ~_BV(LED1); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
54 | } |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
55 | |
3 | 56 | void init_hardware(void) { |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
57 | // reset all ports to input, no pullup |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
58 | DDRA = 0; PORTA = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
59 | DDRB = 0; PORTB = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
60 | DDRC = 0; PORTC = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
61 | DDRD = 0; PORTD = 0; |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
62 | |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
63 | |
34 | 64 | RS232_init(); // initialize RS232 interface |
3 | 65 | initADC(); |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
66 | SFIOR = 0; |
3 | 67 | |
68 | // set LED output | |
69 | DDR(LED1_PORT) |= _BV(LED1); | |
70 | DDR(LED2_PORT) |= _BV(LED2); | |
71 | DDR(LED3_PORT) |= _BV(LED3); | |
72 | DDR(LED4_PORT) |= _BV(LED4); | |
73 | DDR(LED5_PORT) |= _BV(LED5); | |
74 | ||
75 | // set Controller Input Pull-UPs | |
76 | CONTROLLER_PORT |= (_BV(CONTROLLER1_SW) | _BV(CONTROLLER2_SW) | _BV(CONTROLLER3_SW) | _BV(CONTROLLER4_SW)); | |
77 | ||
78 | // switch pull-ups | |
79 | SW_FUEL_PORT |= _BV(SW_FUEL); | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
80 | SW_START_PORT |= _BV(SW_START); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
81 | SW_PACECAR_PORT |= _BV(SW_PACECAR); |
3 | 82 | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
83 | // control outputs |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
84 | DDR(LAP_COUNTER_PORT) |= _BV(LAP_COUNTER); |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
85 | |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
86 | // setup rail control |
3 | 87 | //RAIL_DETECT_PORT |= _BV(RAIL_DETECT); // enable internal pull-up |
88 | DDR(RAIL_POWER_PORT) |= _BV(RAIL_POWER); | |
89 | ||
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
90 | // display init sequence on LEDs |
34 | 91 | LED(1, 1); blinkdelay(); |
92 | LED(2, 1); blinkdelay(); | |
93 | LED(3, 1); blinkdelay(); | |
94 | LED(4, 1); blinkdelay(); | |
95 | LED(5, 1); blinkdelay(); | |
96 | LED(1, 0); blinkdelay(); | |
97 | LED(2, 0); blinkdelay(); | |
98 | LED(3, 0); blinkdelay(); | |
99 | LED(4, 0); blinkdelay(); | |
100 | LED(5, 0); | |
3 | 101 | |
6
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
102 | |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
103 | // setup response receiver timer |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
104 | TCCR0 = (1<<CS01); //divide by 8 |
2405aff29a51
added response receiver timer
Malte Bayer <mbayer@neo-soft.org>
parents:
4
diff
changeset
|
105 | // interrupt enable + tcnt0 set in timer2 |
3 | 106 | |
107 | ||
108 | // setup data bit timer | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
109 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
3 | 110 | OCR2 = TIMER2_50US; |
111 | TIMSK |= 1<<OCIE2; //enable timer2 interrupt | |
112 | ||
113 | // setup data packet timer | |
114 | //TCCR1A = (1<<COM1A1); | |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
6
diff
changeset
|
115 | TCCR1B = (1<<CS11) | (1<<WGM12); //divide by 8, set compare match |
3 | 116 | //TCCR1B = (1<<CS11) | (1<<CS10); //divide by 64 |
117 | //TCNT1 = TIMER_7500NS; | |
118 | OCR1A = TIMER1_7500NS; | |
119 | TIMSK |= 1<<OCIE1A; //enable timer1 interrupt | |
120 | ||
37
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
121 | RS232_puts_p(PSTR("FreeSlot Blackbox v")); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
122 | RS232_puts(VERSION); |
136a79772098
implemented race prepare phase, countdown and jumpstart detection
Malte Bayer <mbayer@neo-soft.org>
parents:
34
diff
changeset
|
123 | RS232_putc('\n'); |
3 | 124 | |
125 | } | |
126 | ||
127 | ||
128 | ||
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
129 | void check_rails_shortcut(void) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
130 | // check for short circuit on the rails |
10 | 131 | uint8_t i = 100; |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
132 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
10 | 133 | while (i>0) { |
134 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) != 0) return 0; | |
135 | _delay_us(20); | |
33 | 136 | i--; |
10 | 137 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
138 | if ((PIN(RAIL_DETECT_PORT) & _BV(RAIL_DETECT)) == 0) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
139 | cli(); // disable ALL Interrupts |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
140 | RAIL_POWER_PORT &= ~_BV(RAIL_POWER); // disable rails power |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
141 | RS232_puts_p(PSTR("!!! SHORT CIRCUIT ON RAILS, POWERED OFF !!!\n")); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
142 | LED(1, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
143 | LED(2, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
144 | LED(3, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
145 | LED(4, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
146 | LED(5, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
147 | while (1) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
148 | LED(1, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
149 | LED(2, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
150 | LED(4, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
151 | LED(5, 2); |
33 | 152 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
153 | LED(3, 2); |
33 | 154 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
155 | LED(3, 2); |
33 | 156 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
157 | LED(3, 2); |
33 | 158 | blinkdelay(); |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
159 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
160 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
161 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
162 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
163 | } |