-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTime.cpp
72 lines (55 loc) · 1.31 KB
/
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
68
69
70
#include "Time.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
#include <util/delay.h>
volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_clock_cycles = 0;
volatile unsigned long timer0_millis = 0;
void init_time()
{
// set timer 0 prescale factor to 64
TCCR0B = _BV(CS01) | _BV(CS00);
// enable timer 0 overflow interrupt
TIMSK0 = _BV(TOIE0);
}
void wait(unsigned long t)
{
_delay_ms(t);
}
unsigned long millis()
{
unsigned long m;
// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of the timer0_millis++)
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
m = timer0_millis;
}
return m;
}
unsigned long micros() {
unsigned long m, t;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
t = TCNT0;
#ifdef TIFR0
if ((TIFR0 & _BV(TOV0)) && (t == 0))
t = 256;
#else
if ((TIFR & _BV(TOV0)) && (t == 0))
t = 256;
#endif
m = timer0_overflow_count;
}
return ((m << 8) + t) * (64 / cyclesPerMicro());
}
ISR(TIMER0_OVF_vect)
{
timer0_overflow_count++;
// timer 0 prescale factor is 64 and the timer overflows at 256
timer0_clock_cycles += 64UL * 256UL;
while (timer0_clock_cycles > cyclesPerMicro() * 1000UL) {
timer0_clock_cycles -= cyclesPerMicro() * 1000UL;
timer0_millis++;
}
}