receiver/main.c

changeset 15
7d4c0c816465
parent 11
69c2a1408619
child 17
9e6feafc19e1
equal deleted inserted replaced
14:d115c088b508 15:7d4c0c816465
10 10
11 #include "driver/rs232.h" 11 #include "driver/rs232.h"
12 #include "util/delay.h" 12 #include "util/delay.h"
13 13
14 14
15 static char buffer[RS232_BUFSIZE+1]; 15 ISR ( USART_RXC_vect ) {
16 static uint8_t buffer_len; 16 }
17
18 // PD2 / PD3 = INT0 / INT1
19 // connect IR receiver to these pins to measure frequencies
17 20
18 21
19 22 uint8_t get_car(uint8_t pin) {
20 23 uint8_t i=0xff;
21 // USART0 RX interrupt 24 // wait ~~ Xµs for low signal
22 ISR ( USART_RXC_vect ) { 25 while ( (PIN(PORTD) & pin) != 0) {
23 char c = UDR; 26 _delay_us(5);
24 27 i--;
25 // check for buffer overflow 28 if (i==0) return 0; // no low signal, do not longer block
26 if (buffer_len==sizeof(buffer)) { 29 }
27 buffer_len=0; 30 // wait until signal is high again to start measurement
28 if (c == 27) { 31 while ( (PIN(PORTD) & pin) == 0) ;
29 // escape sequence, store to empty buffer 32 i = 0;
30 buffer[buffer_len++] = c; 33 while (i<100) {
31 } 34 _delay_us(4);
32 } else { 35 i++;
33 // collect characters until end of line 36 if ( (PIN(PORTD) & pin) == 0) { // return car ID
34 if (c == 27) { 37 if ( (i>= 5) & (i<= 9) ) return 1; // 05-09 = car1
35 // escape sequence, clear buffer 38 if ( (i>=13) & (i<=16) ) return 2; // 13-16 = car2
36 buffer_len = 0; 39 if ( (i>=19) & (i<=22) ) return 3; // 19-22 = car3
37 buffer[buffer_len++] = c; 40 if ( (i>=28) & (i<=31) ) return 4; // 28-31 = car4
38 } else if ( (c==0xff) && (buffer_len > 3) ) { 41 // debug: return higher values:
39 buffer[buffer_len]=0; 42 if (i>32) return i;
40
41 // packet end received, parse the received packet
42
43 // wait for the next packet
44 buffer_len=0;
45 } else {
46 buffer[buffer_len++]=c;
47 } 43 }
48 } 44 }
45 return 0; // timeout or incorrect measurement
49 } 46 }
50
51
52 #define PULSE_PORT PORTD
53 #define PULSE_BIT PD2
54
55 volatile uint16_t data = 0;
56 volatile uint8_t data_len = 0;
57 volatile uint8_t bitbuf_len = 0;
58 volatile uint16_t bitbuf = 0;
59
60 ISR ( INT0_vect ) {
61 writeBit(PORTD, 5, 1);
62 GICR &= ~_BV(INT0) ; // Disable INT0
63 // Startsignal erkannt, ab hier den Timer2 starten,
64 // der liest dann alle 50µs den Zustand ein und schreibt das
65 // empfangene Bit in den Puffer
66 bitbuf = 0; // init
67 bitbuf_len = 0b10000000; // init 1 pulse received
68 TCNT2 = 0;
69 TIMSK |= _BV(OCIE2); //enable timer2 interrupt
70 writeBit(PORTD, 5, 0);
71 }
72
73
74 ISR ( TIMER2_COMP_vect ) {
75 writeBit(PORTD, 4, 0);
76
77 uint8_t clock;
78 uint8_t state;
79 uint8_t state2;
80 if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff;
81 if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff;
82 if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0; else state2 = 0xff;
83
84 if (clock) {
85 // second pulse of bit
86 bitbuf_len &= ~_BV(7); // switch clock to low
87 if ((state==state2) & state2) {
88 // two cycles high: packet end received
89 data_len = (bitbuf_len & 0b00111111);
90 if (data_len == 13) PORTD ^= _BV(6); // debug sync output on program packets
91 TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt
92 GICR |= _BV(INT0) ; // Enable INT0
93 data = bitbuf; // output data
94 } else {
95 bitbuf_len++; // increment bit counter
96 bitbuf = bitbuf << 1; // shift bits
97 if (state2 == 0) bitbuf |= 1; // receive logic one
98 }
99 } else {
100 // first pulse of bit
101 bitbuf_len |= _BV(7); // switch clock to high
102 if (state2) {
103 bitbuf_len |= _BV(6); // store new state
104 } else {
105 bitbuf_len &= ~_BV(6); // store new state
106 }
107 }
108
109
110 writeBit(PORTD, 4, 1);
111 }
112
113 47
114 48
115 int main(void) 49 int main(void)
116 { 50 {
117 uint8_t i; 51 uint16_t i;
118 unsigned char s[30]; 52 unsigned char s[30];
119 53
120 // setup data bit timer 54 RS232_init(); // initialize RS232 interface
121 TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match 55 RS232_puts_p(PSTR("Car ID Scanner v0.1\n"));
122 OCR2 = TIMER2_50US;
123 TIMSK |= 1<<OCIE2; //enable timer2 interrupt
124 56
125 MCUCR = _BV(ISC00); // falling edge
126 GICR = _BV(INT0) ; // Enable INT0
127
128 writeBit(DDRD, 4, 1);
129 writeBit(DDRD, 5, 1);
130 writeBit(DDRD, 6, 1);
131
132
133 RS232_init(); // initialize RS232 interface
134 RS232_puts_p(PSTR("CarreraShark 1.0 - INIT OK\n"));
135 //RS232_puts_p(PSTR("Receiving one complete cycle:\n"));
136
137 sei();
138 i = 0;
139 while (1) { 57 while (1) {
140 // main loop 58 // main loop
141 59
142 if (data != 0) { 60 i = get_car(_BV(PD2));
143 if (data_len > 5) { 61 if (i > 0) {
144 if (data_len == 13) { // sync to first packet 62 itoa ( i , s, 10);
145 i = 1; 63 RS232_puts(s);
146 RS232_puts("\n"); 64 RS232_putc('\n');
147 } else if (i!=0) i++;
148 if (i>0) {
149 itoa( data, s, 16);
150 data = 0;
151 RS232_puts("0x");
152 RS232_puts( s );
153 RS232_putc(' ');
154 }
155 }
156 //if (i==10) for (;;);
157 } 65 }
66
158 } // main loop end 67 } // main loop end
159 }; 68 };
160 69

mercurial