Professional Documents
Culture Documents
Han-Way Huang
September 2009
Timer Module
Input Capture
Output Compare
Pulse Accumulator
PWM
1
The HCS12/MC9S12 Microcontroller
The following applications are difficult to implement without a dedicated timer function:
The HCS12 has a standard timer module (TIM) that consists of:
The HCS12 devices in the automotive family have implemented an Enhanced Capture
Timer module (ECT). The ECT module contains:
2
The HCS12/MC9S12 Microcontroller
3
The HCS12/MC9S12 Microcontroller
4
The HCS12/MC9S12 Microcontroller
//-------------------------------------------------------
void interrupt 16 handler (void){
TCNT = 41536;// initialize TCNT to count 24000 times before flipover and interrupt generation.
}
//------------------------------------------------
main(){
TCNT = 41536;// initialize TCNT to count 24000 times before flipover and interrupt generation.
TSCR2 = 0x80;// enable TCNT overflow interrupt
TSCR1= 0x90; //enable TCNT with fast flag clear.
asm {cli;} // enable interrupts
}
Copyright © 2010 Delmar Cengage Learning H. Huang Transparency No.8-10
10
5
The HCS12/MC9S12 Microcontroller
or
11
The following instruction will enable the output compare channels 7...4 and input capture
channel 3…0:
movb #$F0,TIOS
Each port pin can be used as a general I/O pin when timer function is not selected.
Pin 7 can be used as input capture 7, output compare 7 action, and pulse accumulator input.
When a timer port pin is used as a general I/O pin, its direction is configured by the DDRT
register.
12
6
The HCS12/MC9S12 Microcontroller
13
14
7
The HCS12/MC9S12 Microcontroller
Method 1. Use the BCLR instruction with a 0 at the bit position (s) corresponding to the
flag (s) to be cleared. For example,
Method 2. Use the movb instruction with a 1 at the bit position (s) corresponding to the
flag (s) to be cleared. For example,
movb #$01,TFLG1
When fast timer flag clear function is enabled, see Figure 8.1.
15
one period
one period
16
8
The HCS12/MC9S12 Microcontroller
17
18
9
The HCS12/MC9S12 Microcontroller
Example 8.2 Period measurement. Use the IC0 to measure the period of an unknown
signal. The period is known to be shorter than 128 ms. Assume that the E clock frequency
is 24 MHz. Use the number of clock cycles as the unit of the period.
Solution:
Since the input-capture register is 16-bit, the longest period of the signal that can be
measured with the prescaler to TCNT set to 1 is:
Set the prescale factor to 1 and keep track of the number of times that the timer counter
overflows.
Set the prescale factor to 64 and do not keep track of the number of times that the timer
counter overflows.
We will set the prescale factor to TCNT to 64. The logic flow for measuring the signal
period is shown in Figure 8.16.
19
20
10
The HCS12/MC9S12 Microcontroller
#include "c:\miniide\hcs12.inc"
org $1000
edge1 ds.b 2 ; memory to hold the first edge
period ds.b 2 ; memory to store the period
org $1500
movb #$90,TSCR1 ; enable timer counter and enable fast timer flags clear
bclr TIOS,IOS0 ; enable input-capture 0
movb #$06,TSCR2 ; disable TCNT overflow interrupt, set prescaler to 64
movb #$01,TCTL4 ; capture the rising edge of PT0 signal
movb #C0F,TFLG1 ; clear the C0F flag
brclr TFLG1,C0F,* ; wait for the arrival of the first rising edge
ldd TC0 ; save the first edge and clear the C0F flag
std edge1
brclr TFLG1,C0F,* ; wait for the arrival of the second edge
ldd TC0
subd edge1 ; compute the period
std period
swi
end
21
#include "c:\cwHCS12\include\hcs12.h"
void main(void)
{
unsigned int edge1, period;
TSCR1 = 0x90; // enable timer counter, enable fast flag clear
TIOS &= ~IOS0; // enable input-capture 0
TSCR2 = 0x06; // disable TCNT overflow interrupt, set prescaler to 64
TCTL4 = 0x01; // capture the rising edge of the PT0 pin
TFLG1 = C0F; // clear the C0F flag
while (!(TFLG1 & C0F)); // wait for the arrival of the first rising edge
edge1 = TC0; // save the first captured edge and clear C0F flag
while (!(TFLG1 & C0F)); // wait for the arrival of the second rising edge
period = TC0 - edge1;
while(1);
}
22
11
The HCS12/MC9S12 Microcontroller
Example 8.3 Write a program to measure the pulse width of a signal connected to the
PT0 pin. The E clock frequency is 24 MHz.
Solution:
1. Set the prescale factor to TCNT to 32. Use clock cycle as the unit of measurement.
2. The pulse width may be longer than 216 clock cycles, we need to keep track of the number
of times that the TCNT timer overflows. Let
ovcnt = TCNT counter overflow count
diff = the difference of two consecutive edges
edge1 = the captured time of the first edge
edge2 = the captured time of the second edge
Case 1
edge2 edge1
Case 2
edge2 < edge 1
23
Figure 8.17 Logic flow for measuring pulse width of slow signals
upt
interr
from
pt
rru
Return
te
in
V
TO
Enable main timer overflow interrupt.
overflow - 1
Is second edge smaller?
Save the first captured edge.
C0F = 1?
Disable all interrupts.
yes
yes
yes
Stop
Start
overflow
overflow
no
no
no
24
12
The HCS12/MC9S12 Microcontroller
#include "c:\miniide\hcs12.inc"
org $1000
edge1 ds.b 2
overflow ds.b 2
PW ds.b 2
org $1500
movw #tov_isr,UserTimerOvf ; set up TCNT overflow interrupt vector
lds #$1500 ; set up stack pointer
movw #0,overflow
movb #$90,TSCR1 ; enable TCNT and fast timer flag clear
movb #$05,TSCR2 ; disable TCNT interrupt, set prescaler to 32
bclr TIOS,IOS0 ; select IC0
movb #$01,TCTL4 ; capture rising edge
movb #C0F,TFLG1 ; clear C0F flag
brclr TFLG1,C0F,* ; wait for the first rising edge
movw TC0,edge1 ; save the first edge & clear the C0F flag
movb #TOF,TFLG2 ; clear TOF flag
bset TSCR2,$80 ; enable TCNT overflow interrupt
cli ; "
movb #$02,TCTL4 ; capture the falling edge on PT0 pin
brclr TFLG1,C0F,* ; wait for the arrival of the falling edge
ldd TC0
subd edge1
25
std PW
bcc next ; is the second edge smaller?
ldx overflow ; second edge is smaller, so decrement
dex ; overflow count by 1
stx overflow ; "
next swi
26
13
The HCS12/MC9S12 Microcontroller
27
28
14
The HCS12/MC9S12 Microcontroller
29
30
15
The HCS12/MC9S12 Microcontroller
A successful compare will set the corresponding flag bit in the TFLG1 register.
An interrupt may be optionally requested if the associated interrupt enable bit in the TIE
register is set.
Example 8.4 Generate an active high 1 KHz digital waveform with 30 percent duty cycle
from the PT0 pin. Use the polling method to check the success of the output compare
operation. The frequency of the E clock is 24 MHz.
Solution:
An active high 1 KHz waveform with 30 percent duty cycle is shown in Figure 8.19. The
logic flow of this problem is illustrated in Figure 8.20.
Setting the prescaler to the TCNT to 8, then the period of the clock signal to the TCNT
will be 1/3 us (0.3333 us). Then need 3000 clocks to gain a 1ms period (1 KHz freq.) So
the numbers of clock cycles that the signal is high and low are 900 (i.e. 0.3*3000) and 2100
(i.e. 0.7*3000), respectively.
31
We should use interrupt-driven method to generate the waveform so that the CPU can
Still perform other operations.
32
16
The HCS12/MC9S12 Microcontroller
#include "c:\miniide\hcs12.inc"
HiCnt equ 900 ; delay count for high interval of the waveform
LoCnt equ 2100 ; delay count for low interval of the waveform
org $1000
HiorLo ds.b 1 ; flag to select HiCnt (1) or LoCnt (0)
org $1500
lds #$1500 ; establish stack pointer
movw #OC5ISR,UserTimerCh5 ; set up OC0 interrupt vector
movb #$90,TSCR1 ; enable TCNT and fast timer flag clear
movb #$03,TSCR2 ; set TCNT clock prescaler to 8
bset TIOS,BIT5 ; enable OC5
movb #$0C,TCTL1 ; configure the OC5 pin action to pull high
ldd TCNT ; start OC5 with delay count equal to 10 and
addd #10 ; pin action to pull high
std TC5
brclr TFLG1,C5F,* ; wait until C5F is set to 1 and PT5 pin pulled high
ldd TC5 ; start another OC5 operation with
addd #HiCnt ; delay count set to HiCnt
std TC5 ; "
movb #$04,TCTL1 ; change pin action to toggle
clr HiorLo ; LoCnt will be the delay count next time
bra $ ; prepare to perform other operations
33
; --------------------------------------------------------------------------------------------------------------
; OC5 interrupt service routine starts a new OC5 operation with delay count equal to
; HiCnt or LoCnt depending on whether HIorLO flag is 1 or 0. After starting a new
; OC5 operation, toggle HIorLO flag.
; --------------------------------------------------------------------------------------------------------------
OC5ISR tst HIorLO ; which delay count should be added?
beq addLow ; if 0 then select LoCnt
ldd TC5 ; select HiCnt as the delay count for
addd #HiCnt ; the new OC5 operation
std TC5 ;"
clr HiorLo ; toggle HiorLo flag
rti
addLow ldd TC5 ; select LoCnt as the delay count for
addd #LoCnt ; the new OC5 operation
std TC5 ;"
movb #1,HiorLo ; toggle HiorLo flag
rti
end
34
17
The HCS12/MC9S12 Microcontroller
#include "c:\cwHCS12\include\hcs12.h"
#define HiCnt 900
#define LoCnt 2100
char HiorLo;
void main (void)
{
TSCR1 = 0x90; // enable TCNT and fast timer flag clear
TSCR2 = 0x03; // disable TCNT interrupt, set prescaler to 8
TIOS |= OC5; // enable OC5 function
TCTL1 = 0x0C; // set OC5 action to be pull high
TFLG1 = 0xFF; // clear all CxF flags
TC5 = TCNT + 10;
while(!(TFLG1 & C5F)); // wait until C5F is set
TCTL1 = 0x04; // set OC5 pin action to toggle
TC5 += HiCnt; // start an new OC5 operation
HiorLo = 0; // add LoCnt for the next OC5 operation
TIE = 0x20; // enable OC5 interrupt locally
asm("cli"); // enable interrupt globally
while(1);
}
35
The OC5 interrupt vector is set up using the following function (in vectors.c):
extern void near oc5ISR(void);
#pragma CODE_SEG __NEAR_SEG NON_BANKED
#pragma CODE_SEG DEFAULT // Change code section to DEFAULT.
36
18
The HCS12/MC9S12 Microcontroller
Example 8.5 Write a function to generate a time delay that is a multiple of 1 ms.
Assume that the E clock frequency is 24 MHz. The number of milliseconds is passed in
Y. Also write an instruction sequence to test this function.
Solution: One method to create 1 ms delay is as follows:
Set the prescaler to TCNT to 8
Perform the number of output-compare operations (given in Y) with each operation
creating a 1-ms time delay.
The number to be added to the copy of TCNT is 3000. (3000 8 24000000 = 1 ms)
delayby1ms pshd
movb #$90,TSCR1 ; enable TCNT & fast flags clear
movb #$06 #$03,TSCR2 ; configure prescaler to 64 8
bset TIOS,OC0 ; enable OC0
ldd TCNT
again0 addd #3000 ; start an output-compare operation
std TC0 ; with 1 ms time delay
wait_lp0 brclr TFLG1,OC0,wait_lp0
ldd TC0
dbne y,again0
puld
rts
37
void delayby1ms(int k)
{
int ix;
TSCR1 = 0x90; /* enable TCNT and fast timer flag clear */
TSCR2 = 0x03; /* disable timer interrupt, set prescaler to 64 */
TIOS |= OC0; /* enable OC0 */
TC0 = TCNT + 3000;
for(ix = 0; ix < k; ix++) {
while(!(TFLG1 & C0F));
TC0 += 3000;
}
TIOS &= ~OC0; /* disable OC0 */
}
38
19
The HCS12/MC9S12 Microcontroller
39
#include "c:\miniide\hcs12.inc"
DelayHi equ 18000 ; pulse high interval duration
DelayLo equ 12000 ; pulse low interval duration
NN equ 10 ; number of pulses to be created
org $1000
pcount ds.b 1 ; number of OC0 operations remaining to be performed
HiorLo ds.b 1 ; flag to choose DelayHi(1) or DelayLo (0)
org $1500
lds #$1500
movw #oc0ISR,UserTimerCh0 ; set up OC0 interrupt vector
movb #$90,TSCR1
movb #$04,TSCR2
bset TIOS,OC0 ; enable OC0
movb #C0F,TFLG1 ; clear C0F flag
movb #$03,TCTL2 ; set OC0 pin action to pull high
ldd TCNT ; use OC0 operation to pull TC0 pin high
addd #12 ; quickly
std TC0 ; "
brclr TFLG1,C0F,* ; wait until C0F flag is set
movb #$01,TCTL2 ; set OC0 pin action to toggle
ldd TC0 ; start next OC0 operation with
addd #DelayHi ; delay set to DelayHi
40
20
The HCS12/MC9S12 Microcontroller
41
#include "c:\cwHCS12\include\hcs12.h"
#include "c:\cwHCS12\include\SetClk.h"
#define DelayHi 18000 // high time of the pulses to be created
#define DelayLo 12000 // low time of the pulses to be created
#define NN 10 // number of pulses to be created
int pcnt; // pulse count
char HiorLo; // flag to choose
void main(void)
{
SetClk8(); // use PLL to generate bus and system clocks
TSCR1 = 0x90; // enable TCNT and faster timer flag clear
TSCR2 = 0x04; // set TCNT clock input prescaler to 16
TFLG1 = C0F; // clear C0F flag
TIOS |= OC0; // enable OC0
TCTL2 = 0x03; // set OC0 pin action to be pull high
TC0 = TCNT + 16; // pull PT0 pin high quickly
while(!(TFLG1 & C0F)); // "
pcnt = 2 * NN - 1;// prepare to create NN pulses (toggle 2 * NN - 1 times)
TCTL2 = 0x01; // set OC0 pin action to be toggle
TC0 += DelayHi; // start the second OC0 operation
HiorLo = 0; // next time use DelayLo as delay count of OC0 operation
TIE |= C0I; // enable TC0 interrupt
asm("cli"); // "
while (1); // do nothing or do something else
}
42
21
The HCS12/MC9S12 Microcontroller
43
HCS12DP256
3.3 F
PT5
Buzzer
44
22
The HCS12/MC9S12 Microcontroller
Step 1
Enable an output compare channel to drive the buzzer (or speaker).
Step 2
Start an output compare operation with a delay count equal to half the period of the siren.
and enable the OC interrupt.
Step 3
Waiting for the duration of the siren tone (say half a second). During the waiting period,
interrupts will be requested many times by the output compare function. The interrupt
service routine simply restarts the output compare operation.
Step 4
At the end of the siren tone duration, choose a different delay count for the output compare
operation so that the siren sound may have a different frequency.
Step 5
Wait for the same duration as in Step 3. During this period, many interrupts will be requested
by the output compare operation.
Step 6
Go to Step 2.
45
Example 8.7 Write a program to generate a two-tone siren that oscillates between 300 Hz
and 1200 Hz.
Solution:
Set the prescaler to TCNT to 1:8.
The delay count for the low frequency tone is (24000000 8) 300 2 = 5000.
The delay count for the high frequency tone is (24000000 8) 1200 2 = 1250.
#include "c:\miniide\hcs12.inc"
hi_freq equ 1250 ; delay count for 1200 Hz (with 1:8 prescaler)
lo_freq equ 5000 ; delay count for 300 Hz (with 1:8 prescaler)
toggle equ $04 ; value to toggle the TC5 pin
org $1000
delay ds.w 1 ; store the delay for output-compare operation
org $2000
lds #$2000
movw #oc5_isr,UserTimerCh5 ; initialize the interrupt vector entry
movb #$90,TSCR1 ; enable TCNT, fast timer flag clear
movb #$03,TSCR2 ; set main timer prescaler to 8
bset TIOS,OC5 ; enable OC5
movb #toggle,TCTL1; select toggle for OC5 pin action
ldd #hi_freq
std delay ; use high-frequency delay count first
ldd TCNT ; start timer
46
23
The HCS12/MC9S12 Microcontroller
47
48
24
The HCS12/MC9S12 Microcontroller
49
#include "c:\miniide\hcs12.inc"
G3 equ 7653 ; delay count to generate G3 note (with 1:8 prescaler)
B3 equ 6074 ; delay count to generate B3 note (with 1:8 prescaler)
C4 equ 5733 ; delay count to generate C4 note (with 1:8 prescaler)
C4S equ 5412 ; delay count to generate C4S (sharp) note
D4 equ 5108 ; delay count to generate D4 note (with 1:8 prescaler)
E4 equ 4551 ; delay count to generate E4 note (with 1:8 prescaler)
F4 equ 4295 ; delay count to generate F4 note (with 1:8 prescaler)
F4S equ 4054 ; delay count to generate F4S note (with 1:8 prescaler)
G4 equ 3827 ; delay count to generate G4 note (with 1:8 prescaler)
A4 equ 3409 ; delay count to generate A4 note (with 1:8 prescaler)
B4F equ 3218 ; delay count to generate B4F note (with 1:8 prescaler)
B4 equ 3037 ; delay count to generate B4 note (with 1:8 prescaler)
C5 equ 2867 ; delay count to generate C5 note (with 1:8 prescaler)
D5 equ 2554 ; delay count to generate D5 note (with 1:8 prescaler)
E5 equ 2275 ; delay count to generate E5 note (with 1:8 prescaler)
F5 equ 2148 ; delay count to generate F5 note (with 1:8 prescaler)
ZZ equ 20 ; delay count to generate an inaudible sound
notes equ 101
toggle equ $04 ; value to toggle the TC5 pin
50
25
The HCS12/MC9S12 Microcontroller
org $1000
delay ds.w 1 ; store the delay for output-compare operation
rep_cnt ds.b 1 ; repeat the song this many times
ip ds.b 1 ; remaining notes to be played
org $1500
lds #$1500
; establish the SRAM vector address for OC5
movw #oc5_isr,UserTimerCh5
movb #$90,TSCR1 ; enable TCNT, fast timer flag clear
movb #$03,TSCR2 ; set main timer prescaler to 8
bset TIOS,OC5 ; enable OC5
movb #toggle,tctl1 ; select toggle for OC5 pin action
ldx #score ; use as a pointer to score table
ldy #duration ; points to duration table
movb #1,rep_cnt ; play the song twice
movb #notes,ip
movw 2,x+,delay ; start with zeroth note
ldd TCNT ; play the first note
addd delay ;"
std TC5 ;"
bset TIE,C5I ; enable OC5 interrupt
cli ; "
51
52
26
The HCS12/MC9S12 Microcontroller
53
; ************************************************************************
; This table determines the frequency of each note.
; ************************************************************************
score dc.w D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S
dc.w D4,ZZ,D4,ZZ,D4,B4,A4,G4,F4S,E4,F4S,G4,ZZ,G4,D4,B3,G3
dc.w D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S,D4,ZZ,D4,ZZ,D4
dc.w B4,A4,G4,F4S,E4,F4S,G4,ZZ,G4,D4,B3,G3,B4,ZZ,B4
dc.w B4,C5,D5,ZZ,D5,C5,B4,A4,B4,C5,ZZ,C5,ZZ,C5,B4,A4,G4
dc.w F4S,E4,F4S,G4,B3,C4S,D4,ZZ,D4,G4,ZZ,G4,ZZ,G4,F4S
dc.w E4,ZZ,E4,ZZ,E4,A4,C5,B4,A4,G4,ZZ,G4,F4S,D4,ZZ,D4
dc.w G4,A4,B4,C5,D5,G4,A4,B4,C5,A4,G4
; ************************************************************************
; Each of the following entries multiplied by 10 ms gives the duration of a note.
; ************************************************************************
duration dc.w 30,10,40,40,40,80,30,10,40,40,40
dc.w 80, 3,20,3,20,60,20,40,80,20,20,40,3,40,40,40,40
dc.w 30,10,40,40,40,80,30,10,40,40,40,80,3,20,3,20
dc.w 60,20,40,80,20,20,40,3,40,40,40,40,20,3,20
dc.w 40,40,40,3,80,20,20,40,40,40,3,80,3,40,60,20,40
dc.w 80,20,20,40,40,40,80,3,40,40,3,40,3,20,20
dc.w 40, 3,40,3,40,40,20,20,20,20,3,40,40,20,3,20
dc.w 60,20,20,20,80,20,20,60,20,40,80
end
54
27
The HCS12/MC9S12 Microcontroller
#include “c:\cwHCS12\include\hcs12.h”
#include “c:\cwHCS12\include\SetClk.h”
#define G3 7653 // delay count to be added to TC5
#define B3 6074 // to generate the given note
#define C4 5733
#define C4S 5412
#define D4 5108
#define E4 4551
#define F4 4295
#define F4S 4054
#define G4 3827
#define A4 3409
#define B4F 3218
#define B4 3037
#define C5 2867
#define D5 2554
#define E5 2275
#define F5 2148
#define ZZ 20 // delay count to create inaudible sound
#define toggle 0x04
int delay;
55
56
28
The HCS12/MC9S12 Microcontroller
main (void)
{
int j;
SetClk8(); // set E clock to 24 MHz using PLL
TSCR1 = 0x90; // enable TCNT, fast timer flag clear
TSCR2 = 0x03; // set TCNT prescaler to 8
TFLG1 = 0xFF; // clear all TxF flags
TIE |= C5I; // enable TC5 interrupt
TIOS |= OC5; // enable OC5 function
TCTL1 = toggle; // select toggle as OC5 pin action
j = 0;
delay = score[0];
TC5 = TCNT + delay; // play the first note
asm(" cli "); // enable TC5 interrupt
while (score[j]) { // play the song once
delay = score[j]; // play the jth note
delayby10ms(dur[j]);
j++;
}
TIOS &= ~OC5; /* stop playing the song */
while (1);// infinite loop
}
57
58
29
The HCS12/MC9S12 Microcontroller
59
Example 8.9 What value should be written into OC7M and OC7D if one wants pins
PT2, PT3, and PT4 to assume the values of 1, 0, and 1, respectively when OC7 compare
succeeds?
Solution:
Bits 4, 3, and 2 of OC7M must be set to 1, and bits 4, 3, 2, of OC7D should be set to 1, 0,
and 1, respectively.
The following instruction sequence will achieve the desired effect:
movb #$1C,OC7M
movb #$14,OC7D
60
30
The HCS12/MC9S12 Microcontroller
Forced Output-Compare
There are applications in which the user wants an output compare in action to occur
immediately instead of waiting for a match between the TCNT and the proper output
compare register.
This situation arises in the spark plug timing control and some automotive engine
control applications.
To force an output compare operation, write ones to the corresponding bit in the CFORC
register.
At the next timer count after the write to the CFORC register, the forced channels will
trigger their programmed pin actions to occur.
7 6 5 4 3 2 1 0
FOC7 FOC6 FOC5 FOC4 FOC3 FOC2 FOC1 FOC0
reset: 0 0 0 0 0 0 0 0
61
Example 8.12 Suppose that the contents of the TCTL1 and TCTL2 registers are $D6 and
$6E, respectively. The contents of the TFLG1 are $00. What would occur on pins PT7 to
PT0 on the next clock cycle if the value $7F is written into the CFORC register?
Solution:
The TCTL1 and TCTL2 configure the output-compare actions as shown in Table8.2.
The TFLG1 register indicates that none of the started output-compare operations
have succeeded yet.
The actions indicated in Table 8.2 will be forced to occur immediately.
62
31
The HCS12/MC9S12 Microcontroller
Pulse Accumulator
The HCS12 standard timer system has a 16-bit pulse accumulator PACA.
The HCS12 ECT system has four 8-bit pulse accumulators (PAC3…PAC0).
Two adjacent 8-bit pulse accumulators can be concatenated into a 16-bit pulse
accumulator.
PAC3 and PAC2 can be concatenated into the 16-bit PACA whereas PAC1 and PAC0
can be concatenated into the 16-bit PACB.
There are four possible pulse accumulator configurations:
Four 8-bit pulse accumulators PAC3…PAC0 are sharing the signal pins PT3…PT0.
When concatenated into 16-bit pulse accumulators, PACA shares the PT7 pin whereas
PACB shares the use of PT0 pin.
63
64
32
The HCS12/MC9S12 Microcontroller
65
Event counting mode. The 16-bit PACA can operate in this mode and count the number
of events arrived at the PT7 pin. The 16-bit PACB and all four 8-bit pulse accumulators
can operate only in this mode.
Gated accumulation mode. The 16-bit PACA can also operate in this mode. As long
as the PT7 signal is active (can be high or low), the PACA counter is clocked by a free-
running E 64 signal.
The active edge of the PACB and PAC3…PAC0 are identical to those of IC0 and
IC3…IC0, respectively. Therefore, one needs to use the TCTL4 register to select the
active edges for these pulse accumulators.
The 16-bit PACA has two interrupt sources: PT7-edge and PACA counter overflow.
Only two (PAC3 and PAC1) of the 8-bit pulse accumulators can generate interrupt. These
two pulse accumulators can interrupt whenever their counters overflow.
PACB can interrupt the MCU whenever its upper 8-bit counter overflows.
66
33
The HCS12/MC9S12 Microcontroller
The operation of the 16-bit PACA is controlled by the PACTL register. The contents
of this register are shown in Figure 8.27.
PACA has a 16-bit counter which comprises of PAC3 and PAC2. This 16-bit counter
can be accessed by using the name PACNT.
The status of the PACA is recorded in the PAFLG register.
The operation of the PACB is controlled by the PBCTL register.
The status of PACB is recorded in the PBFLG register.
Each of the 8-bit pulse accumulators can be enabled by setting a proper bit in the
ICPAR register.
Each of the 8-bit pulse accumulator also has a holding register (PA3H…PA0H).
The user can prevent the 8-bit pulse accumulators from counting further than $FF by
setting the PACMX bit of the ICSYS register.
67
68
34
The HCS12/MC9S12 Microcontroller
69
70
35
The HCS12/MC9S12 Microcontroller
7 6 5 4 3 2 1 0
SH37 SH26 SH15 SH04 TFMOD PACMX BUFEN LATQ
reset: 0 0 0 0 0 0 0 0
SHxy -- share input action of input capture channel x and y bits
0 = normal operation
1 = The channel input x causes the same action on the channel y.
TFMOD -- timer flag-setting mode bit
0 = The timer flags C3F-C0F in TFLG1 are set when a valid input
capture transition on the corresponding port pin occurs.
1 = If in queue mode (BUFEN = 1 and LATQ = 0), the timer flags
C3F-C0F in TFLG1 are set only when a latch on the
corresponding holding register occurs. If the queue mode is not
engaged, the timer flags C3F-C0F are set the same way as for
TFMOD = 0.
PACMX -- 8-bit pulse accumulator maximum count bit
0 = normal operation. When the 8-bit pulse accumulator has reached
the value $FF, with the next active edge, it will be incremented
to $00.
1 = When the 8-bit pulse accumulator has reached the value $FF, it
will not be incremented further. The value $FF indicates a count
of 255 or more.
BUFEN -- IC buffer enable bit
0 = input capture and pulse accumulator holding registers are
disabled.
1 = input capture and pulse accumulator holding registers are
enabled.
LATQ -- input capture latch or queue mode select bit
The BUFEN bit should be set to enable IC and pulse accumulator's
holding registers. Otherwise, LATQ latching mode is disabled.
0 = queue mode of input capture is enabled
1 = latch mode is enabled. Latching function occurs when modulus
down-counter reaches 0 or a 0 is written into the count register
MCCNT. With a latching event, the contents of IC registers and
8-bit pulse accumulators are transferred to their holding registers.
The 8-bit pulse accumulators are cleared.
Figure 8.31 Input control system control register (ICSYS)
71
Example 8.13 Suppose that certain events are converted into pulses and connected to the
PT7 pin. Write a program that enables the PACA to generate an interrupt to the MCU when
N events have occurred.
Solution: By writing the 2s complement of N, the PACA will interrupt the MCU when the
nth event arrives:
#include "c:\miniide\hcs12.inc"
N equ 1350
org $1500
lds #$1500 ; set up stack pointer
movw #paov_isr,UserPAccOvf ; set up PAOV interrupt vector
ldd #N ; place the 2s complement in PACNT
coma ; “
comb ; “
addd #1 ; “
std PACNT ; “
movb #$52,PACTL ; enable PACA, event counting mode, active
; edge is rising
cli ; enable PAOV interrupt
; …
swi
paov_isr movb #PAOVF,PAFLG ; clear the PAOVF flag
end
72
36
The HCS12/MC9S12 Microcontroller
#include “c:\cwHCS12\include\hcs12.h”
#define NN 1252
void main(void)
{
PACNT = ~NN + 1; // load the 2’s complement of N into PACNT
PACTL = 0x52; // enable PACTL, event counting mode
asm("cli"); // enable PAOV interrupt
....
}
Interrupt void paov_isr(void)
{
PAFLG = PAOVF;
}
73
Step 1
Connect the signal to the PT7 pin.
Step 2
Set up the PACA to operate in event counting mode.
Step 3
Use one of the output-compare functions to create a 1-s time interval or call the library
delay function. (Or use the modulus down counter).
Step 4
Use a memory location to keep track of the number of times that the PACNT overflows.
Step 5
Enable the PAOV interrupt.
Step 6
Disable the PAOV interrupt at the end of one second.
The assembly and C programs for measuring signal frequency is in the following pages:
74
37
The HCS12/MC9S12 Microcontroller
#include "c:\miniide\hcs12.inc"
org $1000
oc_cnt rmb 1
paov_cnt rmb 2 ; use to keep track of PACNT overflow count
frequency rmb 4 ; hold the signal frequency
org $1500
lds #$1500
movw #paov_isr,UserPAccOvf ; set up PAOV interrupt vector
movb #50,oc_cnt ; prepare to perform 50 OC0 actions
ldd #0
std PACNT ; let PACNT count up from 0
std paov_cnt ; initialize PACNT overflow count to 0
std frequency ; initialize frequency to 0
std frequency+2 ; "
movb #$90,TSCR1 ; enable TCNT and fast timer flag clear
bset TIOS,OC0 ; select OC0 function
movb #$03,TSCR2 ; set prescaler to TCNT to 8
bclr DDRT,$80 ; configure PT7 for input
; configure PA function: enable PA, select event counting mode, rising edge
; of PAI signal increments the PACNT counter, enable PAOV interrupt
movb #$52,PACTL
cli ; enable PAOV interrupt
ldd TCNT
75
76
38
The HCS12/MC9S12 Microcontroller
#include "c:\cwHCS12\include\hcs12.h"
unsigned long int frequency;
unsigned int paov_cnt;
void main (void)
{
int oc_cnt;
PACNT = 0;
frequency = 0;
paov_cnt = 0;
TSCR1 = 0x90; // enable TCNT and fast flag clear
TIOS = OC0; // select OC0 function
TSCR2 = 0x03; // set prescale factor to 8
PACTL = 0x52; // enable PA function, enable PAOV interrupt
DDRT &= 0x7F; // configure the PT7 pin for input
asm("cli"); // enable interrupt globally
oc_cnt = 50;
TC0 = TCNT + 60000u;
while (oc_cnt) {
while(!(TFLG1 & C0F));
TC0 = TC0 + 60000u;
77
oc_cnt --;
}
PACTL = 0x00; // disable PA function
asm("sei");
frequency = (long)paov_cnt * 65536l + (long)PACNT;
asm("swi");
}
interrupt void paovISR (void)
{
PAFLG = PAOVF; // clear PAOVF flag
paov_cnt = paov_cnt + 1;
}
The function that sets up the interrupt vector for pulse accumulator A is as follows:
78
39
The HCS12/MC9S12 Microcontroller
Step 1
Select gated time accumulation mode, and initialize PACNT to 0.
Step 2
Select the falling edge as the active edge, which will enable PACNT to count when the PAI
pin is high.
Step 3
Enable the PAI active edge interrupt and wait for the arrival of the active edge of PAI.
Step 4
Stop the pulse accumulator counter when the interrupt arrives.
Example 8.13 Write a program to measure the duration of an unknown signal connected to
the PAI pin.
Solution: The assembly program is as follows:
79
#include "c:\miniide\hcs12.inc"
org $1000
paovCnt ds.b 1 ; use to keep track of the PACNT overflow count
pulse_width ds.b 3 ; hold the pulse width
org $1500
movw #paov_isr,UserPAccOvf ; set up PAOV interrupt vector
ldd #0
std PACNT ; let PACNT count up from 0
clr paovCnt ; initialize PACNT overflow count to 0
movb #$0,TSCR2 ; set TCNT timer prescaler to 1
movb #$72,PACTL
bclr DDRT,$80 ; configure PAI pin for input
cli ; enable PAOV interrupt
brclr PAFLG,PAIF,* ; wait for the arrival of the falling edge of PAI
movb #0,PACTL ; disable PA function
sei ; disable interrupt
ldd PACNT
std pulse_width+1
ldaa paov_cnt
staa pulse_width
swi
80
40
The HCS12/MC9S12 Microcontroller
Modulus Down-Counter
81
82
41
The HCS12/MC9S12 Microcontroller
Example 8.17 Write an instruction sequence to generate periodic interrupt every 10 ms.
Solution: One possible value to be written into the MCCTL register is $C7 which will:
enable MCCNT
enable MCCNT interrupt
enable modulus mode
set prescaler to 16
The instruction sequence to achieve the desired setting is as follows:
movb #$C7,MCCTL
movw #15000,MCCNT ; place the value that will be decremented to
; to 0 in 10 ms
cli ; enable interrupt
83
Example 8.18 Write a subroutine to create a time delay that is a multiple of 10 ms using
the modulus down counter. The multiple is passed in Y. E clock is 24 MHz.
Solution:
By setting the prescaler to 16, the value of 15000 will take 10 ms to decrement to 0. The
following subroutine will create a time delay equals to a multiple of 10 ms:
delayby10ms
bset TSCR1,TFFCA ; enable timer fast flag clear
movb #$07,MCCTL ; enable modulus down counter with 1:16 as prescaler
movw #15000,MCCNT ; load the value to be down counted
brclr MCFLG,MCZF,*
bclr MCCTL,$04 ; disable modulus down counter
dbne y,delay10ms
rts
Time delays equal to a multiple of 10 ms, 50 ms, 1 ms, 100ms, and 1s can be created by
modifying this subroutine.
These six delay functions are placed in the file delay.asm and delay.c and can be included
in user’s program.
84
42
The HCS12/MC9S12 Microcontroller
85
86
43
The HCS12/MC9S12 Microcontroller
The prescale factors for clock A and clock B are determined by the PCKA2…PCKA0 and
PCKB2…PCKB0 bits of the PWMPRCLK register.
Clock SA is derived by dividing clock A by the value of the PWMSCLA register and
then dividing by 2.
Clock SB is derived by dividing clock B by the value of the PWMSCLB register and
then dividing by 2.
The clock source selection is controlled by the PWMCLK register.
7 6 5 4 3 2 1 0
0 PCKB2 PCKB1 PCKB0 0 PCKA2 PCKA1 PCKA0
reset: 0 0 0 0 0 0 0 0
Table 8.3 Clock B prescaler selects Table 8.4 Clock A prescaler selects
value of clock value of clock
PCKB2 PCKB1 PCKB0 PCKA2 PCKA1 PCKA0
B A
0 0 0 E clock 0 0 0 E clock
0 0 1 E clock/2 0 0 1 E clock/2
0 1 0 E clock/4 0 1 0 E clock/4
0 1 1 E clock/8 0 1 1 E clock/8
1 0 0 E clock/16 1 0 0 E clock/16
1 0 1 E clock/32 1 0 1 E clock/32
1 1 0 E clock/64 1 1 0 E clock/64
1 1 1 E clock/128 1 1 1 E clock/128
87
7 6 5 4 3 2 1 0
PCLK7 PCLK6 PCLK5 PCLK4 PCLK3 PCLK2 PCLK1 PCLK0
reset: 0 0 0 0 0 0 0 0
The main part of each PWM channel x consists of an 8-bit counter (PWMCNTx), an
8-bit period register (PWMPERx), and an 8-bit duty cycle register (PWMDTYx).
The waveform output period is controlled by the match between the PWMPERx
register and PWMCNTx register.
The waveform output duty cycle is controlled by the match of the PWMDTYx
register and the PWMCNTx register.
The starting polarity of the output is selectable on a per channel basis by programming
the PWMPOL register.
A PWM channel must be enabled by setting the proper bit of the PWME register.
The overall operation of the PWM module is shown in Figure 8.44.
88
44
The HCS12/MC9S12 Microcontroller
7 6 5 4 3 2 1 0
PPOL7 PPOL6 PPOL5 PPOL4 PPOL3 PPOL2 PPOL1 PPOL0
reset: 0 0 0 0 0 0 0 0
7 6 5 4 3 2 1 0
PWME7 PWME6 PWME5 PWME4 PWME3 PWME2 PWME1 PWME0
reset: 0 0 0 0 0 0 0 0
89
clock
source
8-bit counter
GATE
PWMCNTx From port PTP
(clock edge sync)
data register
up/
reset
down
8-bit compare=
T Q M M
U to pin
PWMDTYx U driver
R Q X X
PPOLx
8-bit compare=
PWMPERx
Q T CAEx
Q R
PWMEx
Figure 8.44 PWM channel block diagram
90
45
The HCS12/MC9S12 Microcontroller
Left-Aligned Output
The PWMCNTx counter is configured as a count-up counter.
Polarity = 0
PWMx duty cycle = [(PWMPERx – PWMDTYx) PWMPERx] 100%
Polarity = 1
PWMx duty cycle = [PWMDTYx PWMPERx] 100%
91
Center-Aligned Mode
PWM counter operates as an up/down counter and is set to count up whenever the counter
is equal to $00.
When the counter matches the duty register the output flip-flop changes state causing the
PWM output to also change state.
A match between the PWM counter and the period register changes the counter direction
from an up-count to a down-count.
When the PWM counter decrements and matches the duty register again, the output
flip-flop changes state causing the PWM output to also change state.
When the PWM counter decrements to 0, the counter direction changes from a down-count
back to an up-count and the period and duty registers are reloaded from their buffers.
92
46
The HCS12/MC9S12 Microcontroller
In Center-Aligned Mode,
When polarity = 0,
PWMx duty cycle = [(PWMPERx – PWMDTYx) PWMPERx] 100%
When polarity = 1,
PWMx duty cycle = [PWMDTYx PWMPERx] 100%
93
94
47
The HCS12/MC9S12 Microcontroller
95
Example 8.21 Write an instruction sequence to generate a 100KHz waveform with 50%
duty cycle from the PWM0 pin (PP0). Assume that the E clock frequency is 24 MHz.
Solution: Use the following setting:
select clock A as the clock source to PWM0 and set its prescaler to 2.
select left-aligned mode
load the value 120 into the PWMPER0 register (= 24000000 100000 2)
load the value 60 into the PWMDTY0 register (= 120 50%)
#include “c:\miniide\hcs12.inc”
…
movb #0,PWMCLK ; select clock A as the clock source for PWM0
movb #1,PWMPRCLK ; set clock A prescaler to 2
movb #1,PWMPOL ; channel 0 output high at the start of the period
movb #0,PWMCAE ; select left-aligned mode
movb #$0C,PWMCTL ; 8-bit mode, stop PWM in wait and freeze mode
movb #120,PWMPER0 ; set period value
movb #60,PWMDTY0 ; set duty value
movb #0,PWMCNT0 ; reset the PWM0 counter
bset PWMEN,PWME0 ; enable PWM channel 0
96
48
The HCS12/MC9S12 Microcontroller
Example 8.22 Write an instruction sequence to generate a square wave with 20 us period
and 60% duty cycle from PWM0 and use center-aligned mode.
Solution:
Select clock A as the clock source and set its prescaler to 2.
Load the value 120 into PWMPER0 register.
PWMPER0 = (20 24,000,000 1000,000) 2 2 = 120
PWMDTY0 = PWMPER0 60% = 72.
97
98
49
The HCS12/MC9S12 Microcontroller
Example 8.24 Use PWM to dim the light bulb. Assume that we use the PWM0 output to
control the brightness of a light bulb. Write a C program to dim the light to 10% brightness
gradually in five seconds. The E clock frequency is 24 MHz.
Solution:
Set duty cycle to 100% at the beginning.
Dim the brightness by 10% in the first second and then 20% per second in the following
four seconds.
Load 100 into the PWMPER0 register at the beginning.
Decrement PWMPER0 by 1 every 100 ms during the first second and decrement
PWMPER0 by 2 every 100 ms in the following four seconds.
#include "c:\cwHCS12\include\hcs12.h"
#include "c:\cwHCS12\include\delay.h"
99
void main ()
{
int dim_cnt;
PWMCLK = 0; /* select clock A as the clock source */
PWMPOL = 1; /* make waveform to start with high level */
PWMCTL = 0x0C; /* select 8-bit mode */
PWMPRCLK = 2; /* set clock A prescaler to 4 */
PWMCAE = 0; /* select left-aligned mode */
PWMPER0 = 100; /* set period of PWM0 to 0.1 ms */
PWMDTY0 = 100; /* set duty cycle to 100% */
PWME |= 0x01; /* enable PWM0 channel */
/* reduce duty cycle 1 % per 100 ms in the first second */
for (dim_cnt = 0; dim_cnt < 10 ; dim_cnt ++) {
delayby100ms(1);
PWMDTY0--;
}
/* reduce duty cycle 2% per 100 ms in the next 4 seconds */
for (dim_cnt = 0; dim_cnt < 40; dim_cnt ++) {
delayby100ms(1);
PWMDTY0 -= 2;
}
while (1);
}
100
50
The HCS12/MC9S12 Microcontroller
DC Motor Control
The DC motor has a permanent magnetic field and its armature is a coil.
When a voltage and a subsequent current flow are applied to the armature, the motor
begins to spin.
The voltage level applied across the armature determines the speed of rotation.
Almost every application uses a DC motor requires it reverse its direction of rotation or
vary its speed.
Reversing the direction is done by changing the polarity of voltage applied to the motor.
Changing the speed requires varying the voltage level of the input to the motor.
Changing the voltage level can be achieved by varying the pulse width of a digital signal
input to the DC motor.
The HCS12 can interface with a DC motor through a driver as shown in Figure 8.52.
A suitable driver must be selected to take control signals from the HCS12 and deliver the
necessary voltage and current to the motor.
An example of DC motor driver is shown in Figure 8.53.
The L293 has two supply voltages: VSS and VS. VSS is logic supply and can be from 4.5 to
36V. VS is analog and can be as high as 36 V.
101
102
51
The HCS12/MC9S12 Microcontroller
103
DC Motor Feedback
The DC motor speed must be fed back to the microcontroller so that it can be controlled.
The motor speed can be fed back by using an optical encoder, infrared detector, or a Hall-
effect sensor.
Basing on the speed feedback, the microcontroller can make adjustment to increase or
decrease the speed, reverse the direction, or stop the motor.
Assuming two magnets are attached to the shaft (rotor) of a DC motor and a Hall-effect
transistor is mounted on the armature (stator).
As shown in Figure 8.54, every time the Hall-effect transistor passes through the magnetic
field, it generates a pulse.
The input capture function of the HCS12 can capture the passing time of the pulse. The
time between two captures is half of a revolution. Thus the motor speed can be
calculated.
104
52
The HCS12/MC9S12 Microcontroller
105
106
53
The HCS12/MC9S12 Microcontroller
107
Example 8.25 Write a subroutine in C language to measure the motor speed (in rpm)
assuming E clock is 16 MHz.
Solution:
- Two consecutive rising edges on the PT0 pin must be captured in order to measure the
motor speed.
- Let diff be the difference of two captured edges and the period is set to 1 ms, then
108
54
The HCS12/MC9S12 Microcontroller
#include “c:\cwHCS12\include\hcs12.h”
109
Electrical Braking
Electrical braking is done by reversing the voltage applied to the motor for appropriate
amount of time.
In Figure 8.56, the motor can be braked by (1) reducing the PWM duty-count to 0, (2)
setting the PP7 pin output to high for an appropriate amount of time.
110
55