-
Notifications
You must be signed in to change notification settings - Fork 2
/
timers.c
293 lines (269 loc) · 11.2 KB
/
timers.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
* File: timers.c
* Author: Will Flores [email protected]
* Usage:
* Description: This file contains the function implentations for the timer
* initializations for this board.
* Environment: Windows 7, x64 build
* Built in HEW with MC16 Series Compiler V.5.44 Release 00
* Notes: NONE
* Revisions: 0.0, Initial Release
* 1.0, Better documented file header with function headers
* Created on March 12, 2012
*******************************************************************************/
#include "QSKDefines.h"
#include "proto.h"
#include "extern.h"
#define NUM_OF_SECS 1e-3
/* Use these vars to designate how many times you want a certain wheel to be on */
volatile unsigned int r_count;
volatile unsigned int l_count;
/* Use these vars as timer flags, to denote timers being turned off and on */
volatile unsigned short timerA0_started;
volatile unsigned short timerB0_started;
volatile unsigned short timerA2_started;
volatile unsigned short timerA1_started;
// Designates how long timer_A1 has to be on
unsigned int A1_ticks;
/*******************************************************************************
* Purpose: This function calls all the timers functions for this board as five
* ms timers.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void TimerInit(void) {
TimerA0_Init(); // left wheel timer
TimerA1_Init(); // 5 millisecond delay timer
TimerA2_Init(); // an extra timer
TimerB0_Init(); // right wheel timer
return;
}
/*******************************************************************************
* Purpose: This function initializes TimerA0 as the left wheel timer. It also
* also initializes the right and left wheel counts.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void TimerA0_Init(void) {
ta0mr = CLEAR_REGISTER; // Clear Timer A0 Mode Register
ta0mr |= TIMER_MODE; // Timer mode
ta0mr |= SRC_F32; // Clock Source f32
// (12MHz / 32 * [time in millisecond] / 1000 ms per second )-1
ta0 = (unsigned int) (((f1_CLK_SPEED/32)*DESIRED_TIME_MS*NUM_OF_SECS) - 1);
// disable irqs before setting irq registers - macro defined in skp_bsp.h
DISABLE_IRQ
// Set the timer B0's IPL (interrupt priority level) to 3
ta0ic = NORMAL_PRIORITY_LEVEL;
ENABLE_IRQ // enable interrupts macro defined in skp_bsp.h
// to enable time set tb0s =1
// initialize the counting variables for the wheels
r_count = WHEELS_STOPPED;
l_count = WHEELS_STOPPED;
}
/*******************************************************************************
* Purpose: This function starts TimerA0.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void start_leftWheel(void) {
/* Start timer A0 */
ta0s = TIMER_START;
timerA0_started = TIMER_START;
return;
}
/*******************************************************************************
* Purpose: This function stops TimerA0.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void stop_timerA0(void) {
/* Stop timer A0 */
ta0s = TIMER_STOP;
timerA0_started = TIMER_STOP;
return;
}
/*******************************************************************************
* Purpose: This function initializes TimerA0 as a 5 ms delay timer.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void TimerA1_Init(void) {
ta1mr = CLEAR_REGISTER; // Clear Timer A1 Mode Register
ta1mr |= TIMER_MODE; // Timer mode
ta1mr |= SRC_F32; // Count Source f32
// (12MHz / 32 * [time in millisecond] / 1000 ms per second )-1
ta1 = (unsigned int) (((f1_CLK_SPEED/32)*DESIRED_TIME_MS*NUM_OF_SECS) - 1); // 5 ms timer
// disable irqs before setting irq registers - macro defined in skp_bsp.h
DISABLE_IRQ
ta1ic = NORMAL_PRIORITY_LEVEL;
ENABLE_IRQ // enable interrupts macro defined in skp_bsp.h
}
/*******************************************************************************
* Purpose: This function starts TimerA1 for a specified amount of ticks.
* In this configuration, the ticks occur in 5 ms intervals.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void timerDelay(unsigned int timeDelay) {
/* current configuration tA1 == 5ms */
A1_ticks = timeDelay;
ta1s = TIMER_START; // start the timer
timerA1_started = TIMER_START;
return;
}
/*******************************************************************************
* Purpose: This function initializes TimerA2 as a 5 ms delay timer. In this
* configuation, it's not used.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void TimerA2_Init(void) {
ta2mr = CLEAR_REGISTER; // Clear Timer A2 Mode Register
ta2mr |= TIMER_MODE; // Timer mode
// changed the count source :(
ta2mr |= SRC_F32; // Count Source f32
// (12MHz / 32 * [time in millisecond] / 1000 ms per second )-1
ta2 = (unsigned int) (((f1_CLK_SPEED/32)*DESIRED_TIME_MS*NUM_OF_SECS) - 1); // 5 ms timer
// disable irqs before setting irq registers - macro defined in skp_bsp.h
DISABLE_IRQ
ta2ic = NORMAL_PRIORITY_LEVEL;
ENABLE_IRQ // enable interrupts macro defined in skp_bsp.h
}
/*******************************************************************************
* Purpose: This function starts TimerA2.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void start_timerA2(void) {
/* Start timer A2 */
ta2s = TIMER_START;
timerA2_started = TIMER_START;
return;
}
/*******************************************************************************
* Purpose: This function stops TimerA2.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void stop_timerA2(void) {
/* Stop timer A2 */
ta2s = TIMER_STOP;
timerA2_started = TIMER_STOP;
return;
}
/*******************************************************************************
* Purpose: This function initializes TimerB0 as the right wheel timer. It also
* also initializes the right and left wheel counts.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void TimerB0_Init(void) {
tb0mr = CLEAR_REGISTER; // Clear Timer B0 Mode Register
tb0mr |= TIMER_MODE; // Timer mode
tb0mr |= SRC_F32; // Clock Source f32
// (12MHz / 32 * [time in millisecond] / 1000 ms per second )-1
tb0 = (unsigned int) (((f1_CLK_SPEED/32)*DESIRED_TIME_MS*NUM_OF_SECS) - 1);
// disable irqs before setting irq registers - macro defined in skp_bsp.h
DISABLE_IRQ
// Set the timer B0's IPL (interrupt priority level) to 3
tb0ic = NORMAL_PRIORITY_LEVEL;
ENABLE_IRQ // enable interrupts macro defined in skp_bsp.h
// to enable time set tb0s =1
// initialize the counting variables for the wheels
r_count = WHEELS_STOPPED;
l_count = WHEELS_STOPPED;
}
/*******************************************************************************
* Purpose: This function starts TimerB0.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void start_rightWheel(void) {
/* Start timer B0 */
tb0s = TIMER_START;
timerB0_started = TIMER_START;
return;
}
/*******************************************************************************
* Purpose: This function stops TimerB0.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void stop_timerB0(void) {
/* Stop timer B0 */
tb0s = TIMER_STOP;
timerB0_started = TIMER_STOP;
return;
}
/*******************************************************************************
* Purpose: This function starts both wheel timers.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void start_wheelTimers(void) {
start_leftWheel();
start_rightWheel();
return;
}
/*******************************************************************************
* Purpose: This function stops both wheel timers.
* Passed: No arguments passed.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void stop_wheelTimers(void) {
stop_timerA0();
stop_timerB0();
return;
}
/*******************************************************************************
* Purpose: This function sets the r_count variable.
* Passed: unsigned int delay - used to set the amount of 5ms intervals for the
* right motor to be on.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void set_rWheelCount(unsigned int delay) {
r_count = delay;
return;
}
/*******************************************************************************
* Purpose: This function sets the l_count variable.
* Passed: unsigned int delay - used to set the amount of 5ms intervals for the
* left motor to be on.
* Locals: No locals variables used.
* Returned: No values returned.
* Author: Will Flores [email protected]
*******************************************************************************/
void set_lWheelCount (unsigned int delay) {
l_count = delay;
return;
}