Mon, 14 Nov 2011 21:38:00 +0100
fixed track change keys
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 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
10 | void LED(uint8_t num, uint8_t state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
11 | switch (num) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
12 | case 1: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
13 | case 0: LED1_PORT &= ~_BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | case 1: LED1_PORT |= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | case 2: LED1_PORT ^= _BV(LED1); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | case 2: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
19 | case 0: LED2_PORT &= ~_BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | case 1: LED2_PORT |= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | case 2: LED2_PORT ^= _BV(LED2); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | case 3: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | case 0: LED3_PORT &= ~_BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | case 1: LED3_PORT |= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | case 2: LED3_PORT ^= _BV(LED3); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | case 4: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | case 0: LED4_PORT &= ~_BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | case 1: LED4_PORT |= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | case 2: LED4_PORT ^= _BV(LED4); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
35 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
36 | case 5: switch (state) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
37 | case 0: LED5_PORT &= ~_BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
38 | case 1: LED5_PORT |= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
39 | case 2: LED5_PORT ^= _BV(LED5); break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
40 | } break; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
41 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
42 | } |
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
43 | |
3 | 44 | void init_hardware(void) { |
45 | RS232_init(); // initialize RS485 interface | |
46 | RS232_puts_p(PSTR("CARRERA beta loading\n")); | |
47 | ||
48 | initADC(); | |
49 | ||
50 | // set LED output | |
51 | DDR(LED1_PORT) |= _BV(LED1); | |
52 | DDR(LED2_PORT) |= _BV(LED2); | |
53 | DDR(LED3_PORT) |= _BV(LED3); | |
54 | DDR(LED4_PORT) |= _BV(LED4); | |
55 | DDR(LED5_PORT) |= _BV(LED5); | |
56 | ||
57 | // set Controller Input Pull-UPs | |
58 | CONTROLLER_PORT |= (_BV(CONTROLLER1_SW) | _BV(CONTROLLER2_SW) | _BV(CONTROLLER3_SW) | _BV(CONTROLLER4_SW)); | |
59 | ||
60 | // switch pull-ups | |
61 | SW_FUEL_PORT |= _BV(SW_FUEL); | |
62 | ||
63 | //RAIL_DETECT_PORT |= _BV(RAIL_DETECT); // enable internal pull-up | |
64 | DDR(RAIL_POWER_PORT) |= _BV(RAIL_POWER); | |
65 | ||
66 | ||
67 | LED(1, 1); _delay_ms(50); | |
68 | LED(2, 1); _delay_ms(50); | |
69 | LED(3, 1); _delay_ms(50); | |
70 | LED(4, 1); _delay_ms(50); | |
71 | LED(5, 1); _delay_ms(50); | |
72 | LED(1, 0); _delay_ms(50); | |
73 | LED(2, 0); _delay_ms(50); | |
74 | LED(3, 0); _delay_ms(50); | |
75 | LED(4, 0); _delay_ms(50); | |
76 | LED(5, 0); _delay_ms(50); | |
77 | ||
78 | //TCCR0 = (1<<CS01); //divide by 8 | |
79 | ||
80 | ||
81 | // setup data bit timer | |
82 | TCCR2 = (1<<CS21); //divide by 8 | |
83 | TCCR2 |= (1<<WGM21); // set compare match | |
84 | OCR2 = TIMER2_50US; | |
85 | TIMSK |= 1<<OCIE2; //enable timer2 interrupt | |
86 | ||
87 | // setup data packet timer | |
88 | //TCCR1A = (1<<COM1A1); | |
89 | TCCR1B = (1<<CS11); //divide by 8 | |
90 | TCCR1B |= (1<<WGM12); // set compare match | |
91 | //TCCR1B = (1<<CS11) | (1<<CS10); //divide by 64 | |
92 | //TCNT1 = TIMER_7500NS; | |
93 | OCR1A = TIMER1_7500NS; | |
94 | TIMSK |= 1<<OCIE1A; //enable timer1 interrupt | |
95 | ||
96 | RS232_puts_p(PSTR("INIT OK\n")); | |
97 | ||
98 | } | |
99 | ||
100 | ||
101 | ||
2
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
102 | void check_rails_shortcut(void) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
103 | // check for short circuit on the rails |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
104 | 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
|
105 | _delay_ms(1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
106 | 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
|
107 | cli(); // disable ALL Interrupts |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
108 | 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
|
109 | 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
|
110 | LED(1, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
111 | LED(2, 1); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
112 | LED(3, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
113 | LED(4, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
114 | LED(5, 0); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
115 | while (1) { |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
116 | LED(1, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
117 | LED(2, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
118 | LED(4, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
119 | LED(5, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
120 | _delay_ms(100); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
121 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
122 | _delay_ms(100); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
123 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
124 | _delay_ms(100); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
125 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
126 | _delay_ms(100); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
127 | LED(3, 2); |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
128 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
129 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
130 | } |
6c59b4293fa9
finished: rails short circuit check
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
131 | } |