trackswitch/main.c

changeset 22
9af54a45ae42
parent 21
8b7cdb655e7b
child 40
c36bf33489f9
equal deleted inserted replaced
21:8b7cdb655e7b 22:9af54a45ae42
23 #define TRACKSWITCH_TYPE 2 // 1=double, 2=single 23 #define TRACKSWITCH_TYPE 2 // 1=double, 2=single
24 24
25 #define SOLENOID_A_PIN PB1 25 #define SOLENOID_A_PIN PB1
26 #define SOLENOID_B_PIN PB2 26 #define SOLENOID_B_PIN PB2
27 27
28 // internal analog comparator doesnt work well
29 //#define ANALOG_COMPARATOR 1
28 30
29 volatile uint16_t data = 0; 31 volatile uint16_t data = 0;
30 volatile uint8_t data_len = 0; 32 volatile uint8_t data_len = 0;
31 volatile uint8_t bitbuf_len = 0; 33 volatile uint8_t bitbuf_len = 0;
32 volatile uint16_t bitbuf = 0; 34 volatile uint16_t bitbuf = 0;
110 } 112 }
111 car0_old = car0_new; 113 car0_old = car0_new;
112 } 114 }
113 115
114 // ISR (TIMER1_CAPT_vect) { 116 // ISR (TIMER1_CAPT_vect) {
117 #ifdef ANALOG_COMPARATOR
115 ISR (ANA_COMP_vect) { 118 ISR (ANA_COMP_vect) {
116 // car1 detector 119 // car1 detector
117 uint16_t tmp = 0; 120 uint16_t tmp = 0;
118 car1_new = TCNT1; // get current counter 121 car1_new = TCNT1; // get current counter
119 if (car1_old < car1_new) { 122 if (car1_old < car1_new) {
124 if ( (tmp > 183) && (tmp < 210) ) car1 = 3; 127 if ( (tmp > 183) && (tmp < 210) ) car1 = 3;
125 if ( (tmp > 242) && (tmp < 270) ) car1 = 4; 128 if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
126 } 129 }
127 car1_old = car1_new; 130 car1_old = car1_new;
128 } 131 }
129 132 #else
130 // ALTERNATIV: 133 // ALTERNATIV:
131 ISR (TIMER1_CAPT_vect) { 134 ISR (TIMER1_CAPT_vect) {
132 // car1 detector 135 // car1 detector
133 uint16_t tmp = 0; 136 uint16_t tmp = 0;
134 car1_new = TCNT1; // get current counter 137 car1_new = TCNT1; // get current counter
140 if ( (tmp > 183) && (tmp < 210) ) car1 = 3; 143 if ( (tmp > 183) && (tmp < 210) ) car1 = 3;
141 if ( (tmp > 242) && (tmp < 270) ) car1 = 4; 144 if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
142 } 145 }
143 car1_old = car1_new; 146 car1_old = car1_new;
144 } 147 }
145 148 #endif
146 149
147 void solenoid_delay(void) { 150 void solenoid_delay(void) {
148 _delay_ms(10); 151 _delay_ms(10);
149 } 152 }
150 153
151 154
152 //#define ANALOG_COMPARATOR 1
153 155
154 int main(void) 156 int main(void)
155 { 157 {
156 uint8_t car0_state, car1_state; 158 uint8_t car0_state, car1_state;
157 159
196 while (1) { 198 while (1) {
197 // main loop 199 // main loop
198 200
199 if (car0 != car0_state) { 201 if (car0 != car0_state) {
200 car0_state = car0; 202 car0_state = car0;
201 if ( (car0_state != 0) && (car_switch[car0_state-1] == 0) ) { 203 if ( (car0_state != 0) && (car_switch[car0_state-1] == 0) && (car_speed[car0_state-1]>0) ) {
202 // trigger solenoid A 204 // trigger solenoid A
203 RS232_putc('A'); 205 RS232_putc('A');
206 RS232_putc('B');
204 RS232_putc('0'+car0_state); 207 RS232_putc('0'+car0_state);
205 RS232_putc('\n'); 208 RS232_putc('\n');
206 209
207 SOLENOID_A_PORT |= _BV(SOLENOID_A_PIN); 210 SOLENOID_A_PORT |= _BV(SOLENOID_A_PIN);
208 solenoid_delay(); 211 solenoid_delay();
209 SOLENOID_A_PORT &= ~_BV(SOLENOID_A_PIN); 212 SOLENOID_A_PORT &= ~_BV(SOLENOID_A_PIN);
210 solenoid_delay(); 213 solenoid_delay();
214 }
215 if (car0_state != 0) {
216 RS232_putc('A');
217 RS232_putc('A');
218 RS232_putc('0'+car0_state);
219 RS232_putc('\n');
211 } 220 }
212 } car0 = 0; 221 } car0 = 0;
213 222
214 223
215 if (car1 != car1_state) { 224 if (car1 != car1_state) {
216 car1_state = car1; 225 car1_state = car1;
217 if ( (car1_state != 0) && (car_switch[car1_state-1] == 0) ) { 226 if ( (car1_state != 0) && (car_switch[car1_state-1] == 0) && (car_speed[car1_state-1]>0) ) {
218 // trigger solenoid A 227 // trigger solenoid A
219 RS232_putc('B'); 228 RS232_putc('B');
229 RS232_putc('A');
220 RS232_putc('0'+car1_state); 230 RS232_putc('0'+car1_state);
221 RS232_putc('\n'); 231 RS232_putc('\n');
222 232
223 SOLENOID_B_PORT |= _BV(SOLENOID_B_PIN); 233 SOLENOID_B_PORT |= _BV(SOLENOID_B_PIN);
224 solenoid_delay(); 234 solenoid_delay();
225 SOLENOID_B_PORT &= ~_BV(SOLENOID_B_PIN); 235 SOLENOID_B_PORT &= ~_BV(SOLENOID_B_PIN);
226 solenoid_delay(); 236 solenoid_delay();
237 }
238 if (car1_state != 0) {
239 RS232_putc('B');
240 RS232_putc('B');
241 RS232_putc('0'+car1_state);
242 RS232_putc('\n');
227 } 243 }
228 } car1 = 0; 244 } car1 = 0;
229 245
230 246
231 } // main loop end 247 } // main loop end

mercurial