bicycle_odometer

changeset 4:f883847a0591 tip

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Aug 2011 06:16:00 +0300
parents c0c68988bcdf
children
files odometer.c
diffstat 1 files changed, 17 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/odometer.c	Thu Aug 25 08:22:19 2011 +0300
     1.2 +++ b/odometer.c	Fri Aug 26 06:16:00 2011 +0300
     1.3 @@ -36,7 +36,8 @@
     1.4  #define PD_DATA_MASK	0xf
     1.5  #define PD_LEDS_MASK	0xc0
     1.6  
     1.7 -#define RPS_TO_KMH(x)	((36 * WHEEL_PERIMETER * (x)) / 1000)
     1.8 +/* 1800 2sec intervals per hour, \times perimeter, \times x, \over 100000 cm in a kilometer */
     1.9 +#define RP2S_TO_KMH(x)	((18 * WHEEL_PERIMETER * (x)) / 1000)
    1.10  
    1.11  #define DEBOUNCE_TICKS	1
    1.12  
    1.13 @@ -49,7 +50,7 @@
    1.14  
    1.15  void init_timer(void);
    1.16  void latch(int n);
    1.17 -void update_display(void);
    1.18 +void update_display(int idx);
    1.19  void disp_speed(int n);
    1.20  void disp_dist(int n);
    1.21  void disp_leds(int n);
    1.22 @@ -60,7 +61,7 @@
    1.23  volatile long ticks;
    1.24  
    1.25  unsigned long nrot;
    1.26 -unsigned long speed_rps;
    1.27 +unsigned long speed_rp2s;
    1.28  
    1.29  int main(void)
    1.30  {
    1.31 @@ -148,28 +149,29 @@
    1.32  		if(state) {
    1.33  			nrot++;
    1.34  
    1.35 -			update_display();
    1.36 +			/*update_display();*/
    1.37  		}
    1.38 +		prev_time = ticks;
    1.39  	}
    1.40 -
    1.41 -	prev_time = ticks;
    1.42  }
    1.43  
    1.44  ISR(TIMER0_OVF_vect)
    1.45  {
    1.46 -	volatile static int sec_ticks;
    1.47 -	volatile static unsigned long last_rot;
    1.48 +	volatile static int sec_ticks, sec;
    1.49 +	volatile static unsigned long last_rot[4];
    1.50  
    1.51  	ticks++;
    1.52  	sec_ticks++;
    1.53  
    1.54 -	if(sec_ticks >= SEC_TICKS) {
    1.55 -		speed_rps = nrot - last_rot;
    1.56 +	if(sec_ticks >= SEC_TICKS / 2) {
    1.57 +		int idx = ++sec & 3;
    1.58  
    1.59 -		update_display();
    1.60 +		speed_rp2s = nrot - last_rot[idx];
    1.61 +
    1.62 +		update_display(idx);
    1.63  
    1.64  		sec_ticks = 0;
    1.65 -		last_rot = nrot;
    1.66 +		last_rot[idx] = nrot;
    1.67  	}
    1.68  
    1.69  
    1.70 @@ -183,11 +185,11 @@
    1.71  	}
    1.72  }
    1.73  
    1.74 -void update_display(void)
    1.75 +void update_display(int idx)
    1.76  {
    1.77  	if(debug_mode) {
    1.78  		disp_dist(nrot);
    1.79 -		disp_speed(speed_rps);
    1.80 +		disp_speed(speed_rp2s);
    1.81  
    1.82  		if(state) {
    1.83  			PORTD |= (1 << 5);
    1.84 @@ -196,7 +198,7 @@
    1.85  		}
    1.86  	} else {
    1.87  		unsigned long dist_cm = (nrot * WHEEL_PERIMETER);
    1.88 -		int speed_kmh = RPS_TO_KMH(speed_rps);
    1.89 +		int speed_kmh = RP2S_TO_KMH(speed_rp2s);
    1.90  
    1.91  		disp_dist(dist_cm / 10000);
    1.92  		disp_speed(speed_kmh);