-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimers.c
executable file
·509 lines (493 loc) · 19.1 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*******************************************************************************
*
* TITLE: timers.c
*
* VERSION: 0.2 (Beta)
*
* DATE: 03-Jan-2008
*
* AUTHOR: R. Kevin Watson
*
* COMMENTS: This file contains template timer initialization & interrupt
* handling code for the IFI FRC robot controller.
*
* This version is compatible with Microchip C18 3.0+ only.
*
* This file best viewed with tabs set to four.
*
* You are free to use this source code for any non-commercial
* use. Please do not make copies of this source code, modified
* or un-modified, publicly available on the internet or elsewhere
* without permission. Thanks.
*
* Copyright ©2007-2008 R. Kevin Watson. All rights are reserved.
*
********************************************************************************
*
* Change log:
*
* DATE REV DESCRIPTION
* ----------- --- ----------------------------------------------------------
* 24-Dec-2007 0.1 RKW Original
* 03-Jan-2008 0.2 RKW - Renamed all ISRs for consistancy across all
* modules of the new robot controller code.
*
*******************************************************************************/
#include "ifi_frc.h"
#include "timers.h"
/*******************************************************************************
*
* FUNCTION: Initialize_Timer_0()
*
* PURPOSE: Initializes the timer 0 hardware.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_0_ISR
void Initialize_Timer_0(void)
{
TMR0L = 0x00; // least significant 8-bits of the timer 0 register (this is readable and writable)
TMR0H = 0x00; // most significant 8-bits of the timer 0 register (this is readable and writable)
//
T0CONbits.T0PS0 = 0; // T0PS2 TOPS1 T0PS0
T0CONbits.T0PS1 = 0; // 0 0 0 1:2 prescaler (clock=5MHz/each tick=200ns)
T0CONbits.T0PS2 = 0; // 0 0 1 1:4 prescaler (clock=2.5MHz/each tick=400ns)
// 0 1 0 1:8 prescaler (clock=1.25MHz/each tick=800ns)
// 0 1 1 1:16 prescaler (clock=625KHz/each tick=1.6us)
// 1 0 0 1:32 prescaler (clock=312.5KHz/each tick=3.2us)
// 1 0 1 1:64 prescaler (clock=156.25KHz/each tick=6.4us)
// 1 1 0 1:128 prescaler (clock=78.125KHz/each tick=12.8us)
// 1 1 1 1:256 prescaler (clock=39.0625 KHz/each tick=25.6us)
//
T0CONbits.PSA = 1; // 0: use the prescaler to derive the timer clock
// 1: don't use the prescaler (clock=10MHz/each tick=100ns)
//
T0CONbits.T0SE = 0; // 0: when using an external clock, timer increments on the rising-edge
// 1: when using an external clock, timer increments on the falling-edge
//
T0CONbits.T0CS = 0; // 0: use the internal clock (leave at 0)
// 1: use an external clock on RA4/T0CKI (don't use - not available on IFI controllers)
//
T0CONbits.T08BIT = 0; // 0: timer 0 is configured as a 16-bit timer/counter
// 1: timer 0 is configured as an 8-bit timer/counter
//
INTCON2bits.TMR0IP = 0; // 0: timer 0 overflow interrupt is low priority (leave at 0 for IFI controllers)
// 1: timer 0 overflow interrupt is high priority
//
INTCONbits.TMR0IF = 0; // 0: timer 0 overflow hasn't happened (set to 0 before enabling the interrupt)
// 1: timer 0 overflow has happened
//
INTCONbits.TMR0IE = 0; // 0: disable timer 0 interrupt on overflow (i.e., a transition from FFFF->0 or FF->0)
// 1: enable timer 0 interrupt on overflow (i.e., a transition from FFFF->0 or FF->0)
//
T0CONbits.TMR0ON = 0; // 0: timer 0 is disabled
// 1: timer 0 is enabled (running)
}
#endif
/*******************************************************************************
*
* FUNCTION: Timer_0_ISR()
*
* PURPOSE: If enabled, the timer 0 interrupt handler is called when
* the TMR0 register overflows and rolls over to zero.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_0_ISR
#pragma tmpdata low_isr_tmpdata
void Timer_0_ISR(void)
{
// this function will be called when a timer 0 interrupt occurs
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Timer_1()
*
* PURPOSE: Initializes the timer 1 hardware.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_1_ISR
void Initialize_Timer_1(void)
{
TMR1L = 0x00; // least significant 8-bits of the timer 1 register (this is readable and writable)
TMR1H = 0x00; // most significant 8-bits of the timer 1 register (this is readable and writable)
//
T1CONbits.T1CKPS0 = 0; // T1CSP1 T1CSP0
T1CONbits.T1CKPS1 = 0; // 0 0 1:1 prescaler (clock=10MHz/each tick=100ns)
// 0 1 1:2 prescaler (clock=5MHz/each tick=200ns)
// 1 0 1:4 prescaler (clock=2.5MHz/each tick=400ns)
// 1 1 1:8 prescaler (clock=1.25MHz/each tick=800ns)
//
T1CONbits.T1OSCEN = 0; // 0: timer 1 oscillator disabled (leave at 0 to allow the use of an external clock)
// 1: timer 1 oscillator enabled (can't be used because of hardware constraints)
//
T1CONbits.TMR1CS = 0; // 0: use the internal clock
// 1: use an external clock on RC0/T1OSO/T13CLK (rc_dig_in14 on robot controller)
//
T1CONbits.RD16 = 1; // 0: timer 1 register operations are done in two 8-bit accesses
// 1: timer 1 register operations are done in one 16-bit access
// In this mode, reading TMR1L will latch a copy of TMR1H into a buffer
// mapped to the TMR1H memory address. Conversely, a write to the buffer
// followed by a write to the TMR1L register will update the entire 16-bit
// timer at once. This solves the problem where the timer may overflow
// between two 8-bit accesses. Here's an example of how to do a 16-bit read:
//
// unsigned char Temp_Buf; // 8-bit temporary buffer
// unsigned int Timer_Snapshot; // 16-bit variable
//
// Temp_Buf = TMR1L; // TMR1L must be read before TMR1H
// Timer_Snapshot = TMR1H;
// Timer_Snapshot <<= 8; // move TMR1H data to the upper half of the variable
// Timer_Snapshot += Temp_Buf; // we now have all sixteen bits
//
IPR1bits.TMR1IP = 0; // 0: timer 1 overflow interrupt is low priority (leave at 0 on IFI controllers)
// 1: timer 1 overflow interrupt is high priority
//
PIR1bits.TMR1IF = 0; // 0: timer 1 overflow hasn't happened (set to 0 before enabling the interrupt)
// 1: timer 1 overflow has happened
//
PIE1bits.TMR1IE = 0; // 0: disable timer 1 interrupt on overflow (i.e., a transition from FFFF->0)
// 1: enable timer 1 interrupt on overflow (i.e., a transition from FFFF->0)
//
T1CONbits.TMR1ON = 0; // 0: timer 1 is disabled
// 1: timer 1 is enabled (running)
}
#endif
/*******************************************************************************
*
* FUNCTION: Timer_1_ISR()
*
* PURPOSE: If enabled, the timer 1 interrupt handler is called when
* the TMR1 register overflows and rolls over to zero.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_1_ISR
#pragma tmpdata low_isr_tmpdata
void Timer_1_ISR(void)
{
// this function will be called when a timer 1 interrupt occurs
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Timer_2()
*
* PURPOSE: Initializes the timer 2 hardware.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_2_ISR
void Initialize_Timer_2(void)
{
TMR2 = 0x00; // 8-bit timer 2 register (this is readable and writable)
//
PR2 = 249; // timer 2 period register - timer 2 increments to this
// value then resets to zero on the next clock and starts
// all over again
//
T2CONbits.T2OUTPS0 = 1; // T2OUTPS3 T2OUTPS2 T2OUTPS1 T2OUTPS0
T2CONbits.T2OUTPS1 = 0; // 0 0 0 0 1:1 postscaler
T2CONbits.T2OUTPS2 = 0; // 0 0 0 1 1:2 postscaler
T2CONbits.T2OUTPS3 = 1; // 0 0 1 0 1:3 postscaler
// 0 0 1 1 1:4 postscaler
// 0 1 0 0 1:5 postscaler
// 0 1 0 1 1:6 postscaler
// 0 1 1 0 1:7 postscaler
// 0 1 1 1 1:8 postscaler
// 1 0 0 0 1:9 postscaler
// 1 0 0 1 1:10 postscaler
// 1 0 1 0 1:11 postscaler
// 1 0 1 1 1:12 postscaler
// 1 1 0 0 1:13 postscaler
// 1 1 0 1 1:14 postscaler
// 1 1 1 0 1:15 postscaler
// 1 1 1 1 1:16 postscaler
//
T2CONbits.T2CKPS0 = 1; // T2CKPS1 T2CKPS0
T2CONbits.T2CKPS1 = 0; // 0 0 1:1 prescaler (clock = 10MHz/each tick=100ns)
// 0 1 1:4 prescaler (clock = 2.5MHz/each tick=400ns)
// 1 x 1:16 prescaler (clock = 625KHz/each tick=1.6us) (T2CKPS0 doesn't matter)
//
IPR1bits.TMR2IP = 0; // 0: timer 2 interrupt is low priority (leave at 0 for IFI controllers)
// 1: timer 2 interrupt is high priority
//
PIR1bits.TMR2IF = 0; // 0: TMR2 to PR2 match hasn't happened (set to 0 before enabling the interrupt)
// 1: TMR2 to PR2 match has happened
//
PIE1bits.TMR2IE = 1; // 0: disable timer 2 interrupt on PR2 match
// 1: enable timer 2 interrupt on PR2 match
// if the prescaler is enabled (i.e., greater than 1:1), this
// match will occur n times (where n is the postscaler value)
// before an interrupt will be generated
//
T2CONbits.TMR2ON = 0; // 0: timer 2 is disabled
// 1: timer 2 is enabled (running)
}
#endif
/*******************************************************************************
*
* FUNCTION: Timer_2_ISR()
*
* PURPOSE: If enabled, the timer 2 interrupt handler is called when the
* TMR2 register matches the value stored in the PR2 register.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_2_ISR
volatile unsigned int msClock = 0;
#pragma tmpdata low_isr_tmpdata
void Timer_2_ISR(void)
{
msClock++;
}
#pragma tmpdata
void Timer_2_Reset(void)
{
Timer_2_Stop();
msClock = 0;
Timer_2_Start();
}
void Timer_2_Start(void)
{
T2CONbits.TMR2ON = 1;
}
void Timer_2_Stop(void)
{
T2CONbits.TMR2ON = 0;
}
unsigned int Timer_2_Get_Time(void)
{
unsigned int tempTime;
Timer_2_Stop();
tempTime = msClock;
Timer_2_Start();
return tempTime;
}
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Timer_3()
*
* PURPOSE: Initializes the timer 3 hardware.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS: Timer 3 is used by the precision PWM software
* (pwm.c/.h). If you use timer 3 in your application,
* you'll need to remove pwm.c/.h from your project or
* modify it to use timer 1.
*
*******************************************************************************/
#ifdef ENABLE_TIMER_3_ISR
void Initialize_Timer_3(void)
{
TMR3L = 0x00; // least significant 8-bits of the timer 3 register (this is readable and writable)
TMR3H = 0x00; // most significant 8-bits of the timer 3 register (this is readable and writable)
//
T3CONbits.T3CKPS0 = 0; // T3CKPS1 T3CKPS0
T3CONbits.T3CKPS1 = 0; // 0 0 1:1 prescaler (clock=10MHz/each tick=100ns)
// 0 1 1:2 prescaler (clock=5MHz/each tick=200ns)
// 1 0 1:4 prescaler (clock=2.5MHz/each tick=400ns)
// 1 1 1:8 prescaler (clock=1.25MHz/each tick=800ns)
//
T3CONbits.TMR3CS = 0; // 0: use the internal clock
// 1: use an external clock on RC0/T1OSO/T13CLK (rc_dig_in14 on robot controller)
//
T3CONbits.T3SYNC = 1; // 0: do not synchronize the external clock (this can cause timing problems)
// 1: synchronize the external clock to the 18F8520/18F8722 internal clock, which is desirable
//
T3CONbits.RD16 = 1; // 0: timer 3 register operations are done in two 8-bit accesses
// 1: timer 3 register operations are done in one 16-bit access
// In this mode, reading TMR3L will latch a copy of TMR3H into a buffer
// mapped to the TMR3H memory address. Conversely, a write to the buffer
// followed by a write to the TMR3L register will update the entire 16-bit
// timer at once. This solves the problem where the timer may overflow
// between two 8-bit accesses. Here's an example of how to do a 16-bit read:
//
// unsigned char Temp_Buf; // 8-bit temporary buffer
// unsigned int Timer_Snapshot; // 16-bit variable
//
// Temp_Buf = TMR3L; // TMR3L must be read before TMR3H
// Timer_Snapshot = TMR3H;
// Timer_Snapshot <<= 8; // move TMR3H data to the upper half of the variable
// Timer_Snapshot += Temp_Buf; // we now have all sixteen bits
//
IPR2bits.TMR3IP = 0; // 0: timer 3 overflow interrupt is low priority (leave at 0 on IFI controllers)
// 1: timer 3 overflow interrupt is high priority
//
PIR2bits.TMR3IF = 0; // 0: timer 3 overflow hasn't happened (set to 0 before enabling the interrupt)
// 1: timer 3 overflow has happened
//
PIE2bits.TMR3IE = 0; // 0: disable timer 3 interrupt on overflow (i.e., a transition from FFFF->0)
// 1: enable timer 3 interrupt on overflow (i.e., a transition from FFFF->0)
//
T3CONbits.TMR3ON = 0; // 0: timer 3 is disabled
// 1: timer 3 is enabled (running)
}
#endif
/*******************************************************************************
*
* FUNCTION: Timer_3_ISR()
*
* PURPOSE: If enabled, the timer 3 interrupt handler is called when
* the TMR3 register overflows and rolls over to zero.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_TIMER_3_ISR
#pragma tmpdata low_isr_tmpdata
void Timer_3_ISR(void)
{
// this function will be called when a timer 3 interrupt occurs
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Timer_4()
*
* PURPOSE: Initializes the timer 4 hardware.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS: Timer 4 is used by the ADC software (adc.c/.h). If you're
* using the ADC software, try using timer 2 instead.
*
*******************************************************************************/
#ifdef ENABLE_TIMER_4_ISR
void Initialize_Timer_4(void)
{
TMR4 = 0x00; // 8-bit timer 4 register (this is readable and writable)
//
PR4 = 0xFF; // timer 4 period register - timer 4 increments to this
// value then resets to zero on the next clock and starts
// all over again
//
T4CONbits.T4OUTPS0 = 0; // T4OUTPS3 T4OUTPS2 T4OUTPS1 T4OUTPS0
T4CONbits.T4OUTPS1 = 0; // 0 0 0 0 1:1 postscaler
T4CONbits.T4OUTPS2 = 0; // 0 0 0 1 1:2 postscaler
T4CONbits.T4OUTPS3 = 0; // 0 0 1 0 1:3 postscaler
// 0 0 1 1 1:4 postscaler
// 0 1 0 0 1:5 postscaler
// 0 1 0 1 1:6 postscaler
// 0 1 1 0 1:7 postscaler
// 0 1 1 1 1:8 postscaler
// 1 0 0 0 1:9 postscaler
// 1 0 0 1 1:10 postscaler
// 1 0 1 0 1:11 postscaler
// 1 0 1 1 1:12 postscaler
// 1 1 0 0 1:13 postscaler
// 1 1 0 1 1:14 postscaler
// 1 1 1 0 1:15 postscaler
// 1 1 1 1 1:16 postscaler
//
T4CONbits.T4CKPS0 = 0; // T4CKPS1 T4CKPS0
T4CONbits.T4CKPS1 = 0; // 0 0 1:1 prescaler (clock = 10MHz/each tick=100ns)
// 0 1 1:4 prescaler (clock = 2.5MHz/each tick=400ns)
// 1 x 1:16 prescaler (clock = 625KHz/each tick=1.6us) (T2CKPS0 doesn't matter)
//
IPR3bits.TMR4IP = 0; // 0: timer 4 interrupt is low priority (leave at 0 for IFI controllers)
// 1: timer 4 interrupt is high priority
//
PIR3bits.TMR4IF = 0; // 0: TMR4 to PR4 match hasn't happened (set to 0 before enabling the interrupt)
// 1: TMR4 to PR4 match has happened
//
PIE3bits.TMR4IE = 0; // 0: disable timer 4 interrupt on PR4 match
// 1: enable timer 4 interrupt on PR4 match
// if the prescaler is enabled (i.e., greater than 1:1), this
// match will occur n times (where n is the postscaler value)
// before an interrupt will be generated
//
T4CONbits.TMR4ON = 0; // 0: timer 4 is disabled
// 1: timer 4 is enabled (running)
}
#endif
// COMMENTED OUT BECAUSE IT IS USED IN ADC AND IT HAS MULTIPLE DEFINITIONS
/*******************************************************************************
*
* FUNCTION: Timer_4_ISR()
*
* PURPOSE: If enabled, the timer 4 interrupt handler is called when the
* TMR4 register matches the value stored in the PR4 register.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************
#ifdef ENABLE_TIMER_4_ISR
#pragma tmpdata low_isr_tmpdata
void Timer_4_ISR(void)
{
// this function will be called when a timer 4 interrupt occurs
}
#pragma tmpdata
#endif
*/