diff options
Diffstat (limited to 'Tacho_screen.c')
-rw-r--r-- | Tacho_screen.c | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/Tacho_screen.c b/Tacho_screen.c index 9b5a138..ea7cf55 100644 --- a/Tacho_screen.c +++ b/Tacho_screen.c @@ -8,10 +8,10 @@ #include <util/delay.h> #include <avr/interrupt.h> -#define _PRIM PORTC -#define _SEC PORTD +#define _PRIM PORTC //Right digit +#define _SEC PORTD //Left digit -#define _WHEEL_SIZE 207 +#define _WHEEL_SIZE 207 //In cm //Define characters (negative) #define _C1 0b10101111 @@ -27,25 +27,25 @@ #define _C_ 0b11111101 #define _CCLEAR 255 -uint32_t cntr; -unsigned int speed; +uint32_t cntr; //counter for Timer +unsigned int speed; //global var to save the calculated speed in -ISR(TIMER0_OVF_vect) +ISR(TIMER0_OVF_vect) //ISR for TIMER0 overflow { cntr++; } inline void calcSpeed(void) { - float tmrSpeed = F_CPU / 256; - float tmrTime = 1 / tmrSpeed; - float comTime = tmrTime * cntr; + float tmrSpeed = F_CPU / 256; //The timer overflow speed in Hz + float tmrTime = 1 / tmrSpeed; //The timer overflow speed in s + float comTime = tmrTime * cntr; //calculate the total time - float mps = (_WHEEL_SIZE / 100) / comTime; + float mps = (_WHEEL_SIZE / 100) / comTime; //Convert wheelsize to m and derive by the complete time - float kmph = mps * 3.6; + float kmph = mps * 3.6; //km/h = m/s * 3.6 - speed = (int)kmph; + speed = (int)kmph; //cast to int and write into global var speed }//calcSpeed() inline void render(int _screen) @@ -55,7 +55,10 @@ inline void render(int _screen) int i1, i2; itoa(_screen, cNr, 10); //convert int to string for rendering - + /* + * If _screen is smaller than 10, the cNr contains 1xxxxxxxxxx, but if it is bigger than 10 it looks like this: 10xxxxxxxxxx + * So I have to change the digits + */ if(_screen < 10) { i1 = 0; @@ -159,24 +162,24 @@ int main(void) TCCR0 = (1 << CS00); // Prescaler 1 TIMSK = (1<<TOIE0); //Enable Overflowinterrupt - cntr = 0; //counter for Timer - DDRC = DDRD = 255; // Output for scrren pins + cntr = 0; //init counter for Timer + DDRC = DDRD = 255; // Output for screen pins DDRB |= (1<<PB2); DDRB &= ~(1<<PB1); - sei(); + sei(); //enable interrupts - render(0); + render(0); //init the display while(1)//main loop { - while(PINB & (1 << PB1) && cntr < 30000){} // wait for reed + while(PINB & (1 << PB1) && cntr < 30000){} // wait for reedcontact on PB1 TCCR0 = ~(1 << CS00); //disable counter cli(); //disable interrupts calcSpeed(); //reset all variables - cntr = 0; - TCNT0 = 0; + cntr = 0; //Reset counter + TCNT0 = 0; //Reset Timer sei();//enable interrupts TCCR0 = (1 << CS00); //enable counter |