Professional Documents
Culture Documents
13.4 Output Compare: Chapter 13 - M68HCS12 Timer 13-13
13.4 Output Compare: Chapter 13 - M68HCS12 Timer 13-13
13-13
n is 0 - 7 for each of the timer channels. Any of the channels can be input capture or output compare.
13-14
'TSCR2'
TCRE
'TSCR1'
Bus Clock
TEN
Clear
Counter
'TSCR2'
Channel 7
Output Compare
01 TCNT
Clock
10 MUX
'TCNT'
'TFLG2'
16-BIT Counter
TOF
Timer
Overflow
Interrupt
Request
'CCR'
11
I
CLK1 CLK2
'TIE'
'PACTL'
Output
Compare
Interrupt
Request
CnI
'TFLG1'
16-Bit Comparator
TCn Register
TO 7 More
Timer
Channels
CnF
Port
Pin
'CFORC'
PTn
FOCn
'TCTL1 or TCTL2'
'TIOS'
7 6 5 4 3 2 1 0
Set IOSn BIT TO 1 FOR
Output Compare
OMn
OLn
'OC7M:OC7D'
OC7Mn OC7Dn
Output
Bit
Control
13-15
Bit 7
IOS7
IOS6
IOS5
IOS4
IOS3
IOS2
IOS1
IOS0
Write:
Reset:
Read: Anytime. Write: Anytime.
IOS7:IOS0
Input Capture or Output Compare Channel Configuration
Read: Anytime. Write: Anytime.
0 = The corresponding channel acts as input capture or an I/O bit (default).
1 = The channel acts as an output compare.
When IOS[7:0] is zero, the Port T bit can act as an I/O pin whose direction is controlled by the state of
the data direction register DDRT.
Reset:
Reset:
Bit 15
14
13
12
11
10
TC15
TC14
TC13
TC12
TC11
TC10
TC9
TC8
0
Bit 7
0
6
0
5
0
4
0
3
0
2
0
1
0
0
TC7
TC6
TC5
TC4
TC3
TC2
TC1
TC0
0
0
0
0
0
0
0
0
Read: Anytime. Write: Anytime for output compare operations. Writes have no effect in pulse accumulator mode.
Bit 7
C7F
C6F
C5F
C4F
C3F
C2F
C1F
C0F
Write:
Reset:
0
0
0
0
0
0
0
Read: Anytime. Write: Used in the clearing process (writing a one clears the bit).
CnF
Timer Interrupt Flags
When the corresponding timer channel is configured for output compare, the flag is set when the TCNT
and TCn register contents are identical.
When the corresponding timer channel is configured for input capture, the flag is set when a selected
edge is detected at the timer input pin. See Section 13.5
The flag is reset by writing a one to the register. See Section 13.8
13-16
13-17
5A4E
staa TFLG1
;
and wait until it is set
spin:
4F4E 02FC
brclr TFLG1,C1F,spin
;
Timed out, so toggle the bit
8601
ldaa #BIT0
B802 40
eora PTT
7A02 40
staa PTT
;
Get the current time
DC52
ldd
TC1
;
Add the number of clocks for 1 msec delay
C31F 40
addd #MS_1
5C52
std
TC1
;
Now reset the output compare 1 flag
8602
ldaa #C1F
5A4E
staa TFLG1
;
and repeat
20E7
bra
spin
; FOREVER
;********************************
13-18
Example 13-7 shows an output compare time delay that can have at most an 8.192 ms
delay an 8 MHz bus clock is used. Figure 13-3 shows that the TSCR2 register has three
prescaler bits PR2:PR1:PR0. These can be changed to select a prescaler of 1, 2, 4, 8, 16,
32, 64 or 128. Example 13-9 shows a program to generate a 10 ms delay. Lines 24 and 25
set the prescaler bits to 010 to divide by four. Now, each clock pulse is at 0.5 s and a 10
ms delay is represented by 20,000 counts.
If you use this technique to generate longer time delays, remember that any changes
you make will affect all timing being done by the timer system in other parts of the
program.
13-19
Bit 7
C7I
C6I
C5I
C4I
C3I
C2I
C1I
C0I
Write:
Reset:
0
0
0
Read: Anytime. Write: Anytime.
CnI
Timer Interrupt Flags
0 = The corresponding bit in TFLG1 is disabled from generating interrupts (default).
1 = The corresponding bit in TFLG1 may generate an interrupt.
Delays longer than 8.192 ms can be generated by changing the prescaler as shown in
Example 13-9 or by waiting for more output comparisons to be made. Example 13-10
and Example 13-11 shows how to generate a one second delay using the output compare
flag to generate an interrupt. The delay is achieved by waiting for 250 complete four
millisecond delay times generated by the output compare. This is done by reading the
TNCT register in line 34 and then generating an interrupt every 4 ms after that. After 250
interrupts, the LED will toggle. A counter for this is initialized in line 32. After the C2F
flag is cleared and interrupts enabled and unmasked (lines 38 - 43), the processor waits
for the interrupt to occur (line 46). When it does, the counter is checked to see if it is
zero. After the counter reaches zero, it is reinitialized to NTIMES and the LED is
13-20
toggled. The interrupt service routine decrements the counter in line 64. With a bus clock
of 8 MHz, each time an interrupt occurs MS_4 (32,000 bus clock cycles) is added to the
TC2 register in lines 66 - 69. Finally the flag is cleared in lines 71 and 72.
Example 13-10 1 sec Delay Using Output Compare Interrupts
Metrowerks HC12-Assembler
(c) COPYRIGHT METROWERKS 1987-2003
Rel.Loc
---------1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22000000
23
24000003
25000006
26
27000009
28
2900000C
30
31
3200000F
000013
33
34000014
35000016
36
37
38000018
3900001A
40
4100001C
42
4300001F
44
45
46000021
47000022
48000025
13-21
13-22
TFLG1 = TFLG1_C2F_MASK;
/* Clear C2F flag */
TIE_C2I = 1;
/* Enable the interrupt */
EnableInterrupts;
/* DO */
for(;;) {
/* Wait until the counter is zero */
while (counter > 0) {
asm (wai);
/* Wait for interrupt */
}
/* When the counter is zero reinitialize it */
counter = NTIMES;
/* and toggle the LED */
if (PORTA_BIT0 == 0) PORTA_BIT0 = 1;
else PORTA_BIT0 = 0;
/* FOREVER */
}
}
/*******************************************************************
* The interrupt service routine decrements the counter
* sets up TC2 for the next interrupt and resets the flag.
*******************************************************************/
void interrupt 10 OC2_isr ( void ){
/*******************************************************************/
/* Decrement the counter */
--counter;
/* Set up TC2 */
TC2 += MS_4;
/* Clear the flag */
TFLG1 = TFLG1_C2F_MASK; /* Clear C2F flag */
}
Example 13-12
In Example 13-10 the programmer calculates the count for the next interrupt by adding 32,000 clock cycles to the
current value of the TC2 register (lines 68 - 71). Why didn't the programmer first read the TCNT register and then
add 32,000 to find the time for the next interrupt?
Solution:
Interrupts are required every 4 msec in this example. If the TCNT registger is used every time to calculate the time
for the next interrupt, the time interval will be longer than 4 msec because the TCNT register increments with every
clock cycle.
13-23
Priority
15
14
13
12
11
10
9
8
Vector
Address
$FFE0:FFE1
$FFE2:FFE3
$FFE4:FFE5
$FFE6:FFE7
$FFE8:FFE9
$FFEA:FFEB
$FFEC:FFED
$FFEE:FFEF
Interrupt
Source
Timer Channel 7
Timer Channel 6
Timer Channel 5
Timer Channel 4
Timer Channel 3
Timer Channel 2
Timer Channel 1
Timer Channel 0
Local
Enable Bit
C7I
C6I
C5I
C4I
C3I
C2I
C1I
C0I
See
Register
TIE
TIE
TIE
TIE
TIE
TIE
TIE
TIE
HPRIO
Value to
Promote
$E0
$E2
$E4
$E6
$E8
$EA
$EC
$EE
13-24
TCTL1
OM7
OL7
OM6
OL6
OM5
OL5
OM4
OL4
Reset:
0
Bit 7
0
6
0
5
0
4
0
3
0
2
0
1
0
0
TCTL2
OM3
OL3
OM2
OL2
OM1
OL1
OM0
OL0
Reset:
0
0
0
Read: Anytime. Write: Anytime.
OMn:OLn
Output Compare Bit Control
OMn = Output Mode.
OLn = Output Level.
These pairs of bits specify the output action to be taken as a result of a successful output compare. When either
OMn or OLn is one, the pin associated with OCn becomes an output tied to OCn regardless of the state of
the associated DDRT bit.
Table 13-4 Compare Result Output Action
OMn
0
0
1
1
OLn
0
1
0
1
Bit-n Action
Timer disconnected from output pin logic.
Toggle OCn output line.
Clear OCn output line to zero.
Set OCn output line to one.
4C40 04
DC44
5C54
8604
5A4E
4C4C 04
4D49 20
4C49 10
10EF
3E
20FD
DC54
C33E 80
5C54
8604
5A4E
0B
13-25
Example 13-14
Write a short section of code to cause Port T, bit -5 to clear the bit when an output comparison is made.
Solution:
Port T, bit-5 is connected to Output Compare 5. Therefore, use the code
; Set OM5, OL5 in TCTL1 to 1 0
bset TCTL1, %00001000
bclr TCTL1, %00000100
13-26
13-27
Bit 7
TOV7
TOV6
TOV5
TOV4
TOV3
TOV2
TOV1
TOV0
Write:
Reset:
0
0
0
Read: Anytime. Write: Anytime.
TOVn
Toggle on Overflow
0 = Toggle output compare pin on overflow disabled (default).
1 = Toggle output compare pin on overflow enabled.
When this bit is set and the associated output compare channel overflows, the output compare pin is
toggled. This is the same as setting OMn:OLn = 10.
Bit 7
OC7M7
OC7M6
OC7M5
OC7M4
OC7M3
OC7M2
OC7M1
OC7M0
Write:
Reset:
Read: Anytime. Write: Anytime.
OC7Mn
Setting any bit in OC7M enables the corresponding bit in OC7D to be output to the corresponding pin
in Port T. Setting OC7Mn sets the Port T bit to be an output regardless of the state of bits in DDRT.
13-28
Bit 7
OC7D7
OC7D6
OC7D5
OC7D4
OC7D3
OC7D2
OC7D1
OC7D0
Write:
Reset:
Read: Anytime. Write: Anytime.
OC7Dn
When a bit in OC7M is set, the data value in the corresponding bit in OC7D is output to the
corresponding pin in Port T when the output compare in channel 7 is made.
OC7
OC5
OC6
OC7
OC7
OC5
OC6
OC7
PT-7
PT-6
PT-5
PT-4
OC4
OC4
(a)
OC7
OC7
OC7
OC7
PT-7
PT-6
PT-5
PT-4
(b)
Figure 13-4 (a) Output Compare 7 Controlling Four Bits Each with (a) Shorter Comparison Times;
(b) Equal Comparison Times
13-29
Solution:
Metrowerks HC12-Assembler
(c) COPYRIGHT METROWERKS 1987-2003
Rel.Loc
---------1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24000000
25
26
27000003
28
29000006
30000008
31
3200000A
3300000C
34
3500000E
36
37
38
39
40
41000011
42
43
44
45000015
46000017
4700001A
4800001C
4900001E
50000021
51000023
52000025
53000028
5400002A
C33E 80
5C5E
86C0
5A4E
9643
88F0
5A43
20D4
13-30
addd
#DELAY1 ; 2.0 ms
std
TC7
;
Reset C7F
ldaa #C7F|C6F
staa TFLG1
;
Toggle the bits in OC7D
ldaa OC7D
eora #O_BITS
staa OC7D
; FOREVER
;
Return to spin
bra
spin
;********************************
13-31
*/
- 4 */
bit change */
*/
Example 13-18 Control Five Bits with One Output Compare in C (Equal Comparison Values)
Show an C program to output the waveforms shown in Figure 13-4(b) where the output high times are 2 ms.
Solution:
/*******************************************************************
* This example shows how to control four bits simultaneously
* using the output compare channel 7 with each channel
* switching at the same time.
*******************************************************************/
#include <mc9s12c32.h>
/* derivative information */
#define DELAY1 16000
/* Number clocks for 2 ms */
#define O_BITS 0xF0
/* Bits to be output */
#define D_BITS 0xA0
/* Initial data value to output */
/*******************************************************************/
void main(void) {
/*******************************************************************/
/* Initialize I/O */
TIOS = O_BITS;
/* Enable output compare bits 7 - 4 */
OC7M = O_BITS;
/* Initialize the mask register */
OC7D = D_BITS;
/* Initialize the data register */
TSCR1_TEN = 1;
/* Enable the timer */
/* DO */
for(;;) {
/* Wait until output compare 7 hits */
while (TFLG1_C7F == 0) {}
/* The data in OC7D is output to bits 7 - 4 */
/* Now set up each channel for the next bit change */
TC4 = TC7;
TC5 = TC7;
TC6 = TC7;
/* Reset the output compare flag */
TFLG1 = TFLG1_C7F_MASK;
/* Toggle the bits in the data register */
OC7D = OC7D ^ O_BITS; //O_BITS;
}
13-32
/* FOREVER */
}
13-33
Bit 7
FOC7
FOC6
FOC5
4
FOC4
FOC3
FOC2
FOC1
FOC0
Write:
Reset:
0
0
0
0
0
0
0
Read: Anytime (but will always return $00). Write: Anytime.
FOC7:FOC0
Force Output Compare Action for Channels 7 0.
Writing a one to any of these bits causes the action programmed by OMn:OLn bits to occur. The action
taken will be the same as if a successful comparison had just taken place except the interrupt flag is
13-34
not set.