-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.c
67 lines (60 loc) · 2.15 KB
/
Timer.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
// filename ******** Timer.C **************
// TCNT increments at a fixed rate
// Jonathan W. Valvano 10/11/09
// This example accompanies the books
// "Embedded Microcomputer Systems: Real Time Interfacing",
// Thompson, copyright (c) 2006,
// "Introduction to Embedded Systems: Interfacing to the Freescale 9S12",
// Cengage Publishing 2009, ISBN-10: 049541137X | ISBN-13: 9780495411376
// Copyright 2009 by Jonathan W. Valvano, valvano@mail.utexas.edu
// You may use, edit, run or distribute this file
// as long as the above copyright notices remain
#include <mc9s12dp512.h> /* derivative information */
//---------Timer_Init------------------
// activate TCNT timer at 1.5 MHz,667ns cycles
// inputs: none
// outputs: none
void Timer_Init(void){
TSCR1 = 0x80; // Enable TCNT, 24MHz boot mode, 8MHz in run mode
TSCR2 = 0x04; // divide by 8 TCNT prescale, TCNT at 667nsec
/* Bottom three bits of TSCR2 (PR2,PR1,PR0) set TCNT period
divide FastMode(24MHz) Slow Mode (8MHz)
000 1 42ns TOF 2.73ms 125ns TOF 8.192ms
001 2 84ns TOF 5.46ms 250ns TOF 16.384ms
010 4 167ns TOF 10.9ms 500ns TOF 32.768ms
011 8 333ns TOF 21.8ms 1us TOF 65.536ms
100 16 667ns TOF 43.7ms 2us TOF 131.072ms
101 32 1.33us TOF 87.4ms 4us TOF 262.144ns
110 64 2.67us TOF 174.8ms 8us TOF 524.288ms
111 128 5.33us TOF 349.5ms 16us TOF 1.048576s */
PACTL = 0; // timer prescale used for TCNT
}
//---------Timer_Wait------------------
// fixed time delay
// inputs: time to wait in 667ns cycles
// outputs: none
void Timer_Wait(unsigned short delay){
unsigned short startTime;
startTime = TCNT;
while((TCNT-startTime) <= delay){}
}
//---------Timer_Wait1ms------------------
// fixed time delay
// inputs: time to wait in ms
// outputs: none
// 1500 cycles equals 1ms
void Timer_Wait1ms(unsigned short delay){
for(;delay>0;delay--){
Timer_Wait(1500);
}
}
//---------Timer_Wait10ms------------------
// fixed time delay
// inputs: time to wait in 10ms
// outputs: none
// 15000 cycles equals 10ms
void Timer_Wait10ms(unsigned short delay){
for(;delay>0;delay--){
Timer_Wait(15000);
}
}