blackbox/main.c

changeset 41
9857c18c5e64
parent 37
136a79772098
child 42
018d4d63ad3b
equal deleted inserted replaced
40:c36bf33489f9 41:9857c18c5e64
32 #define MAX_SLOTS 6 32 #define MAX_SLOTS 6
33 volatile uint8_t speedlimit[MAX_SLOTS]; 33 volatile uint8_t speedlimit[MAX_SLOTS];
34 volatile uint8_t fuel[MAX_SLOTS]; 34 volatile uint8_t fuel[MAX_SLOTS];
35 volatile uint8_t jumpstart_time[MAX_SLOTS]; 35 volatile uint8_t jumpstart_time[MAX_SLOTS];
36 36
37 37 volatile uint16_t car0, car1;
38 volatile uint16_t car0_new, car0_old;
39 volatile uint16_t car1_new, car1_old;
40 uint8_t car0_state, car1_state;
38 41
39 volatile uint8_t program_count = 0; 42 volatile uint8_t program_count = 0;
40 volatile uint8_t program_id; 43 volatile uint8_t program_id;
41 volatile uint8_t program_command; 44 volatile uint8_t program_command;
42 volatile uint8_t program_parameter; 45 volatile uint8_t program_parameter;
44 volatile uint8_t datalen = 0; 47 volatile uint8_t datalen = 0;
45 char data[10]; // 8 bytes data buffer + string termination 48 char data[10]; // 8 bytes data buffer + string termination
46 49
47 static char buffer[RS232_BUFSIZE+1]; 50 static char buffer[RS232_BUFSIZE+1];
48 static uint8_t buffer_len; 51 static uint8_t buffer_len;
49
50 52
51 volatile uint16_t transmit_buffer; 53 volatile uint16_t transmit_buffer;
52 volatile uint16_t transmit_buffer_queue; 54 volatile uint16_t transmit_buffer_queue;
53 volatile uint8_t transmit_len; 55 volatile uint8_t transmit_len;
54 volatile uint8_t transmit_len_next; 56 volatile uint8_t transmit_len_next;
65 transmit_len_queue = len; 67 transmit_len_queue = len;
66 return 1; 68 return 1;
67 } 69 }
68 return 0; 70 return 0;
69 } 71 }
70
71
72 72
73 ISR ( USART_RXC_vect ) { 73 ISR ( USART_RXC_vect ) {
74 uint8_t tmp; 74 uint8_t tmp;
75 char c = UDR; 75 char c = UDR;
76 76
239 239
240 ISR ( TIMER1_COMPA_vect ) { 240 ISR ( TIMER1_COMPA_vect ) {
241 // trigger packet transfer: 241 // trigger packet transfer:
242 transmit_len = transmit_len_next; 242 transmit_len = transmit_len_next;
243 // here is some more time to do something else... 243 // here is some more time to do something else...
244
245 // reset both car counters to overflow
246 car0_old = TIMER1_7500NS;
247 car1_old = TIMER1_7500NS;
248
244 } 249 }
245 250
246 ISR ( TIMER2_COMP_vect ) { 251 ISR ( TIMER2_COMP_vect ) {
247 //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!! 252 //OCR2 = TIMER2_50US; // make sure that timer2 is 50µs !!!
248 // data packet timer 100µs pro bit... 253 // data packet timer 100µs pro bit...
314 RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power 319 RAIL_POWER_PORT |= _BV(RAIL_POWER); // restore rails power
315 } 320 }
316 } else timer0_delay--; // 2.3 ms delay not reached yet 321 } else timer0_delay--; // 2.3 ms delay not reached yet
317 } 322 }
318 323
324 ISR (INT0_vect) {
325 // car0 detector
326 uint16_t tmp = 0;
327 car0_new = TCNT1; // get current counter
328 if (car0_old < car0_new) {
329 // calculate difference
330 if (car0 == 0) tmp = car0_new-car0_old;
331 if ( (tmp > 54) && (tmp < 74) ) car0 = 1;
332 if ( (tmp > 118) && (tmp < 138) ) car0 = 2;
333 if ( (tmp > 186) && (tmp < 206) ) car0 = 3;
334 if ( (tmp > 246) && (tmp < 266) ) car0 = 4;
335 }
336 car0_old = car0_new;
337 }
338
339 ISR (INT1_vect) {
340 // car1 detector
341 uint16_t tmp = 0;
342 car1_new = TCNT1; // get current counter
343 if (car1_old < car1_new) {
344 // calculate difference
345 if (car1 == 0) tmp = car1_new-car1_old;
346 if ( (tmp > 54) && (tmp < 74) ) car1 = 1;
347 if ( (tmp > 118) && (tmp < 138) ) car1 = 2;
348 if ( (tmp > 186) && (tmp < 206) ) car1 = 3;
349 if ( (tmp > 246) && (tmp < 266) ) car1 = 4;
350 }
351 car1_old = car1_new;
352 }
353
354
319 ISR (INT2_vect) { 355 ISR (INT2_vect) {
320 // Lap counter Interrupt 356 // Lap counter Interrupt
321 // do not know if this ever occurs ?! this is normally an output pin to trigger the counter start 357 // do not know if this ever occurs ?! this is normally an output pin to trigger the counter start
322 } 358 }
323 359
350 mode = 3; 386 mode = 3;
351 } break; 387 } break;
352 } 388 }
353 } 389 }
354 390
391 void check_cars(void) {
392 if (car0 != car0_state) {
393 car0_state = car0;
394 if (car0_state != 0) {
395 RS232_putc('A');
396 RS232_putc('A');
397 RS232_putc('0'+car0_state);
398 RS232_putc('\n');
399 }
400 } car0 = 0;
401
402 if (car1 != car1_state) {
403 car1_state = car1;
404 if (car1_state != 0) {
405 RS232_putc('B');
406 RS232_putc('B');
407 RS232_putc('0'+car1_state);
408 RS232_putc('\n');
409 }
410 } car1 = 0;
411 }
412
355 413
356 int main(void) 414 int main(void)
357 { 415 {
358 416
359 unsigned char s[10]; 417 unsigned char s[10];
360 uint8_t packet_index = 1; 418 uint8_t packet_index = 1;
361 419
362 uint8_t btn_start = _BV(SW_START); 420 uint8_t btn_start = _BV(SW_START);
363 uint8_t old_start = btn_start; 421 uint8_t old_start = btn_start;
422
364 423
365 init_hardware(); 424 init_hardware();
366 reset_vars(); 425 reset_vars();
367 LED(3, 1); // enable middle led == idle mode 426 LED(3, 1); // enable middle led == idle mode
368 427
370 RAIL_POWER_PORT |= _BV(RAIL_POWER); 429 RAIL_POWER_PORT |= _BV(RAIL_POWER);
371 430
372 while (1) { 431 while (1) {
373 // check for short circuit on the rails 432 // check for short circuit on the rails
374 check_rails_shortcut(); 433 check_rails_shortcut();
434 check_cars();
375 435
376 if (response_len > 0) { 436 if (response_len > 0) {
377 itoa(response, s, 2); 437 itoa(response, s, 2);
378 response_len = 0; 438 response_len = 0;
379 RS232_puts("ANSWER RX: "); 439 RS232_puts("ANSWER RX: ");

mercurial