implemented car detection to cu

Wed, 07 Dec 2011 15:56:05 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Wed, 07 Dec 2011 15:56:05 +0100
changeset 41
9857c18c5e64
parent 40
c36bf33489f9
child 42
018d4d63ad3b

implemented car detection to cu

blackbox/lowlevel.c file | annotate | diff | comparison | revisions
blackbox/main.c file | annotate | diff | comparison | revisions
--- 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<<CS21) | (1<<WGM21); //divide by 8, set compare match
     OCR2 = TIMER2_50US;
     TIMSK |= 1<<OCIE2; //enable timer2 interrupt
 
+    // enable carid interrupts
+    MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge
+    GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1
+
+
+
     // setup data packet timer
-    //TCCR1A = (1<<COM1A1);
-    TCCR1B = (1<<CS11) | (1<<WGM12); //divide by 8, set compare match
-    //TCCR1B = (1<<CS11) | (1<<CS10); //divide by 64
-    //TCNT1 = TIMER_7500NS;
+    TCCR1B = (1<<CS11) | (1<<WGM12); //divide by 8, set compare match (1mhz clock)
     OCR1A = TIMER1_7500NS;
     TIMSK |= 1<<OCIE1A; //enable timer1 interrupt
 
--- a/blackbox/main.c	Tue Dec 06 16:45:52 2011 +0100
+++ b/blackbox/main.c	Wed Dec 07 15:56:05 2011 +0100
@@ -34,7 +34,10 @@
 volatile uint8_t fuel[MAX_SLOTS];
 volatile uint8_t jumpstart_time[MAX_SLOTS];
 
-
+volatile uint16_t car0, car1;
+volatile uint16_t car0_new, car0_old;
+volatile uint16_t car1_new, car1_old;
+uint8_t car0_state, car1_state;
 
 volatile uint8_t program_count = 0;
 volatile uint8_t program_id;
@@ -47,7 +50,6 @@
 static char buffer[RS232_BUFSIZE+1];
 static uint8_t buffer_len;
 
-
 volatile uint16_t transmit_buffer;
 volatile uint16_t transmit_buffer_queue;
 volatile uint8_t  transmit_len;
@@ -68,8 +70,6 @@
     return 0;
 }
 
-
-
 ISR ( USART_RXC_vect ) {
     uint8_t tmp;
     char c = UDR;
@@ -241,6 +241,11 @@
     // trigger packet transfer:
     transmit_len = transmit_len_next;
     // here is some more time to do something else...
+
+    // reset both car counters to overflow
+    car0_old = TIMER1_7500NS;
+    car1_old = TIMER1_7500NS;
+
 }
 
 ISR ( TIMER2_COMP_vect ) {
@@ -316,6 +321,37 @@
     } else timer0_delay--; // 2.3 ms delay not reached yet
 }
 
+ISR (INT0_vect) {
+    // car0 detector
+    uint16_t tmp = 0;
+    car0_new = TCNT1; // get current counter
+    if (car0_old < car0_new) {
+        // calculate difference
+        if (car0 == 0) tmp = car0_new-car0_old;
+        if ( (tmp > 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);

mercurial