Wed, 23 Nov 2011 14:22:28 +0100
done working car id measurement via short-blocking function call (slot1-4 for now)
#include <avr/interrupt.h> #include <avr/io.h> #include <avr/wdt.h> #include <avr/eeprom.h> #include <stdlib.h> #include <stdint.h> #include <avr/pgmspace.h> #include "main.h" #include "driver/rs232.h" #include "util/delay.h" ISR ( USART_RXC_vect ) { } // PD2 / PD3 = INT0 / INT1 // connect IR receiver to these pins to measure frequencies uint8_t get_car(uint8_t pin) { uint8_t i=0xff; // wait ~~ Xµs for low signal while ( (PIN(PORTD) & pin) != 0) { _delay_us(5); i--; if (i==0) return 0; // no low signal, do not longer block } // wait until signal is high again to start measurement while ( (PIN(PORTD) & pin) == 0) ; i = 0; while (i<100) { _delay_us(4); i++; if ( (PIN(PORTD) & pin) == 0) { // return car ID if ( (i>= 5) & (i<= 9) ) return 1; // 05-09 = car1 if ( (i>=13) & (i<=16) ) return 2; // 13-16 = car2 if ( (i>=19) & (i<=22) ) return 3; // 19-22 = car3 if ( (i>=28) & (i<=31) ) return 4; // 28-31 = car4 // debug: return higher values: if (i>32) return i; } } return 0; // timeout or incorrect measurement } int main(void) { uint16_t i; unsigned char s[30]; RS232_init(); // initialize RS232 interface RS232_puts_p(PSTR("Car ID Scanner v0.1\n")); while (1) { // main loop i = get_car(_BV(PD2)); if (i > 0) { itoa ( i , s, 10); RS232_puts(s); RS232_putc('\n'); } } // main loop end };