-
Notifications
You must be signed in to change notification settings - Fork 5
/
timer0.c
executable file
·51 lines (39 loc) · 661 Bytes
/
timer0.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
48
49
50
51
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "timer0.h"
enum {
STOP = 0,
CLK = 1,
CLK8 = 2,
CLK64 = 3,
CLK256 = 4,
CLK1024 = 5,
FALLING_EDGE = 6,
RISING_EDGE = 7
};
volatile uint16_t cntMilliSecs = 0;
void timer0_Init(void)
{
uint8_t sreg = SREG;
cli();
TCCR0B = TIMER_DIVIDER;
TCNT0 = TIMER_RELOAD_VAL;
TIMSK0 |= (1 << TOIE0);
cntMilliSecs = 0;
SREG = sreg;
sei();
}
uint16_t timer0_setDelay (uint16_t t)
{
return(cntMilliSecs + t - 1);
}
int8_t timer0_checkDelay(uint16_t t)
{
return(((t - cntMilliSecs) & 0x8000)>>8);
}
ISR(TIMER0_OVF_vect)
{
cntMilliSecs++;
TCNT0 = TIMER_RELOAD_VAL;
}