forked from cinderblock/3-Phase-Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.cpp
More file actions
75 lines (57 loc) · 1.44 KB
/
Clock.cpp
File metadata and controls
75 lines (57 loc) · 1.44 KB
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
/*
* File: Clock.cpp
* Author: Cameron
*
* Created on October 20, 2015, 12:16 AM
*/
#include "Clock.h"
#include "Timer.h"
void TIMER3_COMPA_vect() {
Clock::tick();
}
u4 Clock::time;
void Clock::init() {
Timer::init();
time = 0;
// Enable the overflow interrupt in the timer
TIMSK3 = 1 << OCIE3A;
}
void Clock::tick() {
time++;
}
void Clock::readTime(u4& dest) {
// Disable interrupts for the next few cycles
cli();
// Copy the time to the destination
dest = time;
// and re-enable the interrupts
sei();
}
void Clock::readTime(MicroTime& dest) {
// Disable interrupts for the next few cycles
cli();
// Copy the times we care about
u2 const micro = Timer::getCurTime();
u4 t = time;
// Save pending interrupts
const u1 pendingInts = TIMSK3;
// Re-enable the interrupts
sei();
// If micro is small and there's a pending overflow, the real time has incremented
if ((micro < 256) && (pendingInts & (1 << OCIE3A)))
t++;
// Copy the times to the destination
dest.setTimerCount(micro).setTicksCount(t);
}
void Clock::readTimeISR(MicroTime& dest) {
// Copy the times we care about
u2 const micro = Timer::getCurTime();
u4 t = time;
// Save pending interrupts
const u1 pendingInts = TIMSK3;
// If micro is small and there's a pending overflow, the real time has incremented
if ((micro < 256) && (pendingInts & (1 << OCIE3A)))
t++;
// Copy the times to the destination
dest.setTimerCount(micro).setTicksCount(t);
}