# HG changeset patch # User John Tsiombikas # Date 1314328560 -10800 # Node ID f883847a059114ecdd658d8fcb7dfbfe5208df1b # Parent c0c68988bcdffa9d39178599b88b0e48ee2b7479 foo diff -r c0c68988bcdf -r f883847a0591 odometer.c --- a/odometer.c Thu Aug 25 08:22:19 2011 +0300 +++ b/odometer.c Fri Aug 26 06:16:00 2011 +0300 @@ -36,7 +36,8 @@ #define PD_DATA_MASK 0xf #define PD_LEDS_MASK 0xc0 -#define RPS_TO_KMH(x) ((36 * WHEEL_PERIMETER * (x)) / 1000) +/* 1800 2sec intervals per hour, \times perimeter, \times x, \over 100000 cm in a kilometer */ +#define RP2S_TO_KMH(x) ((18 * WHEEL_PERIMETER * (x)) / 1000) #define DEBOUNCE_TICKS 1 @@ -49,7 +50,7 @@ void init_timer(void); void latch(int n); -void update_display(void); +void update_display(int idx); void disp_speed(int n); void disp_dist(int n); void disp_leds(int n); @@ -60,7 +61,7 @@ volatile long ticks; unsigned long nrot; -unsigned long speed_rps; +unsigned long speed_rp2s; int main(void) { @@ -148,28 +149,29 @@ if(state) { nrot++; - update_display(); + /*update_display();*/ } + prev_time = ticks; } - - prev_time = ticks; } ISR(TIMER0_OVF_vect) { - volatile static int sec_ticks; - volatile static unsigned long last_rot; + volatile static int sec_ticks, sec; + volatile static unsigned long last_rot[4]; ticks++; sec_ticks++; - if(sec_ticks >= SEC_TICKS) { - speed_rps = nrot - last_rot; + if(sec_ticks >= SEC_TICKS / 2) { + int idx = ++sec & 3; - update_display(); + speed_rp2s = nrot - last_rot[idx]; + + update_display(idx); sec_ticks = 0; - last_rot = nrot; + last_rot[idx] = nrot; } @@ -183,11 +185,11 @@ } } -void update_display(void) +void update_display(int idx) { if(debug_mode) { disp_dist(nrot); - disp_speed(speed_rps); + disp_speed(speed_rp2s); if(state) { PORTD |= (1 << 5); @@ -196,7 +198,7 @@ } } else { unsigned long dist_cm = (nrot * WHEEL_PERIMETER); - int speed_kmh = RPS_TO_KMH(speed_rps); + int speed_kmh = RP2S_TO_KMH(speed_rp2s); disp_dist(dist_cm / 10000); disp_speed(speed_kmh);