receiver/main.c

changeset 9
20dbe0546a36
child 10
6d6e982bbc41
equal deleted inserted replaced
8:84249ab5d691 9:20dbe0546a36
1 #include <avr/interrupt.h>
2 #include <avr/io.h>
3 #include <avr/wdt.h>
4 #include <avr/eeprom.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <avr/pgmspace.h>
8
9 #include "main.h"
10
11 #include "driver/rs232.h"
12 //#include "driver/manchester.h"
13
14 volatile uint8_t datalen = 0;
15 char data[10]; // 8 bytes data buffer + string termination
16
17 static char buffer[RS232_BUFSIZE+1];
18 static uint8_t buffer_len;
19
20 volatile uint8_t bitbuf_len = 0;
21 volatile uint16_t bitbuf = 0;
22 volatile uint16_t bitbuf_out = 0;
23 volatile uint8_t bitbuf_out_len = 0;
24 volatile uint8_t timeout = 0;
25 volatile uint8_t lastbit = 0;
26
27
28
29 // USART0 RX interrupt
30 ISR ( USART_RXC_vect ) {
31 char c = UDR;
32
33 // check for buffer overflow
34 if (buffer_len==sizeof(buffer)) {
35 buffer_len=0;
36 if (c == 27) {
37 // escape sequence, store to empty buffer
38 buffer[buffer_len++] = c;
39 }
40 } else {
41 // collect characters until end of line
42 if (c == 27) {
43 // escape sequence, clear buffer
44 buffer_len = 0;
45 buffer[buffer_len++] = c;
46 } else if ( (c==0xff) && (buffer_len > 3) ) {
47 buffer[buffer_len]=0;
48
49 // packet end received, parse the received packet
50
51 // wait for the next packet
52 buffer_len=0;
53 } else {
54 buffer[buffer_len++]=c;
55 }
56 }
57 }
58
59
60 #define PULSE_IN PIND
61 #define PULSE_BIT 2
62
63 #define TIMER_DIVISOR 8
64 #define VALUE_100US 0.47e-4
65 #define TIMER_100US 0xff - (uint8_t)(VALUE_100US * F_CPU/TIMER_DIVISOR)
66
67 ISR ( INT0_vect ) {
68 writeBit(GICR, INT0, 0); // disable INT0 interrupt
69 // Startsignal erkannt, ab hier den Timer0 starten,
70 // der liest dann alle 100µs den Zustand ein und schreibt das
71 // empfangene Bit in den Puffer
72 lastbit = 0b10000001; // initialize
73
74 TCNT0 = TIMER_100US;
75 TIMSK |= 1<<TOIE0; //enable timer0 interrupt
76 }
77
78 ISR ( TIMER0_OVF_vect ) {
79 uint8_t sr = SREG;
80 TCNT0 = TIMER_100US; // reset timer for next bit time
81 writeBit(PORTD, 4, 0);
82 // read the current bit value, store it to 16bit buffer
83 if ( (PULSE_IN & _BV(PULSE_BIT)) == 0 ) {
84 //RS232_putc('L');
85
86 if ( (lastbit & 1) == 1) {
87 // bit cleared, second block
88 // insert bit
89 bitbuf |= 1;
90 bitbuf = bitbuf << 1;
91 bitbuf_len++;
92 lastbit = 0; // set block number0 & signal state to 0
93 } else {
94 // bit cleared, first block
95 lastbit = 1; // set block number1 & signal state to 0
96 }
97 } else {
98 //RS232_putc('H');
99 if ( (lastbit & 1) == 1) {
100 // bit set, second block
101 if ( (lastbit & _BV(7)) == 0) {
102 // insert zero bit
103 bitbuf = bitbuf << 1;
104 bitbuf_len++;
105 lastbit = 0b10000000; // set block number0 & signal state to 1
106 } else {
107 // no change, transmission END!
108 lastbit = 0xff;
109 }
110 } else {
111 // bit set, first block
112 lastbit = 0b10000001; // set block number1 & signal state to 1
113 }
114 }
115 if ( (bitbuf_len == 16) | (lastbit == 0xFF) ) {
116 // 16 bits full or transmission end
117 // export to uart
118 writeBit(TIMSK, TOIE0, 0); // disable timer0 interrupt
119 if (bitbuf > 0) {
120 bitbuf_out = bitbuf;
121 bitbuf_out_len = bitbuf_len;
122 }
123 bitbuf = 0;
124 bitbuf_len = 0;
125 GICR |= _BV(INT0); // Enable INT0
126 }
127
128 writeBit(PORTD, 4, 1);
129 SREG = sr;
130 }
131
132 volatile uint8_t pulse_counter = 0;
133 volatile uint8_t measure_progress = 0;
134 volatile uint8_t car_id = 0;
135 ISR(TIMER2_OVF_vect) {
136 //TCNT2 = 0x90;
137 pulse_counter++;
138 }
139
140 ISR (INT1_vect) {
141 // measure pulse width
142 //RS232_putc('x');
143 if (measure_progress == 0) {
144 // start measuring
145 pulse_counter = 0;
146 measure_progress = 1;
147 // set INT1 to rising edge (stops current measurement)
148 //MCUCR |= _BV(ISC10);
149 writeBit(PORTD, 4, 1);
150 } else {
151 // stop measuring
152 car_id = pulse_counter;
153 measure_progress = 0;
154 // set INT1 to falling edge (initiates next measurement)
155 //MCUCR &= ~_BV(ISC10);
156 writeBit(PORTD, 4, 0);
157 }
158 }
159
160
161 int main(void)
162 {
163 uint i;
164 char s[30];
165
166 TCCR0 = 0; //timer off
167 //TCCR0 = (1<<CS00); //divide by 1
168 TCCR0 = (1<<CS01); //divide by 8
169 //TCCR0 = (1<<CS01) | (1<<CS00); //divide by 64
170 //TCCR0 = (1<<CS02); //divide by 256
171 //TCCR0 = (1<<CS02) | (1<<CS00); //divide by 1024
172
173 //TIMSK = 1<<TOIE0; //enable timer interrupt
174 // Timer will be enabled on INT0
175
176 // configure timer2 for pulse measurement
177 //TCCR2 = (1<<CS21); // divide 8
178 TCCR2 = (1<<CS20); // divide 1
179 TIMSK = 1<<TOIE2;
180
181 // configure INT0+1 for falling edge
182 MCUCR = _BV(ISC00) | _BV(ISC11);
183 // GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0/2
184 GICR = _BV(INT1) ; // Enable INT0/2
185
186 writeBit(DDRD, 4, 1);
187
188
189 RS232_init(); // initialize RS485 interface
190 RS232_puts_p(PSTR("INIT OK\n"));
191
192 itoa (TIMER_100US, s, 10);
193 RS232_puts(s);
194
195 RS232_puts_p(PSTR(" timer\n"));
196 i = 0;
197
198 sei();
199 while (1) {
200 // main loop
201
202 if (car_id != 0) {
203 itoa( car_id, s, 10);
204 car_id = 0;
205 RS232_puts( s );
206 RS232_putc('\n');
207
208 }
209 /*cli();
210 i = rc5_data; // read two bytes from interrupt !
211 rc5_data = 0;
212 sei();
213 if (i) {
214 itoa( i, s, 10);
215 RS232_puts( s );
216 } */
217 /*
218 if (p_len < 0xFF) p_len++;
219 if( (p_bit ^ PULSE_IN) & 1<<PULSE_BIT ){ // change detect
220 p_bit = ~p_bit; // 0x00 -> 0xFF -> 0x00
221 itoa( p_len, s, 10);
222 RS232_puts(s);
223 RS232_puts_p(PSTR("\n"));
224 p_len = 0;
225 }
226 */
227 } // main loop end
228 };
229

mercurial