Professional Documents
Culture Documents
CH 6 Timer
CH 6 Timer
Prescaler Channel 0
Bus clock
Input capture
IOC0
Output compare
16-bit Counter
Channel 1
Input capture
IOC1
Modulus counter Output compare
Interrupt 16-bit Modulus Counter
Channel 2
Input capture
Timer overflow IOC2
Output compare
interrupt
Timer channel 0 Channel 3
interrupt Input capture
IOC3
Output compare
Registers Channel 4
Input capture
Output compare IOC4
Channel 5
Input capture
IOC5
Output compare
Timer channel 7
interrupt Channel 6
PA overflow 16-bit Input capture
Output compare IOC6
interrupt Pulse accumulator A
PA input Channel 7
interrupt
PB overflow 16-bit Input capture IOC7
interrupt Pulse accumulator B Output compare
- Complete information on the MC9S12 timer subsystem can be found in the ECT 16B8C Block User Guide (Enhanced
Figure 1-1 Timer Block Diagram
Capture Timer).
- On reset, the clock to the timer subsystem is initially turned off to save power.
- To turn on the clock you need to write a 1 to Bit 7 of register TSCR1 (Timer System Control Register 1) as follows:
bset TSCR1, $80 ; Enabling counter in Assembly
TSCR1 = TSCR1 | 0x80; // Enabling counter in C)
- The clock starts at 0x0000, counts up until 0xFFFF. It rolls over from 0xFFFF to 0x0000 (overflow), and continues
counting forever (until the counter is turned off or reset the MC9S12).
- The counter is normally clocked with the bus frequency (8 MHz on the T-Board up to a maximum of 25 MHz).
16
- It takes t = 8.192 ms ( 8∗10
2
6 ) for the counter to count from 0x0000
ECT_16B8C, V01.06 to 0xFFFF and roll over to 0x0000.
- To determine the time an event happens, you can read the current value of the counter (by reading the 16-bit
Freescale Semiconductor 14
TCNT (Timer
Count Register).
- When the timer rolls over from 0xFFFF to 0x0000 it sets a flip-flop to indicate that an overflow has happened.
53
54 3.3.6 TSCR1 — Timer System Control Register 1 Chapter 6 MC9S12 Timer Subsystem
Register offset: $_06
BIT7 6 5 4 3 2 1 BIT0
R 0 0 0 0
TEN TSWAI TSFRZ TFFCA
W
RESET: 0 0 0 0 0 0 0 0
= Unimplemented or Reserved
- To wait for time t = 8.192 ms, just wait until the flip-flop is set, then clear the flip-flop, and wait until the next time the
flip-flop is set.
- The state of the flip-flop can be found by looking at bit 7 (the Timer Overflow Flag (TOF) bit)of the Timer Flag
Register 2 (TFLG2).
Chapter 6 MC9S12 Timer Subsystem 55
- The flip-flop can be cleared by writing a 1 to the TOF bit of TFLG2.
- Note that Bit 7 of TFLG2 for a read is different from Bit 7 of TFLG2 for a write.
= Unimplemented or Reserved
TFLG2 indicates when interrupt conditions have occurred. To clear a bit in the flag register, write the bit
to one.
Read anytime. Write used in clearing mechanism (set bits cause corresponding bits to be cleared).
Any access to TCNT will clear TFLG2 register if the TFFCA bit in TSCR register is set.
TOF — Timer Overflow Flag
Set when 16-bit free-running timer overflows from $FFFF to $0000. This bit is cleared automatically
by a write to the TFLG2 register with bit 7 set. (See also TCRE control bit explanation.)
ECT_16B8C, V01.06
Freescale Semiconductor 28
- Sometimes you may want to delay longer than the roll-over time, or time an event which takes longer. The The MC9S12
allows you to slow down the clock which drives the counter.
- The Timer Prescaler (PR2:0) bits of the Timer System Control Register 2 (TSCR2) allows you to change the fre-
quency of the clock driving the 16-bit counter.
- The timer clock frequency can be reduced by a factor of 2, 4, 8, 16, 32, 64 or 128, giving a larger overflow times.
- This is done by writing to the Prescaler bits (PR2:0) the required prescale factor as seen in the table bellow:
C7I–C0I — Input Capture/Output Compare “x” Interrupt Enable
56 3.3.11 TSCR2 — Timer System Control Register 2 Chapter 6 MC9S12 Timer Subsystem
Register offset: $_0D
BIT7 6 5 4 3 2 1 BIT0
R 0 0 0
TOI TCRE PR2 PR1 PR0
W
RESET: 0 0 0 0 0 0 0 0
= Unimplemented or Reserved
y selected prescale factor will not take effect until the next synchronized edge where all
Example:
counter stages equal zero.A C program used to show a counter on the
ECT_16B8C, LEDs (PORTB) which uses a Hardware Delay
V01.06
Freescale Semiconductor 26
# include < hidef .h >
# include " derivative . h "
LG1 — Main Timer Interrupt Flag 1
void delay ( void );
offset: $_0E
main () {
BIT7 6DDRB = 05xff ; 4 3 2 1 BIT0
PORTB = 0;
C7F C6F C5F C4F C3F C2F C1F C0F
TSCR1 = TSCR1 | 0 x80 ; /* Enable timer subsystem */
: 0 0TSCR2 = 00 x05 ; 0 /*
0 Set overflow
0 time
0 to ? 0ms */
TFLG2 = 0 x80 ; /* Make sure TOF bit clear */
Figure while
3-13 (1) {Timer Interrupt Flag 1 (TFLG1)
Main
PORTB = PORTB + 1;
delay ();
ates when interrupt}conditions have occurred. To clear a bit in the flag register, write a one
}
- Problem: Cannot do anything while waiting, as the C program must keep checking the TOF bit until the time has passed.
This approach is called Polling, which wastes processor time.
- Solution: By using Interrupt, the processor can execute other code, and the hardware timer will signal (interrupt) the
processor when the timer overflow occurs.