blackbox/main.c

changeset 41
9857c18c5e64
parent 37
136a79772098
child 42
018d4d63ad3b
--- 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