-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask 4 [practice time counting]
76 lines (59 loc) · 1.22 KB
/
Task 4 [practice time counting]
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
/*
* Zad4.c
*
* Created: 21.11.2022 15:54:34
* Author: Nemo
*/
// F_CPU/1024 = 1s
#include <avr/io.h>
#include <avr/interrupt.h>
int seconds = 0;
void preset(){
DDRC = 0xFF;
PORTC = 0xFF;
TCNT1 = 58336; // = 2^16 - F_CPU/1024
TCCR1B = (1<<CS10) | (1<<CS12);
// comment two lines below if you use any flag in TIFR for counting time
TIMSK = (1<<TOIE1);
sei();
}
int bin_bcd(int binaryInput){
int bcdResult = 0;
int shift = 0;
while (binaryInput > 0) {
bcdResult |= (binaryInput % 10) << (shift++ << 2);
binaryInput /= 10;
}
return bcdResult;
}
// comment this function if you use any flag in TIFR for counting time
ISR(TIMER1_OVF_vect){
if (seconds != 99) {seconds ++;}
else {seconds = 0;}
PORTC = ~bin_bcd(seconds);
TCNT1 = 58336; // = 2^16 - F_CPU/1024
}
int main(void)
{
preset();
while(1)
{
PORTC = ~bin_bcd(seconds);
/*
// TOV1
if (TIFR & 0b100) {
TIFR = 0xFF;
TCNT1 = 58336;
if (seconds != 99) {seconds ++;}
else {seconds = 0;}
}*/
/*
// OCF1A
if (TIFR & 0b10000) {
TIFR = 0xFF;
TCNT1 = 58336;
if (seconds != 99) {seconds ++;}
else {seconds = 0;}
}*/
}
}