Wed, 23 Nov 2011 14:22:28 +0100
done working car id measurement via short-blocking function call (slot1-4 for now)
receiver/main.c | file | annotate | diff | comparison | revisions |
--- a/receiver/main.c Wed Nov 23 08:16:38 2011 +0100 +++ b/receiver/main.c Wed Nov 23 14:22:28 2011 +0100 @@ -12,149 +12,58 @@ #include "util/delay.h" -static char buffer[RS232_BUFSIZE+1]; -static uint8_t buffer_len; - - - - -// USART0 RX interrupt ISR ( USART_RXC_vect ) { - char c = UDR; +} - // check for buffer overflow - if (buffer_len==sizeof(buffer)) { - buffer_len=0; - if (c == 27) { - // escape sequence, store to empty buffer - buffer[buffer_len++] = c; - } - } else { - // collect characters until end of line - if (c == 27) { - // escape sequence, clear buffer - buffer_len = 0; - buffer[buffer_len++] = c; - } else if ( (c==0xff) && (buffer_len > 3) ) { - buffer[buffer_len]=0; - - // packet end received, parse the received packet - - // wait for the next packet - buffer_len=0; - } else { - buffer[buffer_len++]=c; - } - } -} +// PD2 / PD3 = INT0 / INT1 +// connect IR receiver to these pins to measure frequencies -#define PULSE_PORT PORTD -#define PULSE_BIT PD2 - -volatile uint16_t data = 0; -volatile uint8_t data_len = 0; -volatile uint8_t bitbuf_len = 0; -volatile uint16_t bitbuf = 0; - -ISR ( INT0_vect ) { - writeBit(PORTD, 5, 1); - GICR &= ~_BV(INT0) ; // Disable INT0 - // Startsignal erkannt, ab hier den Timer2 starten, - // der liest dann alle 50µs den Zustand ein und schreibt das - // empfangene Bit in den Puffer - bitbuf = 0; // init - bitbuf_len = 0b10000000; // init 1 pulse received - TCNT2 = 0; - TIMSK |= _BV(OCIE2); //enable timer2 interrupt - writeBit(PORTD, 5, 0); -} - - -ISR ( TIMER2_COMP_vect ) { - writeBit(PORTD, 4, 0); - - uint8_t clock; - uint8_t state; - uint8_t state2; - if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff; - if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff; - if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0; else state2 = 0xff; - - if (clock) { - // second pulse of bit - bitbuf_len &= ~_BV(7); // switch clock to low - if ((state==state2) & state2) { - // two cycles high: packet end received - data_len = (bitbuf_len & 0b00111111); - if (data_len == 13) PORTD ^= _BV(6); // debug sync output on program packets - TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt - GICR |= _BV(INT0) ; // Enable INT0 - data = bitbuf; // output data - } else { - bitbuf_len++; // increment bit counter - bitbuf = bitbuf << 1; // shift bits - if (state2 == 0) bitbuf |= 1; // receive logic one - } - } else { - // first pulse of bit - bitbuf_len |= _BV(7); // switch clock to high - if (state2) { - bitbuf_len |= _BV(6); // store new state - } else { - bitbuf_len &= ~_BV(6); // store new state +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; } } - - - writeBit(PORTD, 4, 1); + return 0; // timeout or incorrect measurement } - int main(void) { - uint8_t i; + uint16_t i; unsigned char s[30]; - // setup data bit timer - TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match - OCR2 = TIMER2_50US; - TIMSK |= 1<<OCIE2; //enable timer2 interrupt - - MCUCR = _BV(ISC00); // falling edge - GICR = _BV(INT0) ; // Enable INT0 + RS232_init(); // initialize RS232 interface + RS232_puts_p(PSTR("Car ID Scanner v0.1\n")); - writeBit(DDRD, 4, 1); - writeBit(DDRD, 5, 1); - writeBit(DDRD, 6, 1); - - - RS232_init(); // initialize RS232 interface - RS232_puts_p(PSTR("CarreraShark 1.0 - INIT OK\n")); - //RS232_puts_p(PSTR("Receiving one complete cycle:\n")); - - sei(); - i = 0; while (1) { // main loop - if (data != 0) { - if (data_len > 5) { - if (data_len == 13) { // sync to first packet - i = 1; - RS232_puts("\n"); - } else if (i!=0) i++; - if (i>0) { - itoa( data, s, 16); - data = 0; - RS232_puts("0x"); - RS232_puts( s ); - RS232_putc(' '); - } - } - //if (i==10) for (;;); + i = get_car(_BV(PD2)); + if (i > 0) { + itoa ( i , s, 10); + RS232_puts(s); + RS232_putc('\n'); } + } // main loop end };