-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTC.c
47 lines (38 loc) · 868 Bytes
/
RTC.c
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
#include "RTC.h"
#include <avr/io.h>
#include <avr/interrupt.h>
static volatile uint8_t ticks = 0;
static uint16_t getPrescalarValue(enum Prescalars prescalar){
switch (prescalar){
case NO_PRESCALAR: return 1;
case PRESCALAR_8: return 8;
case PRESCALAR_64: return 64;
case PRESCALAR_256: return 256;
case PRESCALAR_1024: return 1024;
default: return 1;
}
}
void initRealTimeClock(enum Prescalars prescalar, float periodInSec){
const uint16_t tickToInterapt = (F_CPU / getPrescalarValue(prescalar))
* periodInSec;
cli();
TCCR1A = 0;
TCCR1B = (1 << WGM12) | prescalar;
OCR1A = tickToInterapt;
TIMSK1 = (1 << OCIE1A);
sei();
}
uint8_t getDeltaTime(){
uint8_t oldSREG = SREG;
cli();
uint8_t result = ticks;
ticks = 0;
SREG = oldSREG;
return result;
}
bool isTimePast(){
return ticks > 0;
}
ISR(TIMER1_COMPA_vect){
ticks++;
}