receiver/main.c

changeset 17
9e6feafc19e1
parent 15
7d4c0c816465
--- a/receiver/main.c	Wed Nov 23 14:22:28 2011 +0100
+++ b/receiver/main.c	Thu Nov 24 16:04:30 2011 +0100
@@ -18,6 +18,33 @@
 // PD2 / PD3 = INT0 / INT1
 // connect IR receiver to these pins to measure frequencies
 
+volatile uint8_t car0, car1;
+uint8_t car_tmp0, car_tmp1, car_count0, car_count1, car_buffer0, car_buffer1;
+#define CARDETECT_CYCLES 2
+ISR ( TIMER0_OVF_vect ) {
+    // divides f_cpu by 256
+    // Interrupt every 32µs
+
+    if ( (PIN(PORTD) & _BV(PD2)) == 0) {
+        car_tmp0++;
+    } else {
+        // return car ID length
+        if (car_tmp0 != 0) {
+            car_count0++;
+            car_buffer0 += car_tmp0;
+            if (car_count0 == CARDETECT_CYCLES) {
+                car_buffer0 = (car_buffer0) / CARDETECT_CYCLES;
+                if (car_buffer0 == car_tmp0) car0 = car_tmp0;
+                //car0 = car_buffer0;
+                car_buffer0 = 0;
+                car_count0 = 0;
+            }
+            car_tmp0 = 0;
+        }
+    }
+
+}
+
 
 uint8_t get_car(uint8_t pin) {
     uint8_t i=0xff;
@@ -50,19 +77,33 @@
 {
     uint16_t i;
     unsigned char s[30];
+    
+    // initialize timer0 for IR signal detection
+    TCCR0 = _BV(CS00); // system clock, no prescaling
+    TIMSK = _BV(TOIE0);
 
     RS232_init(); // initialize RS232 interface
     RS232_puts_p(PSTR("Car ID Scanner v0.1\n"));
-
+    sei();
+    
     while (1) {
         // main loop
 
+/*
         i = get_car(_BV(PD2));
         if (i > 0) {
             itoa ( i , s, 10);
             RS232_puts(s);
             RS232_putc('\n');
         }
+*/
+
+        if (car0 > 0) {
+            itoa ( car0 , s, 10);
+            car0 = 0;
+            RS232_puts(s);
+            RS232_putc('\n');
+        }
 
     } // main loop end
 };

mercurial