-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
72 lines (60 loc) · 1.5 KB
/
Copy pathtimer.h
File metadata and controls
72 lines (60 loc) · 1.5 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
#ifndef __TIMER_H__
#define __TIMER_H__
#include <stdint.h>
#include <avr/interrupt.h>
typedef struct _timer {
uint16_t interval; //# ticks (100µs) between firing
uint16_t last;
} timer_t;
extern volatile uint16_t timer_ticker;
static inline uint16_t read_ticker(void) {
uint8_t intr_state = SREG;
cli();
uint16_t current_tick = timer_ticker;
SREG = intr_state;
return current_tick;
}
/******
*
* Returns true if there have been at least timer->interval ticks since the
* last time tick() was called, false otherwise
*
* WARNING: Must be called at least once before timer wraps around (6.55 seconds)
*
*/
static inline uint8_t timer_fired(timer_t *timer) {
uint16_t new_time = read_ticker();
if((uint16_t)(new_time - timer->last) > timer->interval) {
timer->last += timer->interval;
return 1;
} else {
return 0;
}
}
/*****
*
* Initializes a timer_t object to start firing in the given interval
*/
static inline void init_timer(timer_t *timer, uint16_t interval) {
timer->last = read_ticker();
timer->interval = interval;
}
/******
*
* Returns the number of ticks since the last time the method was called.
* since is updated to the current tick, so that an immediate subsequent
* call will return 0
*/
static inline uint16_t ticks_elapsed(uint16_t *since) {
uint16_t cur = read_ticker(),
ret = cur - *since;
*since = cur;
return ret;
}
/********
*
* Initializes Counter3 and OCR3A to run a timer with a 100µs tick
*
*/
void setup_timer(void);
#endif