chapter 8 hcs12 timer functions. why are timer functions important? it is very difficult and...

129
Chapter 8 HCS12 Timer Functions

Post on 20-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Chapter 8

HCS12 Timer Functions

Page 2: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Why are Timer Functions Important?

• It is very difficult and impossible to implement the following applications without a timer function:– Time delay creation and measurement– Period and pulse width measurement– Frequency measurement– Event counting– Arrival time comparison– Time-of-day tracking– Periodic interrupt generation– Waveform generation

Page 3: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

The HCS12 Timer System (1 of 2)

• The HCS12 has a standard timer module (TIM) that consists of: – Eight channels of multiplexed input capture and output compare functions.– 16-bit pulse accumulator A– 16-bit timer counter– The TIM block diagram is shown in Figure 8.1.

• The HCS12 devices in the automotive family have implemented an Enhanced Capture Timer module (ECT). The ECT module contains:

– All the features contained in the TIM module– One 16-bit buffer register for each of the input capture channels– Four 8-bit pulse accumulator– A 16-bit Modulus Down Counter with 4-bit prescaler– Four user selectable delay counters for increasing input noise immunity

• The TIM (of course ECT also) shares the eight Port T pins (IOC0…IOC7).

Page 4: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Prescaler

16-bit counter

Input Capture

Output compare

Channel 0

Input Capture

Output compare

Channel 1

Input Capture

Output compare

Channel 2

Input Capture

Output compare

Channel 3

Input Capture

Output compare

Channel 4

Input Capture

Output compare

Channel 5

Input Capture

Output compare

Channel 6

Input Capture

Output compare

Channel 7

Registers

16-bit Pulseaccumulator A

IOC0

IOC1

IOC2

IOC3

IOC4

IOC5

IOC6

IOC7

Bus clock

Timer overflowinterrupt

TC0 interrupt

TC1 interrupt

TC2 interrupt

TC3 interrupt

TC4 interrupt

TC5 interrupt

TC6 interrupt

TC7 interrupt

PA overflowinterruptPA inputinterrupt

Figure 8.1 HCS12 Standard Timer (TIM) block diagram

The HCS12 Timer System (2 of 2)

Page 5: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Timer Counter Register (TCNT)

• Required for input capture and output compare functions

• Must be accessed in one 16-bit operation in order to obtain the correct value

• Three other registers related to the operation of the TCNT: TSCR1, TSCR2, TFLG2.

Page 6: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

TEN TSWAI TSFRZ TFFCA 0 0 0 0valueafter reset 0 0 0 0 0 0 0 0

TEN -- timer enable bit

0 = disable timer; this can be used to save power consumption1 = allows timer to function normally

TSWAI -- timer stops while in wait mode bit0 = allows timer to continue running during wait mode1 = disables timer when MCU is in wait mode

TSFRZ -- timer and modulus counter stop while in freeze mode

0 = allows timer and modulus counter to continue running while in freeze mode1 = disables timer and modulus counter when MCU is in freeze mode

TFFCA -- timer fast flag clear all bit

0 = allows timer flag clearing to function normally1 = For TFLG1, a read from an input capture or a write to the output compare channel causes the corresponding channel flag, CnF, to be cleared. For TFLG2, any access to the TCNT register clears the TOF flag. Any access to the PACN3 and PACN2 registers clears the PAOVF and PAIF flags in the PAFLG register. Any access to the PACN1 and PACN0 registers clears the PBOVF flag in the PBFLG register.

Figure 8.2 Timer system control register 1 (TSCR1)

Timer System Control Register 1 (TSCR1)

• The contents of TSCR1 are shown in Figure 8.2.

• Setting and clearing the bit 7 of TSCR1 will start and stop the counting of the TCNT.

• Setting the bit 4 will enable fast timer flag clear function. If this bit is clear, then the user must write a one to a timer flag in order to clear it.

Page 7: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Timer System Control Register 2 (TSCR2)

• Bit 7 is the TCNT overflow interrupt enable bit.• TCNT can be reset to 0 when TCNT equals TC7

by setting bit 3 of TSCR2.• The clock input to TCNT can be prescaled by a

factor selecting by bits 2 to 0 of TSCR2.• The contents of TSCR2 are shown in Figure 8.2.

Page 8: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

TOI 0 0 0 TCRE PR2 PR1 PR0valueafter reset 0 0 0 0 0 0 0 0

TOI -- timer overflow interrupt enable bit 0 = interrupt inhibited 1 = interrupt requested when TOF flag is setTCRE -- timer counter reset enable bit 0 = counter reset inhibited and counter free runs 1 = counter reset by a successful output compare 7 If TC7 = $0000 and TCRE = 1, TCNT stays at $0000 continuously. If TC7 = $FFFF and TCRE = 1, TOF will never be set when TCNT rolls over from $FFFF to $0000.

Figure 8.3 Timer system control register 2 (TSCR2)

PR2 PR1 PR0 Prescale Factor

00001111

00110011

01010101

1248

163264128

Table 8.1 Timer counter prescale factor

Timer Interrupt Flag 2 Register (TFLG2)

• Only bit 7 (TOF) is implemented. Bit 7 will be set whenever TCNT overflows.

Page 9: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Rising edge Falling edge

or

Figure 8.4 Events represented by signal edges

Input Capture Functions (1 of 2)

• Physical time is often represented by the contents of the main timer.• The occurrence of an event is represented by a signal edge (rising

or falling edge).• The time when an event occurs can be recorded by latching the

count of the main timer when a signal edge arrives as illustrated in Figure 8.4.

• The HCS12 has eight input capture channels. Each channel has a 16-bit capture register, an input pin, edge-detection logic, and interrupt generation logic.

• Input capture channels share most of the circuit with output compare functions. For this reason, they cannot be enabled simultaneously.

Page 10: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

IOS7 IOS6 IOS5 IOS4 IOS3 IOS2 IOS1 IOS0valueafter reset 0 0 0 0 0 0 0 0

Figure 8.5 Timer input capture/output compare select register (TIOS)

IOS[7:0] -- Input capture or output compare channel configuration bits 0 = The corresponding channel acts as an input capture 1 = The corresponding channel acts as an output compare

Input Capture Functions (2 of 2)• The selection of input capture and output compare is done by programming

the TIOS register. • The contents of the TIOS register are shown in Figure 8.5. Setting a bit

select the output compare function. Otherwise, the input capture function is selected.

• The following instruction will enable the output compare channels 7...4 and input capture channel 3…0:

movb #$F0,TIOS

Page 11: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Timer Port Pins

• 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.

Page 12: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

EDG7B EDG7A EDG6B EDG6A EDG5B EDG5A EDG4B EDG4Avalueafter reset 0 0 0 0 0 0 0 0

Figure 8.5 Timer control register 3 and 4

7 6 5 4 3 2 1 0

EDG3B EDG3A EDG2B EDG2A EDG1B EDG1A EDG0B EDG0A

0 0 0 0 0 0 0 0

(a) Timer control register 3 (TCTL3)

(b) Timer control register 4 (TCTL4)

EDGnB EDGnA -- Edge configuration

0 0 : Capture disabled0 1 : Capture on rising edges only1 0 : Capture on falling edges only1 1 : Capture on both edges

Timer Control Register 3 and 4

• The signal edge to be captured is selected by TCTL3 and TCTL4.

• The edge to be captured is selected by two bits. The user can choose to capture the rising edge, falling edge, or both edges.

Page 13: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

C7I C6I C5I C4I C3I C2I C1I C0I

reset: 0 0 0 0 0 0 0 0

Figure 8.7 Timer interrupt enable register (TIE)

C7I-C0I: input capture/output compare interrupt enable bits 0 = interrupt disabled 1 = interrupt enabled

Timer Interrupt Enable Register (TIE)

• The arrival of a signal edge may optionally generate an interrupt to the CPU.

• The enabling of the interrupt is controlled by the Timer Interrupt Enable Register.

Page 14: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

C7F C6F C5F C4F C3F C2F C1F C0F

reset: 0 0 0 0 0 0 0 0

Figure 8.8 Timer interrupt flag register 1 (TFLG1)

CnF: input capture/ output compare interrupt flag bits 0 = interrupt condition has not occurred 1 = interrupt condition has occurred

Timer Interrupt Flag 1 Register (TFLG1)

• Whenever a signal edge arrives, the associated timer interrupt flag will be set to 1.

Page 15: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

How to Clear a Timer Flag Bit• In normal mode, write a 1 to the flag bit to be cleared.• Method 1

– Use the BCLR instruction with a 0 at the bit position (s) corresponding to the flag (s) to be cleared. For example,

