-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.c
51 lines (40 loc) · 993 Bytes
/
timing.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 <avr/interrupt.h>
#include <avr/io.h>
#include <stddef.h>
#include "timing.h"
volatile unsigned int delay;
volatile unsigned char freq_div;
void (*cb_func_10hz)(void *);
void *cb_state_10hz;
void init_timing() {
TCCR1A = 0x00;
TCCR1B = 0x01; // Timer 0 at 7.372812MHz
TIMSK = 1<<TOIE1; // Turn on interrupt for Timer 0
TCNT1 = 65535 - 7373;
cb_func_10hz = NULL;
cb_state_10hz = NULL;
freq_div = 0;
}
void tick_delay_ms(unsigned int ticks) {
delay = ticks;
while(delay>0) {
}
return;
}
void register_10hz_tick_callback(void (*cb_func)(void *), void *cb_state) {
cb_func_10hz = cb_func;
cb_state_10hz = cb_state;
}
void unregister_10hz_tick_callback() {
cb_func_10hz = NULL;
cb_state_10hz = NULL;
}
SIGNAL(TIMER1_OVF_vect) {
TCNT1 = 65535 - 7373; // 1ms interval
if(delay>0) delay--;
if(freq_div == 99) freq_div = 0;
else freq_div++;
if(freq_div==0) {
if(cb_func_10hz) cb_func_10hz(cb_state_10hz);
}
}