Fri, 09 Dec 2011 20:02:17 +0100
added pitlane skeleton based on trackswitch
63
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
1 | #include <avr/interrupt.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
2 | #include <avr/io.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
3 | #include <avr/wdt.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include <avr/eeprom.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | #include <stdlib.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
6 | #include <stdint.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
7 | #include <avr/pgmspace.h> |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
8 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | #include "main.h" |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
10 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
11 | #include "driver/rs232.h" |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
12 | #include "util/delay.h" |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
13 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
15 | ISR ( USART_RXC_vect ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
18 | #define PULSE_PORT PORTD |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
19 | #define PULSE_BIT PD2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | #define RESPONSE_PORT PORTC |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | #define RESPONSE_PIN PC1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | #define SOLENOID_A_PORT PORTB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | #define SOLENOID_B_PORT PORTB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | #define SOLENOID_A_PIN PB1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | #define SOLENOID_B_PIN PB2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | #define TRACKSWITCH_TYPE 2 // 1=double, 2=single, 3=pitlane |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | // internal analog comparator doesnt work well |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | //#define ANALOG_COMPARATOR 1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | volatile uint16_t data = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | volatile uint8_t data_len = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
35 | volatile uint8_t bitbuf_len = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
36 | volatile uint16_t bitbuf = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
37 | volatile uint8_t car_speed[8]; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
38 | volatile uint8_t car_switch[8]; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
39 | volatile uint16_t car0, car1, car2; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
40 | volatile uint16_t car0_new, car0_old; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
41 | volatile uint16_t car1_new, car1_old; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
42 | volatile uint16_t car2_new, car2_old; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
43 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | volatile uint8_t response = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | uint8_t self_id = 0b1111; // ONLY ONE pitlane |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
46 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
47 | void send_response(uint16_t data) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
48 | /* frame format: |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
49 | 1 startbit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
50 | 2 car id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
51 | 3 car id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
52 | 4 car id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
53 | 5 track change status bit 1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
54 | 6 track change status bit 2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
55 | 7 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
56 | 8 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
57 | 9 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
58 | 9 sender id |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
59 | 10 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
60 | 11 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
61 | 12 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
62 | 13 device type |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
63 | 14 reserved |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
64 | 15 reserved |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
65 | 16 stopbit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
66 | */ |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
67 | uint8_t index = 16; // bit count maximum |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
68 | uint8_t enable = DDR(RESPONSE_PORT) | _BV(RESPONSE_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
69 | uint8_t disable = DDR(RESPONSE_PORT) & ~_BV(RESPONSE_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
70 | data |= 0b100000000000001; // make sure start/stop bits are set |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
71 | data = 0b1010101010101011; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
72 | while (index != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
73 | if ((data & 1) != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
74 | DDR(RESPONSE_PORT) = enable; // enable response output |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
75 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
76 | DDR(RESPONSE_PORT) = disable; // disable response output |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
77 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
78 | data = data >> 1; // next bit prepare |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
79 | index--; // decrement index |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
80 | _delay_us(49); // bit valid phase |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
81 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
82 | // finally be sure to release the bus! |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
83 | DDR(RESPONSE_PORT) = disable; // disable response output |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
84 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
85 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
86 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
87 | ISR ( INT0_vect ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
88 | GICR &= ~_BV(INT0) ; // Disable INT0 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
89 | // Startsignal erkannt, ab hier den Timer2 starten, |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
90 | // der liest dann alle 50µs den Zustand ein und schreibt das |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
91 | // empfangene Bit in den Puffer |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
92 | bitbuf = 0; // init |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
93 | bitbuf_len = 0b10000000; // init 1 pulse received |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
94 | TCNT2 = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
95 | TIMSK |= _BV(OCIE2); //enable timer2 interrupt |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
96 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
97 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
98 | ISR ( TIMER2_COMP_vect ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
99 | uint8_t clock; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
100 | uint8_t state; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
101 | uint8_t state2; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
102 | if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
103 | if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
104 | if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0xff; else state2 = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
105 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
106 | if (clock) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
107 | bitbuf_len &= ~_BV(7); // switch clock to low |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
108 | // second pulse of bit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
109 | if ((state==state2) & state2) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
110 | // two cycles high: packet end received |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
111 | data_len = (bitbuf_len & 0b00111111); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
112 | TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
113 | GICR |= _BV(INT0) ; // Enable INT0 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
114 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
115 | //data = bitbuf; // output data |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
116 | // write data of controllers to array |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
117 | if (data_len == 10) { // controller data packet |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
118 | clock = (bitbuf >> 6) & 0b00000111; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
119 | car_speed[clock] = (bitbuf >> 1) & 0x0F; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
120 | car_switch[clock] = (bitbuf >> 5) & 1; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
121 | // current response for this car? |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
122 | if (response != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
123 | if ( ((response & 0b00001110) >> 1) == clock) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
124 | // add our ID to response: |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
125 | send_response(response | (self_id << 6)); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
126 | response = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
127 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
128 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
129 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
130 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
131 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
132 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
133 | bitbuf_len++; // increment bit counter |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
134 | bitbuf = bitbuf << 1; // shift bits |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
135 | if (state2 == 0) bitbuf |= 1; // receive logic one |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
136 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
137 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
138 | bitbuf_len |= _BV(7); // switch clock to high |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
139 | // first pulse of bit |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
140 | if (state2) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
141 | bitbuf_len |= _BV(6); // store new state |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
142 | } else { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
143 | bitbuf_len &= ~_BV(6); // store new state |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
144 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
145 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
146 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
147 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
148 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
149 | ISR (TIMER1_OVF_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
150 | // reset both car counters to overflow |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
151 | car0_old = 0xffff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
152 | car1_old = 0xffff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
153 | // pitlane exit sensor |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
154 | car2_old = 0xffff; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
155 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
156 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
157 | ISR (INT1_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
158 | // car0 detector |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
159 | uint16_t tmp = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
160 | car0_new = TCNT1; // get current counter |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
161 | if (car0_old < car0_new) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
162 | // calculate difference |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
163 | if (car0 == 0) tmp = car0_new-car0_old; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
164 | if ( (tmp > 54) && (tmp < 74) ) car0 = 1; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
165 | if ( (tmp > 118) && (tmp < 138) ) car0 = 2; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
166 | if ( (tmp > 186) && (tmp < 206) ) car0 = 3; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
167 | if ( (tmp > 246) && (tmp < 266) ) car0 = 4; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
168 | if ( (tmp > 310) && (tmp < 330) ) car0 = 5; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
169 | if ( (tmp > 374) && (tmp < 394) ) car0 = 6; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
170 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
171 | car0_old = car0_new; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
172 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
173 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
174 | // ISR (TIMER1_CAPT_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
175 | #ifdef ANALOG_COMPARATOR |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
176 | ISR (ANA_COMP_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
177 | // car1 detector |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
178 | uint16_t tmp = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
179 | car1_new = TCNT1; // get current counter |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
180 | if (car1_old < car1_new) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
181 | // calculate difference |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
182 | if (car1 == 0) tmp = car1_new-car1_old; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
183 | if ( (tmp > 50) && (tmp < 78) ) car1 = 1; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
184 | if ( (tmp > 114) && (tmp < 146) ) car1 = 2; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
185 | if ( (tmp > 183) && (tmp < 210) ) car1 = 3; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
186 | if ( (tmp > 242) && (tmp < 270) ) car1 = 4; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
187 | if ( (tmp > 310) && (tmp < 330) ) car1 = 5; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
188 | if ( (tmp > 374) && (tmp < 394) ) car1 = 6; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
189 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
190 | car1_old = car1_new; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
191 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
192 | #else |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
193 | // ALTERNATIV: |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
194 | ISR (TIMER1_CAPT_vect) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
195 | // car1 detector |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
196 | uint16_t tmp = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
197 | car1_new = TCNT1; // get current counter |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
198 | if (car1_old < car1_new) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
199 | // calculate difference |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
200 | if (car1 == 0) tmp = car1_new-car1_old; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
201 | if ( (tmp > 50) && (tmp < 78) ) car1 = 1; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
202 | if ( (tmp > 114) && (tmp < 146) ) car1 = 2; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
203 | if ( (tmp > 183) && (tmp < 210) ) car1 = 3; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
204 | if ( (tmp > 242) && (tmp < 270) ) car1 = 4; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
205 | if ( (tmp > 310) && (tmp < 330) ) car1 = 5; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
206 | if ( (tmp > 374) && (tmp < 394) ) car1 = 6; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
207 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
208 | car1_old = car1_new; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
209 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
210 | #endif |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
211 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
212 | void solenoid_delay(void) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
213 | _delay_ms(10); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
214 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
215 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
216 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
217 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
218 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
219 | int main(void) |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
220 | { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
221 | uint8_t car0_state, car1_state; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
222 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
223 | // setup data bit timer2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
224 | TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
225 | OCR2 = TIMER2_50US; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
226 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
227 | // initialize timer1 for IR signal detection |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
228 | #ifdef ANALOG_COMPARATOR |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
229 | TCCR1B = _BV(CS01) ; // 1mhz clock |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
230 | TIMSK = _BV(OCIE2) | _BV(TOIE1) ; //enable timer1+2 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
231 | #else |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
232 | TCCR1B = _BV(CS01) | _BV(ICNC1) | _BV(ICES1); // 1mhz clock, enable ICP on rising edge |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
233 | TIMSK = _BV(OCIE2) | _BV(TOIE1) | _BV(TICIE1); //enable timer1+2 / ICP1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
234 | #endif |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
235 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
236 | // enable both external interrupts |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
237 | // int 0 = data RX |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
238 | // int 1 = car0 input |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
239 | MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
240 | GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
241 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
242 | #ifdef ANALOG_COMPARATOR |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
243 | ACSR = _BV(ACIE) | _BV(ACIS1) | _BV(ACIS0); // setup analog comparator |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
244 | #endif |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
245 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
246 | // oscillator calibration |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
247 | // atmega8@1mhz = 0xac |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
248 | // @4mhz = ca 0xa0 |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
249 | //OSCCAL = 0xa0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
250 | //OSCCAL = 0x9A; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
251 | //OSCCAL = 0xa0; // internal oscillator @ 4 mhz.... doesnt work accurate! |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
252 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
253 | RS232_init(); // initialize RS232 interface |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
254 | RS232_puts_p(PSTR("Freeslot TrackSwitch / Pitlane v1.3\n")); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
255 | sei(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
256 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
257 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
258 | DDR(SOLENOID_A_PORT) |= _BV(SOLENOID_A_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
259 | DDR(SOLENOID_B_PORT) |= _BV(SOLENOID_B_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
260 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
261 | DDR(RESPONSE_PORT) &= ~_BV(RESPONSE_PIN); // switch response off |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
262 | RESPONSE_PORT &= ~_BV(RESPONSE_PIN); // switch response off |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
263 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
264 | while (1) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
265 | // main loop |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
266 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
267 | /* |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
268 | 0 = AA |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
269 | 1 = AB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
270 | 2 = BB |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
271 | 3 = BA |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
272 | */ |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
273 | if (car0 != car0_state) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
274 | car0_state = car0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
275 | if ( (car0_state != 0) && (car_switch[car0_state-1] == 0) && (car_speed[car0_state-1]>0) ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
276 | response = (1 | ((car0_state-1)<<1) | (1 << 4)); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
277 | // trigger solenoid A |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
278 | RS232_putc('A'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
279 | RS232_putc('B'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
280 | RS232_putc('0'+car0_state); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
281 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
282 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
283 | SOLENOID_A_PORT |= _BV(SOLENOID_A_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
284 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
285 | SOLENOID_A_PORT &= ~_BV(SOLENOID_A_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
286 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
287 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
288 | if (car0_state != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
289 | response = (1 | ((car0_state-1)<<1)); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
290 | RS232_putc('A'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
291 | RS232_putc('A'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
292 | RS232_putc('0'+car0_state); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
293 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
294 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
295 | } car0 = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
296 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
297 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
298 | if (car1 != car1_state) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
299 | car1_state = car1; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
300 | if ( (car1_state != 0) && (car_switch[car1_state-1] == 0) && (car_speed[car1_state-1]>0) ) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
301 | response = (1 | ((car1_state-1)<<1) | (3 << 4)); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
302 | // trigger solenoid B |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
303 | RS232_putc('B'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
304 | RS232_putc('A'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
305 | RS232_putc('0'+car1_state); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
306 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
307 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
308 | SOLENOID_B_PORT |= _BV(SOLENOID_B_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
309 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
310 | SOLENOID_B_PORT &= ~_BV(SOLENOID_B_PIN); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
311 | solenoid_delay(); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
312 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
313 | if (car1_state != 0) { |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
314 | response = (1 | ((car1_state-1)<<1) | (2 << 4)); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
315 | RS232_putc('B'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
316 | RS232_putc('B'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
317 | RS232_putc('0'+car1_state); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
318 | RS232_putc('\n'); |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
319 | } |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
320 | } car1 = 0; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
321 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
322 | |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
323 | } // main loop end |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
324 | }; |
ea1e8dcbec44
added pitlane skeleton based on trackswitch
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
325 |