summaryrefslogtreecommitdiff
path: root/Tacho_screen.c
blob: df8d34cf33bfd689236dbe240b9d7a37199b181c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Tacho.c
 *
 * Created: 19.06.2014 15:33:40
 *  Author: Jonas
 */ 

#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#define _PRIM PORTC
#define _SEC  PORTD

#define _WHEEL_SIZE 207

//Define characters (negative)
#define _C1 0b10101111//0b01010000
#define _C2 0b11001000//0b00110111
#define _C3 0b10101000//0b00000000
#define _C4 0b10100101//0b00000000
#define _C5 0b10110000//0b00000000
#define _C6 0b10010001//0b00000000
#define _C7 0b10101110//0b00000000
#define _C8 0b10000000//0b00000000
#define _C9 0b10100000//0b00000000
#define _C0 0b10000010//0b00000000
#define _C_ 0b11111101
#define _CCLEAR 255
 
uint32_t cntr; 
unsigned int speed;
 
ISR(TIMER0_OVF_vect)
{
	cntr++;
}

inline void calcSpeed(void)
{
	float tmrSpeed = F_CPU / 256;
	float tmrTime  = 1 / tmrSpeed;
	float comTime  = tmrTime * cntr;
	
	float mps      = (_WHEEL_SIZE / 100) / comTime;
	
	float kmph     = mps * 3.6;
	
	speed = (int)kmph;
}//calcSpeed()

inline void render(int _screen)
{
	char cNr[16];
	
	int i1, i2;
	
	itoa(_screen, cNr, 10); //convert int to string for rendering
	
	if(_screen < 10)
	{
		i1 = 0;
		i2 = 1;
		cNr[1] = '0';
	}
	else
	{
		i1 = 1;
		i2 = 0;
	}
	
	if(_screen > 99 || _screen < 0)
	{
		_SEC = _PRIM = _C_;
	}
	else
	{
		switch (cNr[i1]) //first character
		{
			case '0':
				_PRIM = _C0;
				PORTB &= ~(1<<PB2);
			break;
			case '1':
				_PRIM = _C1;
				PORTB &= ~(1<<PB2);
			break;
			case '2':
				_PRIM = _C2;
				PORTB |= (1<<PB2);
			break;
			case '3':
				_PRIM = _C3;
				PORTB &= ~(1<<PB2);
			break;
			case '4':
				_PRIM = _C4;
				PORTB &= ~(1<<PB2);
			break;
			case '5':
				_PRIM = _C5;
				PORTB &= ~(1<<PB2);
			break;
			case '6':
				_PRIM = _C6;
				PORTB &= ~(1<<PB2);
			break;
			case '7':
				_PRIM = _C7;
				PORTB &= ~(1<<PB2);
			break;
			case '8':
				_PRIM = _C8;
				PORTB &= ~(1<<PB2);
			break;
			case '9':
				_PRIM = _C9;
				PORTB &= ~(1<<PB2);
			break;
		}
		
		switch (cNr[i2])//second character
		{
			case '0':
				_SEC = _C0;
			break;
			case '1':
				_SEC = _C1;
			break;
			case '2':
				_SEC = _C2;
			break;
			case '3':
				_SEC = _C3;
			break;
			case '4':
				_SEC = _C4;
			break;
			case '5':
				_SEC = _C5;
			break;
			case '6':
				_SEC = _C6;
			break;
			case '7':
				_SEC = _C7;
			break;
			case '8':
				_SEC = _C8;
			break;
			case '9':
				_SEC = _C9;
			break;
		}
	}
}//render()

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
	DDRB |= (1<<PB2);
	DDRB &= ~(1<<PB1);
	
	sei();
	
	render(0);
	
	//PORTC = 0b01010000;
    while(1)//main loop
	{	
		while(PINB & (1 << PB1) && cntr < 30000){} // wait for reed
		TCCR0 = ~(1 << CS00); //disable counter
		cli(); //disable interrupts
		calcSpeed();
		//reset all variables
		cntr = 0;
		TCNT0 = 0;
		sei();//enable interrupts
		TCCR0 = (1 << CS00); //enable counter
		
		render(speed);//render the calculated speed
		_delay_ms(100);
	}
}//main()