BCLR TFLG1, $FE

will clear the C0F flag. • 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

will clear the C0F flag.• When fast timer flag clear function is enabled, see Figure 8.1.

Page 16: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

- Pulse width measurement: need to capture the rising and falling edges

one period

(a) Capture two rising edges

one period

(b) Capture two falling edges

Figure 8.9 Period measurement by capturing two consecutive edges

Pulse width

Rising edge Falling edge

Figure 8.10 Pulse-width measurement using input capture

Applications of Input Capture Function• Event arrival time

recording• Period measurement:

need to capture the main timer values corresponding to two consecutive rising or falling edges

Page 17: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

- Time reference: often used in conjunction with an output compare function

Start ofinterval

End ofinterval

e1e2

e3 e4 ei ej

Figure 8.11 Using an input-capture function for event counting

... ...

Time t0

Time of reference(set up by signal edge)

Time t0 + delay

Time to activateoutput signal

(set up by output compare)

Figure 8.12 A time reference application

Input Capture• Interrupt generation: Each input capture function can be used as an edge-

sensitive interrupt source.• Event counting: count the number of signal edges arrived during a period

Page 18: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

T

T

duty cycle =T

T* 100%

Figure 8.13 Definition of duty cycle

Duty Cycle Measurement

Page 19: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

T

T

signal S1

signal S2

phase difference =T

T* 360o

Figure 8.14 Phase difference definition for two signals

Phase Difference Measurement

Page 20: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Period Measurement (1 of 2)• Example 8.2 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: 216 ÷ 24 MHz = 2.73 ms.

• To measure a period that is equal to 128 ms, we have two options: – Set the prescale factor to 1 and keep track of the number of times the

timer counter overflows.– Set the prescale factor to 64 and do not keep track of the number of

times 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.

Page 21: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Start

Choose to capture the rising edgeSet the timer counter prescale factor to 16Enable the timer counter

Clear the C0F flag

C0F = 1?

yes

no

Saved the captured first edgeClear the C0F flag

C0F = 1?no

yes

Take the difference of the second and thefirst captured edges

Stop

Figure 8.16 Logic flow of period measurement program

Period Measurement (2 of 2)

Page 22: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"org $1000

edge1 ds.b 2 ; memory to hold the first edgeperiod ds.b 2 ; memory to store the period

org $1500movb #$90,TSCR1 ; enable timer counter and enable fast timer flags clearbclr TIOS,IOS0 ; enable input-capture 0movb #$06,TSCR2 ; disable TCNT overflow interrupt, set prescaler to 64movb #$01,TCTL4 ; capture the rising edge of PT0 signalmovb #C0F,TFLG1 ; clear the C0F flagbrclr TFLG1,C0F,* ; wait for the arrival of the first rising edgeldd TC0 ; save the first edge and clear the C0F flagstd edge1brclr TFLG1,C0F,* ; wait for the arrival of the second edgeldd TC0subd edge1 ; compute the periodstd periodswiend

Assembly Program for Period Measurement

Page 23: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\egnu091\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; asm ("swi");}

C Program for Period Measurement

Page 24: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

 Case 1

edge2 edge1

pulse width = ovcnt × 216 + diff Case 2

edge2 < edge 1 

pulse width = (ovcnt – 1) × 216 + diff

• 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:– Set the prescale factor to TCNT to 32. Use clock cycle as the unit of

measurement.– 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

– The pulse width can be calculated by the following equations:

Page 25: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Sta

rt

ov

erfl

ow

0

Set

up

to

cap

ture

th

e ri

sin

g e

dg

e.D

isab

le a

ll i

nte

rru

pts

.

C0

F =

1?

Cle

ar tim

er o

ver

flo

w f

lag

.E

nab

le m

ain

tim

er o

ver

flo

w in

terr

up

t.

C0

F =

1?

Co

mp

ute

th

e d

iffe

ren

ce o

f tw

o e

dg

es.

Cle

ar C

0F

fla

g.

Sav

e th

e fi

rst

cap

ture

d e

dg

e.

yes

yes

no

no

Is s

eco

nd

ed

ge

smal

ler?

ov

erfl

ow

o

ver

flo

w -

1

Co

mb

ine

the

resu

lts.

Sto

p

no

yes

Cle

ar T

OF

fla

g.

ov

erfl

ow

o

ver

flo

w +

1.

Ex

ecu

te t

he

RT

I in

stru

ctio

n.

Tim

er o

ver

flo

w in

terr

up

tse

rvic

e ro

uti

ne

Fig

ure

8.1

7 L

og

ic f

low

fo

r m

easu

rin

g p

uls

e w

idth

of

slo

w s

ign

als

Page 26: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"org $1000

edge1 ds.b 2overflow ds.b 2pulse_width ds.b 2

org $1500movw #tov_isr,UserTimerOvf ; set up TCNT overflow interrupt vectorlds #$1500 ; set up stack pointermovw #0,overflowmovb #$90,TSCR1 ; enable TCNT and fast timer flag clearmovb #$05,TSCR2 ; disable TCNT interrupt, set prescaler to 32bclr TIOS,IOS0 ; select IC0movb #$01,TCTL4 ; capture rising edgemovb #C0F,TFLG1 ; clear C0F flag

wait1 brclr TFLG1,C0F,wait1; wait for the first rising edgemovw TC0,edge1 ; save the first edge & clear the C0F flagmovb #TOF,TFLG2 ; clear TOF flagbset TSCR2,$80 ; enable TCNT overflow interruptcli ; "movb #$02,TCTL4 ; capture the falling edge on PT0 pin

wait2 brclr TFLG1,C0F,wait2; wait for the arrival of the falling edgeldd TC0subd edge1

Page 27: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

std pulse_widthbcc next ; is the second edge smaller?ldx overflow ; second edge is smaller, so decrementdex ; overflow count by 1stx overflow ; "

next swi

tov_isr movb #TOF,TFLG2 ; clear TOF flagldx overflowinxstx overflowrtiend

Page 28: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include <hcs12.h>#include <vectors12.h>#define INTERRUPT __attribute__((interrupt)) unsigned diff, edge1, overflow;unsigned long pulse_width;void INTERRUPT tovisr(void);void main(void){ UserTimerOvf = (unsigned short)&tovisr;

overflow = 0;TSCR1 = 0x90; /* enable timer and fast flag clear */TSCR2 = 0x05; /* set prescaler to 32, no timer overflow interrupt */TIOS &= ~IOS0; /* select input-capture 0 */TCTL4 = 0x01; /* prepare to capture the rising edge */TFLG1 = C0F; /* clear C0F flag */while(!(TFLG1 & C0F)); /* wait for the arrival of the rising edge */TFLG2 = TOF; /* clear TOF flag */

C Program for Pulse Width Measurement

Page 29: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

TSCR2 |= 0x80; /* enable TCNT overflow interrupt */asm("cli");edge1 = TC0; /* save the first edge */TCTL4 = 0x02; /* prepare to capture the falling edge */while (!(TFLG1 & C0F)); /* wait for the arrival of the falling edge */diff = TC0 - edge1;if (TC0 < edge1)

overflow -= 1;pulse_width = overflow * 65536u + diff;asm ("swi");

}void INTERRUPT tovisr(void){ TFLG2 = TOF; /* clear TOF flag */

overflow = overflow + 1;}

Page 30: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Output Compare Function

• The HCS12 has eight output compare functions.• Each output compare channel consists of

– A 16-bit comparator– A 16-bit compare register TCx (also used as inout capture

register)– An output action pin (PTx, can be pulled high, pulled low, or

toggled)– An interrupt request circuit– A forced-compare function (CFOCx)– Control logic

Page 31: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Operation of the Output-Compare Function (1 of 4)

• One of the applications of the output-compare function is to trigger an action at a specific time in the future.

• To use an output-compare function, the user– Makes a copy of the current contents of the TCNT

register– Adds to this copy a value equal to the desired delay– Stores the sum into an output-compare register (TCx,

x = 0..7)

Page 32: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

OM7 OL7 OM6 OL6 OM5 OL5 OM4 OL4valueafter reset 0 0 0 0 0 0 0 0

read: anytimewrite: anytime

Figure 8.18 Timer control register 1 and 2 (TCTL1 & TCTL2)

7 6 5 4 3 2 1 0

OM3 OL3 OM2 OL2 OM1 OL1 OM0 OL0

0 0 0 0 0 0 0 0

(a) TCTL1 register

(b) TCTL2 register

valueafter reset

OMn OLn : output level0011

0101

no action (timer disconnected from output pin)toggle OCn pinclear OCn pin to 0set OCn pin to high

