bicycle_odometer

view odometer.c @ 4:f883847a0591

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Aug 2011 06:16:00 +0300
parents c0c68988bcdf
children
line source
1 /* PORT USAGE
2 *
3 * D0-D3: data bus, connected to the D0-D3 pins of all the 4511 chips
4 * C0-C5,D4,D5: directly driving the 8 speed graph LEDs
5 * B0-B2: connected to the decoder controlling the 4511 latches as such:
6 * 000 - speed most significant
7 * 001 - speed least significant
8 * 010 - dist most significant
9 * 011 - dist middle
10 * 100 - dist least significant
11 * 101 - all latches are off (high)
12 * B3-B5: ISP. B4 also acts as generic pushbutton input
13 * B6,B7: XTAL
14 * D6,D7: comparator, hall effect sensor input and threshold trimpot.
15 */
17 /* 16mhz resonator */
18 #define F_CPU 16000000
20 #include <avr/io.h>
21 #include <avr/interrupt.h>
22 #include <avr/power.h>
23 #include <util/delay.h>
26 #define SEC_TICKS (F_CPU / 256 / 256)
28 #define PRESCL_1 1
29 #define PRESCL_8 2
30 #define PRESCL_64 3
31 #define PRESCL_256 4
32 #define PRESCL_1024 5
35 #define PB_CSEL_MASK 0x7
36 #define PD_DATA_MASK 0xf
37 #define PD_LEDS_MASK 0xc0
39 /* 1800 2sec intervals per hour, \times perimeter, \times x, \over 100000 cm in a kilometer */
40 #define RP2S_TO_KMH(x) ((18 * WHEEL_PERIMETER * (x)) / 1000)
42 #define DEBOUNCE_TICKS 1
45 /* wheel perimeter in centimeters */
46 #define WHEEL_PERIMETER 194
48 #define nop() asm volatile("nop")
51 void init_timer(void);
52 void latch(int n);
53 void update_display(int idx);
54 void disp_speed(int n);
55 void disp_dist(int n);
56 void disp_leds(int n);
58 int state;
59 int debug_mode = 0;
61 volatile long ticks;
63 unsigned long nrot;
64 unsigned long speed_rp2s;
66 int main(void)
67 {
68 int i;
70 DDRB = 0xf; /* only first four bits are outputs */
71 DDRC = 0xff; /* speed leds 6 bits */
72 DDRD = 0x3f; /* 4bits data bus, 2 bits speed leds, 2 bits comparator (input) */
74 /* zero-out the displays and disable all pullups except for the reset pin */
75 PORTB = 0;
76 PORTC = 0x40; /* 6th bit set, C6 is reset */
77 PORTD = 0;
79 for(i=0; i<5; i++) {
80 latch(i);
81 }
83 /* if the button at B4 is pressed during startup, enter debug mode */
84 if((PINB >> 4) & 1) {
85 debug_mode = 1;
86 PORTC |= 3 << 4;
87 }
89 init_timer();
91 /* disable digital input buffer for the AINn pins */
92 DIDR1 = 0;
94 /* read initial comparator state */
95 state = (ACSR >> ACO) & 1;
97 /* in debug mode we want an interrupt both on rising and falling edge
98 * to be able to blink leds when the wheel goes past. otherwise we only need
99 * a rising edge interrupt to increment the rotation counter.
100 */
101 /* rising edge interrupt */
102 /*ACSR |= (2 << ACIS0);*/
104 /* comparator interrupt enable */
105 ACSR |= (1 << ACIE);
107 sei();
109 for(;;) {
110 }
111 return 0;
112 }
114 void init_timer(void)
115 {
116 power_timer0_enable();
118 TCCR0A = 0;
119 TCCR0B = PRESCL_256;
121 /* enable timer overflow interrupt */
122 TIMSK0 = 1;
123 }
125 void latch(int n)
126 {
127 unsigned char upper = PORTB & ~PB_CSEL_MASK;
128 /* pull latch low */
129 PORTB = upper | n;
130 asm volatile("nop");
131 /* pull all latches high again */
132 PORTB = upper | 5;
133 }
135 ISR(ANALOG_COMP_vect)
136 {
137 volatile static long prev_time;
138 unsigned char aco;
140 nop();
141 nop();
142 nop();
144 aco = (ACSR >> ACO) & 1;
146 if(aco != state && (ticks - prev_time) > DEBOUNCE_TICKS) {
147 state = aco;
149 if(state) {
150 nrot++;
152 /*update_display();*/
153 }
154 prev_time = ticks;
155 }
156 }
158 ISR(TIMER0_OVF_vect)
159 {
160 volatile static int sec_ticks, sec;
161 volatile static unsigned long last_rot[4];
163 ticks++;
164 sec_ticks++;
166 if(sec_ticks >= SEC_TICKS / 2) {
167 int idx = ++sec & 3;
169 speed_rp2s = nrot - last_rot[idx];
171 update_display(idx);
173 sec_ticks = 0;
174 last_rot[idx] = nrot;
175 }
178 if(debug_mode) {
179 unsigned char state = (ACSR >> ACO) & 1;
180 if(state) {
181 PORTD |= (1 << 5);
182 } else {
183 PORTD &= ~(1 << 5);
184 }
185 }
186 }
188 void update_display(int idx)
189 {
190 if(debug_mode) {
191 disp_dist(nrot);
192 disp_speed(speed_rp2s);
194 if(state) {
195 PORTD |= (1 << 5);
196 } else {
197 PORTD &= ~(1 << 5);
198 }
199 } else {
200 unsigned long dist_cm = (nrot * WHEEL_PERIMETER);
201 int speed_kmh = RP2S_TO_KMH(speed_rp2s);
203 disp_dist(dist_cm / 10000);
204 disp_speed(speed_kmh);
205 disp_leds(speed_kmh * 8);
206 }
207 }
209 void disp_speed(int n)
210 {
211 PORTD = (PORTD & ~PD_DATA_MASK) | (n % 10);
212 latch(1);
213 n /= 10;
214 PORTD = (PORTD & ~PD_DATA_MASK) | (n % 10);
215 latch(0);
216 }
218 void disp_dist(int n)
219 {
220 PORTD = (PORTD & ~PD_DATA_MASK) | (n % 10);
221 latch(4);
222 n /= 10;
223 PORTD = (PORTD & ~PD_DATA_MASK) | (n % 10);
224 latch(3);
225 n /= 10;
226 PORTD = (PORTD & ~PD_DATA_MASK) | (n % 10);
227 latch(2);
228 }
230 void disp_leds(int n)
231 {
232 int i, count, bits;
234 count = (n + 4) >> 5;
235 bits = 0;
237 for(i=0; i<count; i++) {
238 bits |= (1 << i);
239 }
241 PORTC = bits;
242 PORTD = (PORTD & PD_DATA_MASK) | ((bits & PD_LEDS_MASK) >> 2);
243 }