-
Notifications
You must be signed in to change notification settings - Fork 6
/
time.cpp
67 lines (50 loc) · 987 Bytes
/
time.cpp
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
/*
* time.cpp
*
* Created: 01.02.2020 23:56:06
* Author: Pavel
*/
#include "time.h"
void Time::init(){
TIMSK0 |= (1 << TOIE0);
TCCR0B |= (1 << CS01) | (1 << CS00);
}
Time::Time(){
timer0_overflow_count=0;
timer0_millis =0;
timer0_fract=0;
}
unsigned long Time::millis()
{
unsigned long m;
uint8_t oldSREG = SREG;
cli();
m = timer0_millis;
SREG = oldSREG;
return m;
}
unsigned long Time::micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;
cli();
m = timer0_overflow_count;
t = TCNT0;
if ((TIFR0 & _BV(TOV0)) && (t < 255))
m++;
SREG = oldSREG;
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
//Ïðåðûâàíèå äëÿ òàéìåðà. Äëÿ ïîäñ÷åòà millis è micros
ISR (TIMER0_OVF_vect) {
unsigned long m = time.timer0_millis;
unsigned char f = time.timer0_fract;
m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}
time.timer0_fract = f;
time.timer0_millis = m;
time.timer0_overflow_count++;
}