Operation of the Output-Compare Function (2 of 4)• The actions that can be activated on an output compare pin include

– Pull up to high– Pull down to low– Toggle

• The action is determined by the Timer Control Register 1 & 2 (TCTL1 & TCTL2):

Page 33: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

300 s

700 s

Figure 8.19 1 KHz 30 percent duty cycle waveform

Operation of the Output-Compare Function (3 of 4)

• 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 ms. The numbers of clock cycles that the signal is high and low are 900 and 2100, respectively.

Page 34: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Start

Select pull high as pin action

Clear C0F flag

Start OC0 output comparewith a delay of 700 s

C0F = 1?

Select pull low as pin actionClear C0F flagStart OC0 output comparewith a delay of 300 s

C0F = 1?

no

no

yes

yes

Figure 8.20 The program logic flow for digital waveform generation

Operation of the Output-Compare Function (4 of 4)

Page 35: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"hi_time equ 900lo_time equ 2100

org $1500movb #$90,TSCR1 ; enable TCNT with fast timer flag clearmovb #$03,TSCR2 ; disable TCNT interrupt, set prescaler to 8bset TIOS,OC0 ; enable OC0movb #$03,TCTL2 ; select pull high as pin actionldd TCNT ; start an OC0 operation with 700 us as delay

repeat addd #lo_time ; "std TC0 ; "

low brclr TFLG1,C0F,low ; wait until OC0 pin go highmovb #$02,TCTL2 ; select pull low as pin actionldd TC0 ; start an OC operation with 300 us as delayaddd #hi_time ; "std TC0 ; "

high brclr TFLG1,C0F,high ; wait until OC0 pin go lowmovb #$03,TCTL2 ; select pull high as pin actionldd TC0bra repeatend

Page 36: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\egnu091\include\hcs12.h"#define hi_time 900#define lo_time 2100void main (void){ TSCR1 = 0x90; /* enable TCNT and fast timer flag clear */ TIOS |= OC0; /* enable OC0 function */ TSCR2 = 0x03; /* disable TCNT interrupt, set prescaler to 8 */ TCTL2 = 0x03; /* set OC0 action to be pull high */ TC0 = TCNT + lo_time; /* start an OC0 operation */ while(1) { while(!(TFLG1 & C0F)); /* wait for PT0 to go high */ TCTL2 = 0x02; /* set OC0 pin action to pull low */ TC0 += hi_time; /* start a new OC0 operation */ while(!(TFLG1 & C0F)); /* wait for PT0 pin to go low */ TCTL2 = 0x03; /* set OC0 pin action to pull high */ TC0 += lo_time; /* start a new OC0 operation */ }}

C Program for Generating 1 KHz Digital Waveform

Page 37: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

delayby1ms pshdmovb #$90,TSCR1 ; enable TCNT & fast flags clearmovb #$06,TSCR2 ; configure prescaler to 64bset TIOS,OC0 ; enable OC0ldd TCNT

again0 addd #375 ; start an output-compare operationstd TC0 ; with 1 ms time delay

wait_lp0 brclr TFLG1,OC0,wait_lp0ldd TC0dbne y,again0puldrts

• Example 8.5 Write a function to generate a time delay which 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 64– 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 375. (375 64 24000000 =

1 ms)

Page 38: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

void delayby1ms(int k){ int ix; TSCR1 = 0x90; /* enable TCNT and fast timer flag clear */ TSCR2 = 0x06; /* disable timer interrupt, set prescaler to 64 */ TIOS |= OC0; /* enable OC0 */ TC0 = TCNT + 375; for (ix = 0; ix < k; ix++) { while(!(TFLG1 & C0F)); TC0 += 375; } TIOS &= ~OC0; /* disable OC0 */}

Page 39: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\MiniIDE\hcs12.inc"CR equ $0DLF equ $0A

org $1000oc_cnt rmb 1frequencyrmb 2

org $1500movb #$90,TSCR1 ; enable TCNT and fast timer flags clearmovb #$02,TSCR2 ; set prescale factor to 4movb #$02,TIOS ; enable OC1 and IC0movb #100,oc_cnt ; prepare to perform 100 OC1 operation, each

; creates 10 ms delay and total 1 secondmovw #0,frequency ; initialize frequency count to 0movb #$01,TCTL4 ; prepare to capture the rising edges of PT0movb #C0F,TFLG1 ; clear the C0F flagbset TIE,IC0 ; enable IC0 interruptcli ; "

• Example 8.6 Use an input-capture and an output-compare functions to measure the frequency of the signal connected to the PT0 pin.

• Solution: To measure the frequency, we will– Use one of the output-compare function to create a one-second time base.– Keep track of the number of rising (or falling) edges that arrived at the PT0 pin

within one second.

Page 40: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

ldd TCNT ; start an OC1 operation with 10 ms delaycontinue addd #60000 ; "

std TC1 ; "w_lp brclr TFLG1,C1F,w_lp ; wait for 10 ms

ldd TC1dec oc_cntbne continueldd frequencypshdldd #msgjsr [printf,PCR]leas 2,spswi

msg db CR,LF,"The frequency is %d",CR,LF,0TC0_isr ldd TC0 ; clear C0F flag

ldx frequency ; increment frequency count by 1inx ; "stx frequency ; rtiorg $3E6E ; set up interrupt vector numberfdb TC0_isr ; for TC0end

Page 41: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include <hcs12.h>#include <vectors12.h>#include <convert.c>#include <stdio.c>#define INTERRUPT __attribute__((interrupt))unsigned int frequency;void INTERRUPT TC0_isr(void);void main(void){ char arr[7]; char *msg = "Signal frequency is "; int i, oc_cnt;

unsigned frequency; UserTimerCh0 = (unsigned short)&TC0_isr;

TSCR1 = 0x90; /* enable TCNT and fast flag clear */TSCR2 = 0x02; /* set prescale factor to 4 */TIOS = 0x02; /* select OC1 and IC0 */oc_cnt = 100; /* prepare to perform 100 OC1 operations */frequency = 0;

C Program for Frequency Measurement Using IC0

Page 42: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

TCTL4 = 0x01; /* prepare to capture PT0 rising edge */TFLG1 = C0F; /* clear C0F flag */TIE |= IC0; /* enable IC0 interrupt */asm("cli");TC1 = TCNT + 60000;while (oc_cnt) { while(!(TFLG1 & C1F));

TC1 = TC1 + 60000;oc_cnt = oc_cnt - 1;

} int2alpha(frequency, arr); puts(msg);

puts(&arr[0]);asm("swi");

}void INTERRUPT TC0_isr(void){ TFLG1 = C0F; /* clear C0F flag */

frequency ++;}

Page 43: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

HCS12DP256

PT5

3.3 F

Buzzer

Figure 8.21 Circuit connection for a buzzer

Making Sound Using the Output-Compare Function

• A sound can be generated by creating a digital waveform with appropriate frequency and using it to drive a speaker or a buzzer.

• The circuit connection for a buzzer is shown in Figure 8.21.

• The simplest song is a two-tone siren.

Page 44: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Algorithm for Generating a Siren• 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– Wait 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 restart 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.

Page 45: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#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 $1000delay ds.w 1 ; store the delay for output-compare operation

org $1500lds #$1500movw #oc5_isr,UserTimerCh5 ; initialize the interrupt vector entrymovb #$90,TSCR1 ; enable TCNT, fast timer flag clearmovb #$03,TSCR2 ; set main timer prescaler to 8

• 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.

Page 46: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

bset TIOS,OC5 ; enable OC5movb #toggle,TCTL1 ; select toggle for OC5 pin actionmovw #hi_freq,delay ; use high frequency delay count firstldd TCNT ; start the low frequency soundaddd delay ; "std TC5 ; "bset TIE,OC5 ; enable OC5 interruptcli ; "

forever ldy #5 ; wait for half a secondjsr delayby100ms ; "movw #lo_freq,delay ; switch to low frequency delay countldy #5jsr delayby100msmovw #hi_freq,delay ; switch to high frequency delay countbra forever

oc5_isr ldd TC5addd delaystd TC5rti

#include c:\miniide\delay.asm”end

