1 | /*
|
2 | * Tacho.c
|
3 | *
|
4 | * Created: 18.04.2012 21:55:02
|
5 | * Author: Philipp
|
6 | */
|
7 | #define F_CPU 8000000UL
|
8 | #define REIFENUMFANG 85
|
9 | #define MEASURE_ITERATIONS 3
|
10 |
|
11 | #include <util/delay.h>
|
12 |
|
13 | #include "Tacho.h"
|
14 |
|
15 | volatile uint8_t timerOverflows = 0;
|
16 | volatile uint16_t timerStart = 0;
|
17 | volatile uint16_t timerEnd = 0;
|
18 | volatile uint32_t frequenz = 0;
|
19 | volatile uint8_t workFlag = 0;
|
20 |
|
21 | ISR(TIMER1_CAPT_vect) {
|
22 | // Solange der aktelle Zustand noch nicht ausgegeben ist am LCD, braucht man ihn auch nicht neu berechnen ...
|
23 | if(workFlag)
|
24 | return;
|
25 |
|
26 | static uint8_t durchgang = 0;
|
27 |
|
28 | if(durchgang == 0) {
|
29 | timerStart = ICR1;
|
30 | frequenz = 0;
|
31 | timerOverflows = 0;
|
32 | } else {
|
33 | timerEnd = ICR1;
|
34 | frequenz += 1000000 / ((timerOverflows*0xFFFF) + timerEnd - timerStart);
|
35 | timerOverflows = 0;
|
36 | timerStart = timerEnd;
|
37 | }
|
38 | if(durchgang == MEASURE_ITERATIONS) {
|
39 | frequenz /= MEASURE_ITERATIONS;
|
40 | durchgang = 0;
|
41 | workFlag = 1;
|
42 | } else
|
43 | durchgang++;
|
44 | }
|
45 |
|
46 | ISR(TIMER1_OVF_vect) {
|
47 | timerOverflows++;
|
48 | }
|
49 |
|
50 | int main(void)
|
51 | {
|
52 |
|
53 | // Initialize the LCD
|
54 | LCDInit();
|
55 |
|
56 | // Show Logo
|
57 | LCDShowBitmap(35, 0, Logo);
|
58 |
|
59 | // Set PB0 input, disabled Pull-Up
|
60 | DDRB &= ~(0x01 << PB0);
|
61 | PORTB &= ~(0x01 << PB0);
|
62 |
|
63 | // Initialize Timer1 for mensuring rpm
|
64 | TCCR1A = 0x00;
|
65 |
|
66 | // Noise-Canceler active
|
67 | // Prescaler = 8 => 1MHz
|
68 | // ICP-> Rising-Edge
|
69 | TCCR1B = (0x01 << ICNC1) | (0x01 << ICES1) | (0x01 << CS11);
|
70 |
|
71 | // Enable interrupts
|
72 | // Input Capture Interrupt Enable
|
73 | // Overflow Interrupt Enable
|
74 | TIMSK = (0x01 << TICIE1) | (0x01 << TOIE1);
|
75 |
|
76 | uint8_t kmh = 0;
|
77 |
|
78 | sei();
|
79 |
|
80 | // Main-Loop
|
81 | while(1) {
|
82 | // Wenn die frequenz-messung abgeschlossen ist (3Messungen)
|
83 | if(workFlag) {
|
84 | // Frequenz: Umdrehungen pro Sekunde => *60: Umdrehungen pro Minute => *60: Umdrehungen pro Stunde
|
85 | // => *Reifenumfang in cm: cm pro Stunde => /1000*100: km/h
|
86 | //kmh = frequenz*60*60*REIFENUMFANG/100000;
|
87 |
|
88 | // Wert anzeigen
|
89 | char sSpeed[5];
|
90 | itoa(frequenz, sSpeed, 10);
|
91 | LCDSetBrush(LCD_COLOR_BLUE);
|
92 | LCDFillRect(64-10, 52, 7*5, 8); // Clear font
|
93 | LCDSetBrush(LCD_COLOR_WHITE);
|
94 | LCDPrintText(sSpeed, 64-10, 52);
|
95 |
|
96 | workFlag = 0;
|
97 | }
|
98 | _delay_ms(500);
|
99 | }
|
100 | }
|