Sun, 28 Oct 2012 18:46:03 +0100
typo
9 | 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" | |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
12 | #include "util/delay.h" |
9 | 13 | |
14 | ||
15 | ISR ( USART_RXC_vect ) { | |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
16 | } |
9 | 17 | |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
18 | // PD2 / PD3 = INT0 / INT1 |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
19 | // connect IR receiver to these pins to measure frequencies |
9 | 20 | |
17
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
21 | volatile uint8_t car0, car1; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
22 | uint8_t car_tmp0, car_tmp1, car_count0, car_count1, car_buffer0, car_buffer1; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
23 | #define CARDETECT_CYCLES 2 |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
24 | ISR ( TIMER0_OVF_vect ) { |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
25 | // divides f_cpu by 256 |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
26 | // Interrupt every 32µs |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
27 | |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
28 | if ( (PIN(PORTD) & _BV(PD2)) == 0) { |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
29 | car_tmp0++; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
30 | } else { |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
31 | // return car ID length |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
32 | if (car_tmp0 != 0) { |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
33 | car_count0++; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
34 | car_buffer0 += car_tmp0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
35 | if (car_count0 == CARDETECT_CYCLES) { |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
36 | car_buffer0 = (car_buffer0) / CARDETECT_CYCLES; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
37 | if (car_buffer0 == car_tmp0) car0 = car_tmp0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
38 | //car0 = car_buffer0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
39 | car_buffer0 = 0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
40 | car_count0 = 0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
41 | } |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
42 | car_tmp0 = 0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
43 | } |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
44 | } |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
45 | |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
46 | } |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
47 | |
9 | 48 | |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
49 | uint8_t get_car(uint8_t pin) { |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
50 | uint8_t i=0xff; |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
51 | // wait ~~ Xµs for low signal |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
52 | while ( (PIN(PORTD) & pin) != 0) { |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
53 | _delay_us(5); |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
54 | i--; |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
55 | if (i==0) return 0; // no low signal, do not longer block |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
56 | } |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
57 | // wait until signal is high again to start measurement |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
58 | while ( (PIN(PORTD) & pin) == 0) ; |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
59 | i = 0; |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
60 | while (i<100) { |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
61 | _delay_us(4); |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
62 | i++; |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
63 | if ( (PIN(PORTD) & pin) == 0) { // return car ID |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
64 | if ( (i>= 5) & (i<= 9) ) return 1; // 05-09 = car1 |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
65 | if ( (i>=13) & (i<=16) ) return 2; // 13-16 = car2 |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
66 | if ( (i>=19) & (i<=22) ) return 3; // 19-22 = car3 |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
67 | if ( (i>=28) & (i<=31) ) return 4; // 28-31 = car4 |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
68 | // debug: return higher values: |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
69 | if (i>32) return i; |
9 | 70 | } |
71 | } | |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
72 | return 0; // timeout or incorrect measurement |
9 | 73 | } |
74 | ||
75 | ||
76 | int main(void) | |
77 | { | |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
78 | uint16_t i; |
11
69c2a1408619
done code for sniffing the rails
Malte Bayer <mbayer@neo-soft.org>
parents:
10
diff
changeset
|
79 | unsigned char s[30]; |
17
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
80 | |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
81 | // initialize timer0 for IR signal detection |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
82 | TCCR0 = _BV(CS00); // system clock, no prescaling |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
83 | TIMSK = _BV(TOIE0); |
9 | 84 | |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
85 | RS232_init(); // initialize RS232 interface |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
86 | RS232_puts_p(PSTR("Car ID Scanner v0.1\n")); |
17
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
87 | sei(); |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
88 | |
9 | 89 | while (1) { |
90 | // main loop | |
91 | ||
17
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
92 | /* |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
93 | i = get_car(_BV(PD2)); |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
94 | if (i > 0) { |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
95 | itoa ( i , s, 10); |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
96 | RS232_puts(s); |
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
97 | RS232_putc('\n'); |
9 | 98 | } |
17
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
99 | */ |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
100 | |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
101 | if (car0 > 0) { |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
102 | itoa ( car0 , s, 10); |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
103 | car0 = 0; |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
104 | RS232_puts(s); |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
105 | RS232_putc('\n'); |
9e6feafc19e1
added timer0 based car id detection - works only on 50/50 input signal
Malte Bayer <mbayer@neo-soft.org>
parents:
15
diff
changeset
|
106 | } |
15
7d4c0c816465
done working car id measurement via short-blocking function call (slot1-4 for now)
Malte Bayer <mbayer@neo-soft.org>
parents:
11
diff
changeset
|
107 | |
9 | 108 | } // main loop end |
109 | }; | |
110 |