Page 47: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\egnu091\include\hcs12.h"#include "c:\egnu091\include\delay.c"#include "c:\egnu091\include\v`ectors12.h"#define INTERRUPT __attribute__((interrupt))#define HiFreq 1250#define LoFreq 5000int delay; /* delay count for OC5 operation */void INTERRUPT oc5ISR(void);int main(void){ UserTimerCh5 = (unsigned short)&oc5ISR; TSCR1 = 0x90; /* enable TCNT and fast timer flag clear */ TSCR2 = 0x03; /* set prescaler to TCNT to 1:8 */ TIOS |= BIT5; /* enable OC5 */ TCTL1 = 0x04; /* select toggle for OC5 pin action */ delay = HiFreq; /* use high frequency delay count first */ TC5 = TCNT + delay; /* start an OC5 operation */ TIE |= BIT5; /* enable TC5 interrupt */ asm("cli");

C Program for Siren Generation (1 of 2)

Page 48: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

while(1) { delayby100ms(5); /* wait for half a second */ delay = LoFreq; /* switch to low frequency tone */ delayby100ms(5); /* wait for half a second */ delay = HiFreq; /* switch to high frequency tone */ } return 0;}void INTERRUPT oc5ISR(void){ TC5 += delay;}

C Program for Siren Generation (2 of 2)

Page 49: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Playing Songs Using the OC Function

• Place the frequencies and durations of all notes in a table.

• For every note, use the output-compare function to generate the digital waveform with the specified frequency and duration.

• The next example plays the US national anthem.

Page 50: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#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) noteD4 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)notes equ 101toggle equ $04 ; value to toggle the TC5 pin

Page 51: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

org $1000delay ds.w 1 ; store the delay for output-compare operationrep_cnt ds.b 1 ; repeat the song this many timesip ds.b 1 ; remaining notes to be played

org $1500lds #$1500

; establish the SRAM vector address for OC5movw #oc5_isr,UserTimerCh5movb #$90,TSCR1 ; enable TCNT, fast timer flag clearmovb #$03,TSCR2 ; set main timer prescaler to 8bset TIOS,OC5 ; enable OC5movb #toggle,tctl1 ; select toggle for OC5 pin actionldx #score ; use as a pointer to score tableldy #duration ; points to duration tablemovb #1,rep_cnt ; play the song twicemovb #notes,ipmovw 2,x+,delay ; start with zeroth noteldd TCNT ; play the first noteaddd delay ; "std TC5 ; "bset TIE,C5I ; enable OC5 interruptcli ; "

Page 52: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

forever pshy ; save duration table pointer in stackldy 0,y ; get the duration of the current notejsr delayby10ms ; "puly ; get the duration pointer from stackiny ; move the duration pointeriny ; "ldd 2,x+ ; get the next note, move pointerstd delay ; "dec ipbne foreverdec rep_cntbeq done ; if not finish playing, re-establishldx #score ; pointers and loop countldy #duration ; "movb #notes,ip ; "movw 0,x,delay ; get the first note delay countldd TCNT ; play the first noteaddd #delay ; "std TC5bra forever

done swi

Page 53: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

oc5_isr ldd TC5addd delaystd TC5rti

; ************************************************************************************; The following subroutine creates a time delay which is equal to [Y] times; 10 ms. The timer prescaler is 1:8.; ************************************************************************************delayby10ms

bset TIOS,OC0 ; enable OC0ldd TCNT

again1 addd #30000 ; start an output-compare operationstd TC0 ; with 10 ms time delay

wait_lp1 brclr TFLG1,C0F,wait_lp1ldd TC0dbne y,again1bclr TIOS,OC0 ; disable OC0rts

Page 54: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

score dw D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4Sdw D4,D4,D4,B4,A4,G4,F4S,E4,F4S,G4,G4,D4,B3,G3dw D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S,D4,D4,D4dw B4,A4,G4,F4S,E4,F4S,G4,G4,D4,B3,G3,B4,B4dw B4,C5,D5,D5,C5,B4,A4,B4,C5,C5,C5,B4,A4,G4dw F4S,E4,F4S,G4,B3,C4S,D4,D4,G4,G4,G4,F4Sdw E4,E4,E4,A4,C5,B4,A4,G4,G4,F4S,D4,D4dw 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 dw 30,10,40,40,40,80,30,10,40,40,40

dw 80,20,20,60,20,40,80,20,20,40,40,40,40,40dw 30,10,40,40,40,80,30,10,40,40,40,80,20,20dw 60,20,40,80,20,20,40,40,40,40,40,20,20dw 40,40,40,80,20,20,40,40,40,80,40,60,20,40dw 80,20,20,40,40,40,80,40,40,40,20,20dw 40,40,40,40,20,20,20,20,40,40,20,20dw 60,20,20,20,80,20,20,60,20,40,80end

Page 55: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\egnu091\include\hcs12.h”#include “c:\egnu091\include\vectors12.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 notes 101#define toggle 0x04#define INTERRUPT __attribute__((interrupt))int delay;

Page 56: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

void delayby10ms(int kk);void INTERRUPT oc5isr(void);unsigned int score [101] = {D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S, D4,D4,D4,B4,A4,G4,F4S,E4,F4S,G4,G4,D4,B3,G3, D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S,D4,D4,D4, B4,A4,G4,F4S,E4,F4S,G4,G4,D4,B3,G3,B4,B4, B4,C5,D5,D5,C5,B4,A4,B4,C5,C5,C5,B4,A4,G4, F4S,E4,F4S,G4,B3,C4S,D4,D4,G4,G4,G4,F4S, E4,E4,E4,A4,C5,B4,A4,G4,G4,F4S,D4,D4, G4,A4,B4,C5,D5,G4,A4,B4,C5,A4,G4};unsigned int dur [101] = {30,10,40,40,40,80,30,10,40,40,40, 80,20,20,60,20,40,80,20,20,40,40,40,40,40, 30,10,40,40,40,80,30,10,40,40,40,80,20,20, 60,20,40,80,20,20,40,40,40,40,40,20,20, 40,40,40,80,20,20,40,40,40,80,40,60,20,40, 80,20,20,40,40,40,80,40,40,40,20,20, 40,40,40,40,20,20,20,20,40,40,20,20, 60,20,20,20,80,20,20,60,20,40,80};

Page 57: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

main (void){ int j; UserTimerCh5 = (unsigned short)&oc5isr; 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 */ asm(" cli "); /* enable TC5 interrupt */ j = 0; delay = score[0]; TC5 = TCNT + delay; /* play the first note */ while (j < notes) { /* play the song once */ delay = score[j]; /* play the jth note */ delayby10ms(dur[j]); j++; } TIOS &= ~OC5; /* stop playing the song */ asm ("swi"); /* return to D-Bug12 monitor */}

Page 58: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

void delayby10ms(int kk){ int i; TIOS |= OC0; /* enable OC0 */ TC0 = TCNT + 30000; /* start one OC0 operation */ for (i = 0; i < kk; i++) { while(!(TFLG1 & C0F)); TC0 += 30000; } TIOS &= ~OC0;}void INTERRUPT oc5isr(void){ TC5 += delay;}

Page 59: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Drawback of the Song Algorithm

• Contiguous identical notes become one note• Cannot show crescendo and decrescendo effect• Cannot provide loudness control

Page 60: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset 0 0 0 0 0 0 0 0

OC7M7 OC7M6 OC7M5 OC7M4 OC7M3 OC7M2 OC7M1 OC7M0

OC7Mn n = 0..7

0 = PTn pin is not affected by OC7 function1 = A successful OC7 action will override a successful OC6-OC0 compare action during the same cycle and the OCn action taken will depend on the corresponding OC7D bit.

Figure 8.22 Output Compare 7 Mask Register (OC7M)

7 6 5 4 3 2 1 0

reset 0 0 0 0 0 0 0 0

OC7D7 OC7D6 OC7D5 OC7D4 OC7D3 OC7D2 OC7D1 OC7D0

Figure 8.23 Output Compare 7 Data Register (OC7D)

Using OC7 to Control Multiple OC Functions• OC7 can control up to eight channels of OC functions.• The register OC7M specifies which OC channels are controlled by OC7.• The register OC7D specifies the value that any PTx pin to assume when the

OC7 operation succeeds.• For OC0 to OC6, when the OC7Mn (n = 0,…,6) bit is set, a successful OC7

compare overrides a successful OC0…OC6 compare pin action during the same cycle.

Page 61: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

• 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: – 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,OC7Mmovb #$14,OC7D

Page 62: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

FOC7 FOC6 FOC5 FOC4 FOC3 FOC2 FOC1 FOC0

Figure 8.24 Contents of the CFORC register

Forced Output-Compare (1 of 2)

• 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.

Page 63: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Table 8.2 Pin actions on PT7-PT0 pins

Register Bit positions Value Action to be triggered

TCTL1

TCTL2

7 65 43 21 0

7 65 43 21 0

1 10 10 11 0

0 11 01 11 0

set the PT7 pin to hightoggle the PT6 pintoggle the PT5 pinpull the PT4 pin to low

toggle the PT3 pinpull the PT2 pin to lowset the PT1 pin to highpull the PT0 pin to low

Forced Output-Compare (2 of 2)• 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.

Page 64: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

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:

– Two 16-bit pulse accumulators PACA and PACB– One 16-bit pulse accumulator PACA and two 8-bit pulse accumulators PAC1 and

PAC0– One 16-bit pulse accumulator PACB and two 8-bit pulse accumulators PAC3 and

PAC2– Four 8-bit accumulators PAC3~PAC0 – 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.

Page 65: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Hos

t CP

U d

ata

bus

edge detector delay counterPT08-bit PAC0(PACN0)

EDG0

PA0H holdingregister

0

edge detector delay counterPT18-bit PAC1(PACN1)

EDG1

PA1H holdingregister

0

edge detector delay counterPT28-bit PAC2(PACN2)

EDG2

PA2H holdingregister

0

edge detector delay counterPT38-bit PAC3(PACN3)

EDG3

PA3H holdingregister

0

interrupt

interrupt

Load holding register and reset pulse accumulator

Figure 8.25 Block diagram of four 8-bit pulse accumulators

Page 66: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Inte

rmod

ule

Bus

4:1 MuxCLK1CLK0

prescaled clockfrom timer

timer clock (TIMCLK)

8-bit PAC3(PACN3)

8-bit PAC2(PACN2)

PA

CL

K ÷

655

36

PA

CL

K ÷

256

MUX

edgedetector PT7

÷ 64 M clock

clock select(PAMOD)

interrupt

PACA

8-bit PAC1(PACN1)

8-bit PAC0(PACN0)

PACB

interrupt

delay counter

edge detector PT0

Figure 8.26 16-bit Pulse accumulator block diagram

PAC

LK

Page 67: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Pulse Accumulator Operation Modes• 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.

Page 68: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Interrupt Sources for 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.

Page 69: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Registers Related to Pulse Accumulators

• 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.

Page 70: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

0 PAEN PAMOD PEDGE CLK1 CLK0 PAOVI PAI

Figure 8.27 Pulse accumulator control register (PACTL)

PAEN -- Pulse accumulator system enable bit 0 = PACA is disabled (PACN3 and PACN2 can be enabled) 1 = PACA is enabled (PACN3 and PACN2 cannot be enabled)PAMOD -- Pulse accumulator mode bit 0 = event counter mode 1 = gated time accumulation modePEDGE -- Pulse accumulator edge control bit For PAMOD = 0 (event counter mode) 0 = falling edges on the PAI pin cause the count to increment 1 = rising edges on the PAI pin cause the count to decrement For PAMOD = 1 (gated time accumulation mode) 0 = PAI pin high enables E÷64 clock to pulse accumulator and

the trailing falling edge on the PAI pin sets the PAIF flag 1 = PAI pin low enables E÷64 clock to pulse accumulator and

the trailing rising edge on the PAI pin sets the PAIF flagCLK1 and CLK0 -- Clock select bits 00 = use timer prescaler clock as timer counter clock 01 = use PACLK as input to timer counter (TCNT) clock 10 = use PACLK/256 as timer counter clock 11 = use PACLK/65536 as timer counter clockPAOVI -- Pulse accumulator overflow interrupt enable bit 0 = disable 1 = enablePAI -- PAI pin interrupt enable bit 0 = disabled 1 = enabled

Page 71: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

0 0 0 0 0 0 PAOVF PAIF

Figure 8.28 Pulse accumulator flag register (PAFLG)

PAOVF -- pulse accumulator overflow flag This flag is set when PACNT overflows from $FFFF to $0000 and can be cleared by writing a 1 to it.PAIF -- PT7 pin edge flag When in event counter mode, this bit is set when the selected edge on the PT7 pin is detected. When in gated accumulator mode, the selected trailing edge sets this flag.

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

0 PA3EN PA2EN PA1EN0 0 0 PA0EN

Figure 8.30 Input control pulse accumulator control register (ICPACR)

PAxEN -- 8-bit pulse accumulator x enable bit 0 = pulse accumulator x disabled 1 = pulse accumulator x enabled

Page 72: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

0 0 0 PBOVI0 PBEN 0 0

(a) Pulse accumulator B control register (PBCTL)

PBEN -- Pulse accumulator B system enable bit 0 = 16-bit pulse accumulator disabled. Eight-bit PAC1 and PAC0 can be enabled when their related enable bits in ICPACR are set. 1 = pulse accumulator B system enabled.PBOVI -- Pulse accumulator B overflow interrupt enable bit 0 = interrupt inhibited. 1 = interrupt requested if PBOVF is set

7 6 5 4 3 2 1 0

0 0 0 0 0 0 0 0

0 0 0 PBOVF0 0 0 0

reset:

PBOVF -- Pulse accumulator B overflow flagThis bit is set when the 16-bit pulse accumulator B overflows from$FFFF to $0000 or when 8-bit accumulator 1 (PAC1) overflows from$FF to $00. It is clearded by writing 1 to it or by accessing PACN1and PACN0 when the TFFCA bit in the TSCR register is set.

(b) Pulse accumulator B flag register (PBFLG)

Figure 8.29 Pulse accumulator B control and flag register

Page 73: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

SH04 TFMOD PACMX BUFENSH37 SH26 SH15 LATQ

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)

Page 74: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"N equ 1350

org $1500lds #$1500 ; set up stack pointermovw #paov_isr,UserPAccOvf ; set up PAOV interrupt vectorldd #N ; place the 2s complement in PACNTcoma ; “comb ; “addd #1 ; “std PACNT ; “movb #$52,PACTL ; enable PACA, event counting mode, active

; edge is risingcli ; enable PAOV interrupt

; …swi

paov_isr movb #PAOVF,PAFLG ; clear the PAOVF flagend

• 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:

Page 75: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\egnu091\include\hcs12.h”#include “c:\egnu091\include\vectors12.h”#define N 1252#define INTERRUPT __attribute__((interrupt))void INTERRUPT paov_isr (void);void main(void){ UserPAccOvf = (unsigned short)&paov_isr;

PACNT = ~N + 1; /* load the 2’s complement of N into PACNT */PACTL = 0x52;asm("cli"); /* enable PAOV interrupt */....

}void INTERRUPT paov_isr(void){ PAFLG = PAOVF;}

C Language Version of the Program

Page 76: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Procedure for Measuring Signal Frequency Using the PA Function

• 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-second 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 frequency of the unknown signal is given by the following equation:

frequency = paov_cnt × 216 + PACNT • The assembly and C programs for measuring signal frequency is in the following

pages:

Page 77: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"org $1000

oc_cnt rmb 1paov_cnt rmb 2 ; use to keep track of PACNT overflow countfrequency rmb 4 ; hold the signal frequency

org $1500lds #$1500movw #paov_isr,UserPAccOvf ; set up PAOV interrupt vectormovb #50,oc_cnt ; prepare to perform 50 OC0 actionsldd #0std PACNT ; let PACNT count up from 0std paov_cnt ; initialize PACNT overflow count to 0std frequency ; initialize frequency to 0std frequency+2 ; "movb #$90,TSCR1 ; enable TCNT and fast timer flag clearbset TIOS,OC0 ; select OC0 functionmovb #$03,TSCR2 ; set prescaler to TCNT to 8bclr 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,PACTLcli ; enable PAOV interruptldd TCNT

Page 78: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

sec_loop addd #60000std TC0brclr TFLG1,C0F,* ; wait for 20 ms hereldd TC0dec oc_cntbne sec_loopmovb #0,PACTL ; disable PA functionsei ; disable interruptldd PACNTstd frequency+2ldd paov_cntstd frequencyswi

paov_isr movb #PAOVF,PAFLG ; clear the PAOVF flagldx paov_cnt ; increment PACNT overflowinx ; count by 1stx paov_cnt ; "end

Page 79: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\egnu091\include\hcs12.h”#include “c:\egnu091\include\vectors12.h”#define INTERRUPT __attribute__((interrupt))unsigned long int frequency;unsigned int paov_cnt;void INTERRUPT paov_isr (void);void main (void){ int oc_cnt; UserPAccOvf = (unsigned short)&paov_isr;

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;

C Program for Measuring the Frequency Using the PACA Function

Page 80: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

while (oc_cnt) { while(!(TFLG1 & C0F)); TC0 = TC0 + 60000u; oc_cnt --;

}PACTL = 0x00; /* disable PA function */asm("sei"); /* disable interrupt */frequency = (long)paov_cnt * 65536l + (long)PACNT;asm("swi");

}void INTERRUPT paov_isr (void){ PAFLG = PAOVF; /* clear PAOVF flag */

paov_cnt = paov_cnt + 1;}

Page 81: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Using the PA Function to Measure Pulse Duration

• 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 TACNT 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.

• To measure long pulse, we need to keep track of PA overflow:pulse_width = [(216 × paov_cnt) + PACNT] × 64TE

• 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:

Page 82: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"org $1000

paov_cnt ds.b 1 ; use to keep track of the PACNT overflow countpulse_width ds.b 3 ; hold the pulse width

org $1500movw #paov_isr,UserPAccOvf ; set up PAOV interrupt vectorldd #0std PACNT ; let PACNT count up from 0clr paov_cnt ; initialize PACNT overflow count to 0movb #$0,TSCR2 ; set TCNT timer prescaler to 1movb #$72,PACTLbclr DDRT,$80 ; configure PAI pin for inputcli ; enable PAOV interruptbrclr PAFLG,PAIF,* ; wait for the arrival of the falling edge of PAI movb #0,PACTL ; disable PA functionsei ; disable interruptldd PACNTstd pulse_width+1ldaa paov_cntstaa pulse_widthswi

Page 83: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

paovISR movb #PAOVF,PAFLGinc paov_cntrtiend

Page 84: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Modulus Down-Counter• Can generate periodic interrupts• Can be used to latch the value of IC registers and the pulse

accumulators to their holding registers.• The action of latching can be periodic or only once.• The clock input (E clock) to the modulus down counter is prescaled

by 1, 4, 8, or 16.• The operation of the modulus down counter is controlled by the

MCCTL register and the status of its operation is recorded in the MCFLG register.

• The modulus down counter MCCNT is 16-bit.• The MCCNT register has a 16-bit load register, which will be

reloaded into MCCNT when it decrements to 0.• When writing a value into MCCNT, the value is also written into the

load register.

Page 85: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

ICLAT FLMC MCEN MCPR1MCZI MODMC RDMCL MCPR0

Figure 8.32 Modulus down-counter control register (MCCTL)

MCZI -- modulus counter underflow interrupt enable bit 0 = modulus counter underflow interrupt is disabled 1 = modulus counter underflow interrupt is enabledMODMC -- modulus mode enable bit 0 = the counter counts once from the value written to it and will stop at $0000. 1 = modulus mode is enabled. When the counter reaches $0000, the counter is loaded with the latest value written into to the modulus count register.RDMCL -- read modulus down-counter load bit 0 = reads of the modulus count register will return the present value of the count register. 1 = reads of the modulus count register will return the contents of the load register (i.e., the reload value is returned)ICLAT -- input capture force latch action bit This bit has effect only when both the LATQ and BUFEN bits in ICSYS are set. 0 = no effect 1 = forces the contents of the input capture registers TC0 to TC3 and their corresponding 8-bit pulse accumulators to be latched into the associated holding registers. The pulse accumulators will be cleared when the latch action occurs.FLMC -- force load register into the modulus counter count register bit This bit has effect only when MCEN = 1. 0 = no effect 1 = loads the load register into the modulus counter count register. This also resets the modulus counter prescaler.MCEN -- modulus down-counter enable bit 0 = modulus counter is disabled and preset to $FFFF 1 = modulus counter is enabledMCPR1 & MCPR0 -- modulus counter prescaler select bits 0 0 = prescale rate is 1 0 1 = prescale rate is 4 1 0 = prescale rate is 8 1 1 = prescale rate is 16

Page 86: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

0 POLF3 POLF2 POLF1MCZF 0 0 POLF0

Figure 8.33 Modulus down-counter flag register (MCFLG)

MCZF -- modulus counter underflow interrupt flag This flag is set when the modulus down-counter reaches 0. Writing 1 to this bit clears the flag.POLF3-POLF0 -- first input capture polarity status bits These are read-only bits. Writing to these bits has no effect. Each status bit gives the polarity of the first edge which has caused an input capture to occur after capture latch has been read. 0 = The first input capture has been caused by a falling edge. 1 = The first input capture has been caused by a rising edge.

Page 87: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

movb #$C7,MCCTLmovw #15000,MCCNT ; place the value that will be decremented to

; to 0 in 10 mscli ; enable interrupt

• 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 $C0 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:

Page 88: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

delayby10msbset TSCR1,TFFCA ; enable timer fast flag clearmovb #$07,MCCTL ; enable modulus down counter with 1:16 as

prescalermovw #15000,MCCNT ; load the value to be down countedbrclr MCFLG,MCZF,*bclr MCCTL,$04 ; disable modulus down counterdbne y,delay10msrts

• 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.

Using Modulus Down Counter to Generate Time Delay

• It is most convenient to use the non-modulus mode to create time delay.• 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:

Page 89: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

NOVW4 NOVW3 NOVW2 NOVW1NOVW7 NOVW6 NOVW5 NOVW0

NOVWn: No input capture overwrite 0 = The contents of the related capture register or holding register can be overwritten when a new input capture or latch occurs. 1 = The related capture register or holding register cannot be written by an event unless they are empty. This will prevent the captured value to be overwritten until it is read or latched in the holding register.

Figure 8.34 Input control overwrite register (ICOVW)

Enhanced Capture Timer (ECT)• Has all the features provided in the standard timer module• Has one 16-bit holding register for each of the four input-capture

(IC) channels IC3…IC0• The holding register option is controlled by the ICOVW register.• Has four 8-bit pulse accumulators• Has a 16-bit modulus down counter• Has four user selectable delay counters for increasing input noise

immunity

Page 90: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Why Enhanced Capture Timer?

• Suitable for high-frequency operation (MCU does not need to read the first edge before the second edge arrives).

• Simplifies software for signal measurement• Avoids capture values to be overwritten before

they have been read

Page 91: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Operation of the Enhanced Capture Timer

• A holding register is empty if its value has been read.• The ECT module can be configured in latch mode or queue mode.• In latch mode, latching is done when the modulus down counter

reaches 0 or a zero is written into MCCNT.• During the latching event, the contents of IC registers and the 8-bit

pulse accumulators are transferred to their holding registers. After this, the pulse accumulators are cleared.

• In queue mode, the main timer value is copied into the TCx register by a valid input pin transition. With a new capture, the contents of the TCx register will be transferred to its holding register.

• The ECT module block diagram in latch mode and queue mode are shown in Figure 8.35 and 8.36, respectively.

Page 92: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Prescaler 16-bit free-runningmain timer

pin logic Delaycounter

comparator

TCx capture/compareregister

TCxH hold register

EDG x

Prescalerbus clock

÷1, 4, 8, 16÷1,2,...,128 16-bit load register

16-bit modulusdown counter

ICLAT, LATQ, BUFEN(force latch)

write $0000 tomodulus counter

LATQ(MDC latch enable)

Lat

ch

Figure 8.35 Enhanced Input capture function block diagram in latch mode

to other IC channels

bus clock

PTxone IC channel

(IC0..IC3)

pin logicPTicomparator

TCx capture/compareregisterMUX

EDG i

EDG j

j = 8 - i

one IC channel(IC4..IC7)

Page 93: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Prescaler 16-bit free-runningmain timer

pin logicDelay

counter

comparator

TCx capture/compareregister

TCxH hold register

EDG x

Prescalerbus clock

÷1, 4, 8, 16÷1,2,...,128 16-bit load register

16-bit modulusdown counter

Figure 8.36 Enhanced Input capture function block diagram in Queue mode (channels IC0..IC3 block diagram)

to other IC channels

bus clock

PTxone IC channel

(IC0..IC3)

pin logicPTicomparator

TCx capture/compareregisterMUX

EDG i

EDG j

j = 8 - i

one IC channel(IC4..IC7)

Page 94: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

reset: 0 0 0 0 0 0 0 0

0 0 0 DLY10 0 0 DLY0

Figure 8.37 Delay counter control register (DLYCT)

DLYx -- delay counter select bits 0 0 -- disabled (bypassed) 0 1 -- 256 P clock cycles 1 0 -- 512 P clock cycles 1 1 -- 1024 P clock cycles

True Edge Detection• The ECT module uses a delay counter to distinguish true edge and

false.• A transition is determined to be a true edge if the transition is longer

than the preset duration.• The duration used to determine the true edge is controlled by the

DLYCT register.

Page 95: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"org $1000

period ds.w 1 ; memory to store the periodorg $1500movb #$90,TSCR ; enable timer counter and fast timer flag clearbclr TIOS,IOS0 ; enable input-capture 0movb #$04,TSCR2 ; disable TCNT overflow interrupt, set prescale

; factor to 16movb #$01,TCTL4 ; choose to capture the rising edge of PT0 pinmovb #$0A,ICSYS ; enable timer flag-setting, IC buffer, and queue modeclr DLYCT ; disable delay counterbset ICOVW,NOVW0 ; no input-capture overwrite for IC0ldd TC0 ; empty the input-capture register TC0ldd TC0H ; empty the holding register TC0Hbrclr TFLG1,$FE,* ; wait for the arrival of the second rising edgeldd TC0subd TC0H ; subtract the first edge from the second edgestd periodswiend

• Example 8.19 Write a program to measure the period of an unknown signal connected to the PT0 pin using the enhanced capture feature of ECT.

• Solution:

Page 96: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\egnu091\include\hcs12.h”void main(void){ unsigned int period; TSCR1 = 0x90; /* enable timer counter, enable fast timer flag clear*/ TIOS &= ~IOS0; /* enable input-capture 0 */ TSCR2 = 0x04; /* set prescale factor to 16 */ TCTL4 = 0x01; /* capture the rising edge of PT0 pin *//* enable timer flag-setting mode, IC buffer, and queue mode */ ICSYS = 0x0A; DLYCT = 0x00; /* disable delay counter */ ICOVW |= NOVW0; /* disable input-capture overwrite */ period = TC0; /* empty TC0 and clear the C0F flag */ period = TC0H; /* empty the TC0H register *//* wait for the arrival of the second rising edge */ while (!(TFLG1 & C0F)); period = TC0 - TC0H; asm ("swi");}

Page 97: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include "c:\miniide\hcs12.inc"org $1000

edge1 rmb 2overflow rmb 2pulse_width rmb 2

org $1500movw #tov_isr,UserTimerOvf ; set up timer overflow interrupt vectorldd #0std overflowmovb #$90,TSCR1 ; enable TCNT and fast timer flag clearmovb #$04,TSCR2 ; set prescaler to TCNT to 16bclr TIOS,IOS0 ; enable input-capture 0movb #$01,DLYCT ; set delay count to 256 E cyclesmovb #$01,ICOVW ; prohibit overwrite to TC0 registermovb #$0,ICSYS ; disable queue modemovb #$01,TCTL4 ; capture the rising edge on PT0 pin

• Example 8.20 Write a program to measure the pulse width of a signal connected to the PT0 pin in a noisy environment. Ignore any noise pulse shorter than 256 P clock cycles.

• Solution:

Page 98: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

movb #C0F,TFLG1 ; clear C0F flagbrclr TFLG1,C0F,* ; wait for the arrival of the first rising edgemovb #TOF,TFLG2 ; clear the TOF flagbset TSCR2,TOI ; enable TCNT overflow interruptcli ; "movw TC0,edge1 ; clear C0F flag and save the captured first edgemovb #$02,TCTL4 ; capture the falling edge on PT0 pin brclr TFLG1,C0F,* ; wait for the arrival of the falling edgeldd TC0subd edge1std pulse_widthbcc next

; second edge is smaller, so decrement overflow count by 1ldx overflowdexstx overflow

next switov_isr movb #TOF,TFLG2 ; clear TOF flag

ldx overflow ; increment TCNT overflow countinx ; "stx overflow ; "rtiend

Page 99: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Pulse Width Modulation (PWM)• Many applications require the generation of digital waveform.• Output compare function can be used to generate digital waveform but incur

too much overhead.• Pulse width modulation requires only the initial setup of period and duty

cycle for generating the digital waveform.• The MC9S12DP256 has an 8-channel PWM module.• Each PWM channel has a period register, a duty cycle register, a control

register, and a dedicated counter.• The clock input to PWM is programmable through a two-stage circuitry.• There are four possible clock sources for the PWM module: clock A, clock

SA, clock B, and clock SB.• Clock SA is derived by dividing the clock A by an even number ranging from

2 to 512.• Clock SB is derived by dividing the clock B by an even number ranging from

2 to 512.• Clock A and clock B are derived by dividing the E clock by a power of 2. The

power can range from 0 to 7.

Page 100: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Channel 7

Period and duty Counter

PWM Channels

Channel 6

Period and duty Counter

Channel 5

Period and duty Counter

Channel 4

Period and duty Counter

Channel 3

Period and duty Counter

Channel 2

Period and duty Counter

Channel 1

Period and duty Counter

Channel 0

Period and duty Counter

Clock select

Control

PWMclock

PWM Module

Enable

Polarity

Alignment

Bus clock

PWM7

PWM6

PWM5

PWM4

PWM3

PWM2

PWM1

PWM0

Figure 8.38 HCS12 PWM block diagram

Page 101: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

0 PCKB2 PCKB1 PCKB0 0 PCKA2 PCKA1 PCKA0

reset: 0 00 0 0 0 0 0

Table 8.3 Clock B prescaler selects

PCKB2 PCKB1 PCKB0value of clock

B

00001111

00110011

01010101

E clockE clock/ 2E clock/ 4E clock/ 8E clock/ 16E clock/ 32E clock/ 64E clock/ 128

Table 8.4 Clock A prescaler selects

PCKA2 PCKA1 PCKA0value of clock

A

00001111

00110011

01010101

E clockE clock/ 2E clock/ 4E clock/ 8E clock/ 16E clock/ 32E clock/ 64E clock/ 128

Figure 8.41 PWM prescale clock select register (PWMPRCLK)

PWM Clock Generation• 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.

Page 102: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

PCLK7 PCLK6 PCLK5 PCLK4 PCLK3 PCLK2 PCLK1 PCLK0

reset: 0 00 0 0 0 0 0

Figure 8.42 PWM clock select register (PWMCLK)

PCLKx: PWM channel x clock select (x = 7, 6, 3, 2) 0 = clock B as the clock source 1 = clock SB as the clock sourcePCLKy: PWM channel y clock select (y = 5, 4, 1, 0) 0 = clock A as the clock source 1 = clock SA as the clock source

PWM Channel Timers• 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.

Page 103: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

PPOL7 PPOL6 PPOL5 PPOL4 PPOL3 PPOL2 PPOL1 PPOL0

reset: 0 00 0 0 0 0 0

Figure 8.43 PWM polarity register (PWMPOL)

PPOLx: PWM channel x polarity 0 = PWM channel x output is low at the start of a period, then goes high when the duty count is reached. 1 = PWM channel x output is high at the start of a period, then goes low when the duty count is reached.

7 6 5 4 3 2 1 0

PWME7 PWME6 PWME5 PWME4 PWME3 PWME2 PWME1 PWME0

reset: 0 00 0 0 0 0 0

Figure 8.45 PWM enable register (PWME)

PWMEx: PWM channel x enable 0 = PWM channel x disabled. 1 = PWM channel x enabled.

Page 104: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

GATE8-bit counter

PWMCNTx

8-bit compare=

PWMDTYx

clocksource

8-bit compare=

PWMPERx

T Q

QR

MUX

T

R

CAExQ

Q

MUX

to pindriver

PPOLx

From port PTPdata register(clock edge sync)

PWMEx

up/down re

set

Figure 8.44 PWM channel block diagram

Page 105: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

CAE7 CAE6 CAE5 CAE4 CAE3 CAE2 CAE1 CAE0

reset: 0 00 0 0 0 0 0

Figure 8.46 PWM center align enable register (PWMCAE)

CAEx: Center aligned enable bit for channel x 0 = PWM channel x output is left aligned 1 = PWM channel x output is center alighed

PWM Waveform Alignment

• PWM output waveform can be left-aligned or center-aligned.

• The choice of alignment is controlled by the PWMCAE register.

Page 106: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Left-Aligned Output• The PWMCNTx counter is configured as a count-up counter.

– PWMx frequency = Clock(A, B, SA, SB frequency) PWMPERx

– Polarity = 0

– PWMx duty cycle = [(PWMPERx – PWMDTYx) PWMPERx] 100%

– Polarity = 1

– PWMx duty cycle = [PWMDTYx PWMPERx] 100%

PWMDTYx

Period = PWMPERx

PPOLx = 0

PPOLx = 1

Figure 8.47 PWM left-aligned output waveform

Page 107: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

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.

Page 108: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

PWMx frequency = Clock (A, B, SA, or SB) frequency (2 PWMPERx)

When polarity = 0,PWMx duty cycle = [(PWMPERx – PWMDTYx) PWMPERx] 100%

When polarity = 1, PWMx duty cycle = [PWMDTYx PWMPERx] 100%

PPOLx = 0

PPOLx = 1PWMDTYx PWMDTYx

PWMPERx PWMPERx

Period = PWMPERx * 2

Figure 8.48 PWM center aligned output waveform

In Aligned Mode

Page 109: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

PWM 16-bit Mode (1 of 2)

• Two adjacent PWM channels can be concatenated into a 16-bit PWM channel.

• The concatenation of PWM channels are controlled by the PWMCTL register.

• The 16-bit PWM system is illustrated in Figure 8.49. • When channel k and k+1 are concatenated, channel k is the high-

order channel, whereas channel k+1 is the lower channel. (k is even number). A 16-bit channel outputs from the lower-order channel pin and is also enabled by the lower-order channel.

• Both left-aligned and center-aligned mode apply to the 16-bit mode.

Page 110: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

7 6 5 4 3 2 1 0

CON67 CON45 CON23 CON01 PSWAI PFRZ 0 0

reset: 0 00 0 0 0 0 0

Figure 8.40 PWM control register (PWMCTL)

CONjk: concatenate channels j and k (j = 0, 2, 4, or 6; k = j+1) 0 = channel j and k are separate 8-bit PWMs 1 = Channels j and k are concatenated to create one 16-bit PWM channel. Channel j becomes the high order byte and channel k becomes the low order byte. Channel k output pin is used as the output for this 16-bit PWM. Channel k clock select bit determines the clock source, channel k polarity bit determines the polarity, channel k enable bit enables the output and channel k center aligned enable bit determines the output mode.PSWAI: PWM stops in wait mode 0 = allow the clock to the prescaler to continue while in wait mode 1 = stop the input clock to the prescaler whenever the MCU is in wait modePFRZ: PWM counters stop in freeze mode 0 = allow PWM to continue while in freeze mode 1 = disable PWM input clock to the prescaler whenever the part is in freeze mode.

PWM 16-bit Mode (2 of 2)

Page 111: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

PWMCNT6 PWMCNT7

Period/ Duty compare PWM7

Clock source 7

PWMCNT4 PWMCNT5

Period/ Duty compare PWM5

Clock source 5

PWMCNT2 PWMCNT3

Period/ Duty compare PWM3

Clock source 3

PWMCNT0 PWMCNT1

Period/ Duty compare PWM1

Clock source 1

high

high

high

high

low

low

low

low

Figure 8.49 PWM 16-bit mode

Page 112: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\miniide\hcs12.inc”…movb #0,PWMCLK ; select clock A as the clock source for PWM0movb #1,PWMPRCLK ; set clock A prescaler to 2movb #1,PWMPOL ; channel 0 output high at the start of the periodmovb #0,PWMCAE ; select left-aligned modemovb #$0C,PWMCTL ; 8-bit mode, stop PWM in wait and freeze modemovb #120,PWMPER0 ; set period valuemovb #60,PWMDTY0 ; set duty valuemovb #0,PWMCNT0 ; reset the PWM0 counterbset PWMEN,PWME0 ; enable PWM channel 0

• 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%)

Page 113: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

movb #0,PWMCLK ; select clock A as the clock sourcemovb #1,PWMPOL ; set PWM0 output to start with high levelmovb #1,PWMPRCLK ; set the PWM0 prescaler to clock A to 2movb #1,PWMCAE ; select PWM0 center-aligned modemovb #$0C,PWMCTL ; select 8-bit mode, stop PWM in wait modemovb #120,PWMPER0 ; set period valuemovb #72,PWMDTY0 ; set duty valuebset PWME,PWME0 ; enable PWM channel 0

• Example 8.22 Write an instruction sequence to generate a square wave with 20 ms 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.

Page 114: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

movb #0,PWMCLK ; select clock A as the clock sourcemovb #2,PWMPOL ; set PWM0:PWM1 output to start with high levelmovb #4,PWMPRCLK ; set prescaler to 16movb #$1C,PWMCTL ; concatenate PWM0:PWM1, stop PWM in wait modemovb #0,PWMCAE ; select left align modemovw #30000,PWMPER0 ; set period to 30000movw #24000,PWMDTY0 ; set duty to 24000bset PWME,PWME1 ; enable PWM0:PWM1

• Example 8.23 Write an instruction sequence to generate a 50 Hz digital waveform with 80% duty cycle using the 16-bit mode from the PWM1 output pin.

• Solution: Using the following setting:– Select clock A as the clock source and set its prescaler to 16.– Select left aligned mode and select polarity 1.– Load the value 30000 into the PWMPER0:PWMPER1 register.– Load the value 24000 into the PWMDTY0:PWMDTY1 register.

Page 115: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\egnu091\include\hcs12.h”#include “c:\egnu091\include\delay.c”

• 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.

Page 116: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

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;}asm ("swi");

}

Page 117: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

DC Motor Control (1 of 4)

• 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 that uses a DC motor requires it to 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.

Page 118: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

DC Motor Control (2 of 4)

• 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.

Page 119: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

HCS12

PP7

PP3

PT0feedback

direction

speedon/off

Driver DC motor

Figure 8.52 Simplified circuit for DC motor control

DC Motor Control (3 of 4)

Page 120: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

1

2

3

4

5

6

7

8 9

10

11

12

13

14

15

16CE1

IN1

OUT1

GND

GND

OUT2

IN2

VS CE2

IN3

OUT3

GND

GND

IN4

OUT4

VSS

(a) Pin Assignment

M

M

M10

1010

10

0101 1

2

3

4

5

6

7

8

16

15

1413

12

11

10

9

VSS

VS(b) Motor connection

Figure 8.53 Motor driver L293 pin assignment and motor connection

L293L293

1

2

3

4

DC Motor Control (4 of 4)

Page 121: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

DC Motor Feedback (1 of 2)

• 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.

Page 122: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

t

T/2T is the time for one revolution

Magnets

Hall-effecttransistor

Figure 8.54 The output waveform of the Hall effect transistor

DC Motor Feedback (2 of 2)

Page 123: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

A HCS12-based DC Motor Control System (1 of 4)

• Schematic is shown in 8.55.• The PWM output from the PP3 pin is connected to one end of the

motor whereas the PP7 pin is connected to the other end of the motor.

• The circuit is connected so that the motor will rotate clockwise when the voltage of the PP7 pin is 0 while the PWM output is nonzero (positive).

• The direction of motor rotation is illustrated in Figure 8.56. • By applying appropriate voltages on PP7 and PP3 (PWM3), the

motor can rotate clockwise, counterclockwise, or even stop.

Page 124: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

A HCS12-based DC Motor Control System (2 of 4)

• Input capture channel 0 is used to capture the feedback from the Hall-effect transistor.

• When a DC motor is first powered, it takes time for the motor to reach its final speed.

• When a load is added to the motor, it will be slowed down and hence the duty cycle of the PWM3 should be increased to keep the speed constant.

• When the load is reduced, the speed of the motor will be increased and hence the duty cycle of the PWM3 should be decreased.

• A DC motor does not respond to the change of the duty cycle instantaneously. Several cycles must be allowed for the microcontroller to determine if the change of the duty cycle has achieved its effect.

Page 125: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

VCC

PWM3PP7

4591011

NC

10K

VCC

1827

VCC

163

6.8F

614

151312

0.33 F

6.8F

M

VCC

30137

3 2

1

Hall-effectswitch

All diodes are the same and could be any one of the 1N4000 series

Figure 8.55 Schematic of a HCS12-based motor-control system

PT0

HCS12

L293

A HCS12-based DC Motor Control System (3 of 4)

Page 126: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

Motor

L293

A

B

PWM (PP3)

Port Pin (PP7)

clockwise

off off

counterclockwise

A

B

When A = B, torqueapplied to motor = 0

When A B, motor runs

Figure 8.56 The L293 motor driver

A HCS12-based DC Motor Control System (4 of 4)

Page 127: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

• 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 Speed = 60 × 106 ÷ (2 × diff)

Page 128: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

#include “c:\egnu091\include\hcs12.h”

unsigned int motor_speed (void){ unsigned int edge1, diff, rpm;

long int temp;

TSCR1 = 0x90; /* enable TCNT and fast flag clear */TIOS &= IOS0; /* select IC0 function */TSCR2 = 4; /* set TCNT prescale factor to 16 */TCTL4 = 0x01; /* select to capture the rising edge of PT0 */ TFLG1 = C0F; /* cleared C0F flag */while (!(TFLG1 & C0F)); /* wait for the first edge */edge1 = TC0;while (!(TFLG1 & C0F)); /* wait for the second edge */diff = TC0 - edge1;temp = 1000000l / (long)(2 * diff);rpm = temp * 60;return rpm;

}

Page 129: Chapter 8 HCS12 Timer Functions. Why are Timer Functions Important? It is very difficult and impossible to implement the following applications without

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.