-
Notifications
You must be signed in to change notification settings - Fork 1
/
trtTimers.c
159 lines (135 loc) · 4.73 KB
/
trtTimers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//========================================================
// Define timers for use with TRT
// Bruce Land, Feb 2009
//
//========================================================
#define MAXNBRTIMERS 3
// Three values for TIMERTICK 0.1 mS or 1 mS or 10 mS
// only choose one value
#define TIMERTICK 10
//#define TIMERTICK 1
//#define TIMERTICK 0.1
#define SECONDS2TIMERTICK(t) ((t)*1000/TIMERTICK)
// bit definitions for the mode byte
#define ENABLEBIT 7 // =1 if enabled
#define RUNBIT 0 // =1 if running
#define MODEBIT 1 //periodic=0 oneshot=1
#define PERIODIC 0
#define ONESHOT 1
#define READ(U, N) ((U) >> (N) & 1u)
#define SET(U, N) ((void)((U) |= 1u << (N)))
#define CLR(U, N) ((void)((U) &= ~(1u << (N))))
#define FLIP(U, N) ((void)((U) ^= 1u << (N)))
// define a timer
struct {
uint16_t period; // the timeout value
uint16_t count; // the current value
uint16_t Nperiods; // number of timeouts which have occured
uint8_t status; // running/stopped, enabled/disabled, periodic/oneshot
uint8_t semaphore; // what to signal: zero means no signal
} timers[MAXNBRTIMERS] ;
//====================================================
//timer 0 overflow ISR
ISR (TIMER0_COMPA_vect) {
uint8_t i ;
// Handle array of soft timers
for (i=0; i<MAXNBRTIMERS; i++){
// for all enabled timers
if (READ(timers[i].status,ENABLEBIT)){
// if they are running
if (READ(timers[i].status,RUNBIT)) {
if (timers[i].count > 0) {
timers[i].count-- ;
}//if (timers[i].count > 0)
else { // timer count==0
timers[i].Nperiods++ ;
//reset for next cycle
timers[i].count = timers[i].period-1;
// signal if a semaphore is defined
if (timers[i].semaphore>0) trtSignal(timers[i].semaphore);
// stop the timer if mode==ONESHOT
if (READ(timers[i].status, MODEBIT)) CLR(timers[i].status,RUNBIT);
}// else timer count == 0
}//if (timers[i].status & (1<<RUNBIT))
}//if (READ(timers[i].status,ENABLEBIT))
} //for (i=0; i<MAXNBRTIMERS; i++)
} //ISR
//====================================================
void trtInitTimer(void) {
uint8_t i ;
// set up 0.1 mS tick
if (TIMERTICK==0.1){
//set up timer 0 for 1 mSec timebase
TIMSK0= (1<<OCIE0A); //turn on timer 0 cmp match ISR
OCR0A = 24; //set the compare 250 time ticks
//set prescalar to divide by 64
TCCR0B= 3; //0b00000011;
// turn on clear-on-match
TCCR0A= (1<<WGM01) ;
} //if (TIMERTICK==0.1)
// set up 1 mS tick
if (TIMERTICK==1){
//set up timer 0 for 1 mSec timebase
TIMSK0= (1<<OCIE0A); //turn on timer 0 cmp match ISR
OCR0A = 249; //set the compare 250 time ticks
//set prescalar to divide by 64
TCCR0B= 3; //0b00000011;
// turn on clear-on-match
TCCR0A= (1<<WGM01) ;
} //if (TIMERTICK==1)
// set up 10 mS tick
if (TIMERTICK==10){
//set up timer 0 for 10 mSec timebase
TIMSK0= (1<<OCIE0A); //turn on timer 0 cmp match ISR
OCR0A = 155; //set the compare 156 time ticks
//set prescalar to divide by 1024
TCCR0B= 5; //0b00000101;
// turn on clear-on-match
TCCR0A= (1<<WGM01) ;
} //if (TIMERTICK==10)
//set all timers to disabled
for (i=0; i<MAXNBRTIMERS; i++){
CLR(timers[i].status, ENABLEBIT);
} //for (i=0; i<MAXNBRTIMERS; i++)
} //void trtInitTimer
//====================================================
void trtSetTimer(uint8_t timer_number,
uint16_t period,
uint8_t mode,
uint8_t sem) {
// set period
timers[timer_number-1].period = period ;
// set count=period-1 (zero based)
timers[timer_number-1].count = period-1 ;
// set status to (STOP | mode)
CLR(timers[timer_number-1].status, RUNBIT) ;
if(mode==PERIODIC) CLR(timers[timer_number-1].status, MODEBIT) ;
else SET(timers[timer_number-1].status, MODEBIT) ;
// set Nperiod to zero
timers[timer_number-1].Nperiods = 0 ;
// set semaphore
timers[timer_number-1].semaphore = sem ;
// enable timer
SET(timers[timer_number-1].status, ENABLEBIT);
} //void trtSetTimer
//====================================================
void trtStartTimer(uint8_t timer_number){
SET(timers[timer_number-1].status, RUNBIT) ;
}
//====================================================
void trtStopTimer(uint8_t timer_number){
CLR(timers[timer_number-1].status, RUNBIT) ;
}
//====================================================
void trtDisableTimer(uint8_t timer_number){
CLR(timers[timer_number-1].status, ENABLEBIT) ;
}
//====================================================
uint8_t trtStatusTimer(uint8_t timer_number){
return timers[timer_number-1].status ;
}
//====================================================
uint16_t trtNumPeriods(uint8_t timer_number){
return timers[timer_number-1].Nperiods ;
}
//====================================================