-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathget_millis.c
45 lines (38 loc) · 1 KB
/
get_millis.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
// Source: https://gist.github.com/adnbr/2439125#file-counting-millis-c
#include "get_millis.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
volatile unsigned long timer1_millis;
void millis_init()
{
// CTC mode, Clock/8
TCCR1B |= (1 << WGM12) | (1 << CS11);
// Load the high byte, then the low byte
// into the output compare
OCR1AH = (CTC_MATCH_OVERFLOW >> 8);
OCR1AL = CTC_MATCH_OVERFLOW;
sei();
// Enable the compare match interrupt
#if defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
TIMSK1 |= (1 << OCIE1A);
#elif defined (__AVR_ATmega64__)
TIMSK |= (1 << OCIE1A);
#elif defined (__AVR_ATtiny84A__)
TIMSK1 |= (1 << OCIE1A);
#endif
}
unsigned long millis()
{
unsigned long millis_return;
// ensure this cannnot be disrupted
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
millis_return = timer1_millis;
}
return millis_return;
}
ISR (TIMER1_COMPA_vect)
{
timer1_millis++;
}