# HG changeset patch # User Malte Bayer # Date 1323269765 -3600 # Node ID 9857c18c5e6476ac57d7860d8afe3ab1a768dac4 # Parent c36bf33489f9cb8d64df553a17782556ff9da1a2 implemented car detection to cu diff -r c36bf33489f9 -r 9857c18c5e64 blackbox/lowlevel.c --- a/blackbox/lowlevel.c Tue Dec 06 16:45:52 2011 +0100 +++ b/blackbox/lowlevel.c Wed Dec 07 15:56:05 2011 +0100 @@ -105,16 +105,19 @@ // interrupt enable + tcnt0 set in timer2 - // setup data bit timer + // setup data bit + carid timer TCCR2 = (1< 54) && (tmp < 74) ) car0 = 1; + if ( (tmp > 118) && (tmp < 138) ) car0 = 2; + if ( (tmp > 186) && (tmp < 206) ) car0 = 3; + if ( (tmp > 246) && (tmp < 266) ) car0 = 4; + } + car0_old = car0_new; +} + +ISR (INT1_vect) { + // car1 detector + uint16_t tmp = 0; + car1_new = TCNT1; // get current counter + if (car1_old < car1_new) { + // calculate difference + if (car1 == 0) tmp = car1_new-car1_old; + if ( (tmp > 54) && (tmp < 74) ) car1 = 1; + if ( (tmp > 118) && (tmp < 138) ) car1 = 2; + if ( (tmp > 186) && (tmp < 206) ) car1 = 3; + if ( (tmp > 246) && (tmp < 266) ) car1 = 4; + } + car1_old = car1_new; +} + + ISR (INT2_vect) { // Lap counter Interrupt // do not know if this ever occurs ?! this is normally an output pin to trigger the counter start @@ -352,6 +388,28 @@ } } +void check_cars(void) { + if (car0 != car0_state) { + car0_state = car0; + if (car0_state != 0) { + RS232_putc('A'); + RS232_putc('A'); + RS232_putc('0'+car0_state); + RS232_putc('\n'); + } + } car0 = 0; + + if (car1 != car1_state) { + car1_state = car1; + if (car1_state != 0) { + RS232_putc('B'); + RS232_putc('B'); + RS232_putc('0'+car1_state); + RS232_putc('\n'); + } + } car1 = 0; +} + int main(void) { @@ -362,6 +420,7 @@ uint8_t btn_start = _BV(SW_START); uint8_t old_start = btn_start; + init_hardware(); reset_vars(); LED(3, 1); // enable middle led == idle mode @@ -372,6 +431,7 @@ while (1) { // check for short circuit on the rails check_rails_shortcut(); + check_cars(); if (response_len > 0) { itoa(response, s, 2);