The 68HC11 MicrocontrollerChapter 8: 68HC11 Timer Functions The 68HC11 Microcont rollerHan-Way Huang Minnesota State University, Mankato The 68HC11 MicrocontrollerApplications that Requires a Dedicated Timer System - time dela y creat ion and measure ment - peri od meas ureme nt - eve nt count ing - time-o f-day trac king - periodic interrupt gener ation to remind the p rocessor to performroutine tasks - wav eform gener ation - etc. A Summary of the 68HC11 Timer Functions 1. Main time r • 16-bit non- stop timer• read-o nly after reset 2. Input capture functi on • thr ee c han nels --1 to 3 • each c hannel has a 16-b it latch, edge-detection logic, flag bit, and interrupt logic • will load the c urrent main timer value into the input capture register w hen the selected signal edge is detected • can be used to measure the signal frequency, period , and pulse width and as time referenc e The 68HC11 Microcontroller3. Output compar e functions • A (E) series members have five (fou r/five) ch annels (OC1…O C5) • each ch annel has a 16-bit compa rator, 16-bit register, action pin, interrupt request circ uit, forced-compare function • continuously compare the value of the 16-bit compa re register with that of the main timerand may optionally trigger an action on a pin, generate an interrupt • is often used to c reate a time delay and generate a w aveform 4. Real-time interrup t • generates periodic interrupts when enabled • interr upt perio d is progr amma ble 5. Computer oper ating proper ly (COP) discussed in Chapter 6 6. Pulse accumulato r • has an 8 -bit c ounte r• has two op era tion mo des • can be u sed to measure events, frequency, o r the duration of a pulse width The 68HC11 MicrocontrollerThe Free-Running Main Timer (TCNT) - The main timer is clear ed to 0 o n reset and is rea d-only except in tes t mode. - The timer counter reg ister is meant to be rea d by a 1 6-bit read instruction su ch as LDD orLDX. - The block diagram is shown in Figure 8.1. MCU E clockPrescalerdivide by 1, 4, 8, or 16 TCNT(H) TCNT(L ) 16-bit free-running counterTaps for RTI, COP watchdog, and pulse accumulatorTOF TOI Interrupt request 16-bit timer bus Figure 8.1 68HC11 main timer system
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
8/3/2019 Timers Huang
http://slidepdf.com/reader/full/timers-huang 1/21
The 68HC11 Microcontroller
Chapter 8: 68HC11 Timer Functions
The 68HC11 Microcontroller
Han-Way Huang
Minnesota State University, Mankato
The 68HC11 Microcontroller
Applications that Requires a Dedicated Timer System
- time delay creation and measurement
- period measurement
- event counting
- time-of-day tracking
- periodic interrupt generation to remind the processor to perform routine tasks
- waveform generation
- etc.
A Summary of the 68HC11 Timer Functions
1. Main timer
• 16-bit non-stop timer
• read-only after reset
2. Input capture function
• three channels --1 to 3
• each channel has a 16-bit latch, edge-detection logic, flag bit, and interrupt logic
• will load the current main timer value into the input capture register when the selected
signal edge is detected
• can be used to measure the signal frequency, period, and pulse width and as time reference
The 68HC11 Microcontroller
3. Output compare functions
• A (E) series members have five (four/five) channels (OC1…OC5)
• each channel has a 16-bit comparator, 16-bit register, action pin, interrupt request circuit,
forced-compare function• continuously compare the value of the 16-bit compare register with that of the main timer
and may optionally trigger an action on a pin, generate an interrupt
• is often used to create a time delay and generate a waveform
4. Real-time interrupt
• generates periodic interrupts when enabled
• interrupt period is programmable
5. Computer operating properly (COP)
discussed in Chapter 6
6. Pulse accumulator
• has an 8-bit counter
• has two operation modes
• can be used to measure events, frequency, or the duration of a pulse width
The 68HC11 Microcontroller
The Free-Running Main Timer (TCNT)
- The main timer is cleared to 0 on reset and is read-only except in test mode.
- The timer counter register is meant to be read by a 16-bit read instruction such as LDD or
LDX.
- The block diagram is shown in Figure 8.1.
MCU
E clock
Prescaler
divide by
1, 4, 8, or 16
TCNT(H) TCNT(L)
16-bit free-running
counter
Taps for RTI, COPwatchdog, and
pulse accumulator
TOF
TOIInterrupt
request
16-bit timer bus
Figure 8.1 68HC11 main timer system
8/3/2019 Timers Huang
http://slidepdf.com/reader/full/timers-huang 2/21
The 68HC11 Microcontroller
Registers related to Main Timer
1. Timer counter: TCNT
2. Timer flag register 2: TFLG2
3. Timer mask register 2: TMSK2
- Those bits related to main timer operation in TFLG2 and TMSK2 are in boldface.
- Timer counter is meant to be read using a double-byte read instruction such as LDD or LDX.
If the user accesses TCNT with two 8-bit reads, the result might not be correct, because the
lower byte of TCNT would be incremented when the upper byte is accessed.
TOI RTII PAOII PAII 0 0 PR1 PR0
7 6 5 4 3 2 1 0TMSK2
at $1024value after
reset 0 0 0 0 0 0 0 0
TOF RTIF PAOVF PAIF
7 6 5 4 3 2 1 0TFLG2
at $1025value after
reset 0 0 0 0 0 0 0 0
Figure 8.2 TMSK2 and TFLG2 registers
The 68HC11 Microcontroller
The prescale factor for the main timer is selected by bits 1 and 0 of the timer mask register 2
as shown in Table 8.1.
Example 7.1 What values will be in A and B after execution of the following three instructions
if TCNT contains $5EFE when the upper byte is accessed ? Assume the bits PR1 and PR0 of
TMSK2 are 00.
regbas equ $1000
TCNTH equ $0E
TCNTL equ $0F
ldx #regbas
ldaa TCNTH,X ; read the upper byte of TCNT
ldaa TCNTL,X ; read the lower byte of TCNT
PR1 PR0
Prescalefactor
Overflow period
2 MHzE clock
1 MHzE clock
0011
0101
148
16
32.77 ms131.1 ms262.1.ms524.3 ms
65.54 ms262.1 ms524.3 ms1.049 ms
Table 8.1 Main timer clock frequency vs. PR1 and PR0
The 68HC11 Microcontroller
Solution:
- The main timer prescale factor is 1 and hence the E clock is the clock input to TCNT.
- The instruction LDAA TCNTH,X loads the upper byte (value is $5E) of TCNT into A.
- The instruction LDAB TCNTL,L takes 4 E clock cycles to execute. T herefore, TCNT
will have been incremented by 4 to $5F02. The accumulator B will receive the value $02.
This is not what we expect. If the instruction LDD TCNT,X is executed, then A and B
will contain $5E and $FE respectively.
The 68HC11 Microcontroller
Input Capture Functions
- 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 arrives.
- The 68HC11 has three input capture channels (IC1, IC2, & IC3) to implement this operation.
- Each input capture channel has a 16-bit input capture register, a flag, edge-detectionlogic, and interrupt request circuit.
Rising edge Falling edge
or
Figure 8.3 Events represented by signal edges
16-bit latch
TICx
ICxFEdge-detection
logic
16-bit timer
busICx pin
ICxI Interrupt
request
Figure 8.4 Input-capture function block diagram
8/3/2019 Timers Huang
http://slidepdf.com/reader/full/timers-huang 3/21
The 68HC11 Microcontroller
- The edge to be captured is selected by programming the register TCTL2.
For example, the following instruction sequence captures the rising edge of the signal applied
at PA0 (IC3):
r egb as e qu $1000
TCTL2 equ $21 ; offset of TCTL2 from regbas
ldx #regbas
bclr TCTL2,X %00000010 ; clear bit 1 to 0
bset TCTL2,X %00000001 ; set bit 0 to 1
E DG1B E DG1A E DG2B E DG2A E DG3B E DG3A
0 00 0 0 0
TCTL2
at $1021value after
resetEDGxB EDGxA
0 0 capture disabled
0 1 capture on rising edge
1 0 capture on falling edge
1 1 capture on both edges
x = 1,...,3
Figure 8.5 Contents of TCTL2
The 68HC11 Microcontroller
Registers related to input capture
- the lowest three bits (bits 2 to 0) of this register are input capture flags
- the arrival of a signal edge will set one of the input capture flags
- the upper five bits (bits 7 to 3) of this register are output compare flags
1. timer mask register 1 (TMSK1):
7 6 5 4 3 2 1 0
OC1I OC2I OC3I OC4I OC5I IC1I IC2I IC3I
0 0 0 0 0 0 0 0
2. timer flag register 1 (TFLG1):
- the lowest three bits (bits 2 to 0) of this register enable/disable the interrupt from the
proper input capture channel
- the upper five bits (bits 7 to 3) of this register enable/disable the interrupt from the
corresponding output compare channels
OC1F OC2F OC3F OC4F OC5F IC1F IC2F IC3F
7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0
TFLG1at $1023
value after
reset
Figure 8.6 The contents of the TFLG1 register
The 68HC11 Microcontroller
How to clear a timer flag bit?
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,X $FE
will clear the IC3F f lag. (Assume the index register contains $1000 and TFLG1 = $23)
Method 2. load an accumulator with a mask that has a 1 (or 1s) in the bit (s) corresponding
to the flag (s) to be cleared; then write this value to TFLG1 or TFLG2. For example,
LDAA #$01
STAA TFLG1,X
will clear the IC3F flag.
The 68HC11 Microcontroller
Applications of Input Capture function
- Event arrival time recording
- Period measurement: the input capture function captures the main timer values
corresponding to two consecutive rising or falling edges
- Pulse width measurement: capture the rising and falling edges
one period
(a) Capture two rising edges
one period
(b) Capture two falling edges
Figure 8.8 Period measurement by capturing two consecutive edges
Pulse width
Rising edge Falling edge
Figure 8.9 Pulse-width measurement using input capture
8/3/2019 Timers Huang
http://slidepdf.com/reader/full/timers-huang 4/21
The 68HC11 Microcontroller
- Interrupt generation: three input capture functions can be used as three edge-sensitive
interrupt sources.
- Event counting: by counting the number of signal edges arrived during a period
- Time reference: often used in combination with an output comparefunction
Start of interval
End of interval
e1
e2e
3 e4 ei
e j
Figure 8.10 Using an input-capture function for event counting
... ...
Time t0
Time of reference
(set up by signal edge)
Time t0
+ delay
Time to activate
output signal(set up by output compare)
Figure 8.11 A time reference application
The 68HC11 Microcontroller
Duty Cycle Measurement
∆T
T
duty cycle =∆T
T* 100%
Figure 8.12 Definition of duty cycle
Phase Difference Measurement
∆T
T
signalS1
signalS2
phase difference =∆T
T* 360o
Figure 8.13 Phase difference definition for two signals
The 68HC11 Microcontroller
Example 8.3 Use the input capture channel IC1 t o measure the period of an unknown
signal. The period is known to be shorter than 32 ms. Write a program to set up IC1
to measure its period.
Solution:
- two versions are available.
- The polling method is shown
in Figure 8.15.
Start
Choose to capture the rising edge
Clear the IC1F flag
IC1F = 1?
Save the captured value
of the first edge.
Clear the IC1F flag.
IC1F = 1?
yes
yes
no
no
Take the difference
of the second and
the first edges.
Stop
Figure 8.15 Logic flow of the period-measurement program (polling method)
The 68HC11 Microcontroller
Assembly Program for Period Measurement (Polling Method)
REGBAS EQU $1000
TLFG1 EQU $23
TIC1 EQU $10
TCTL2 EQU $21
IC1rise EQU $10
ORG $00
edge1 RMB 2
period RMB 2
ORG $C000
LD X # RE GBAS
BCLR TFLG1,X $FB ; clear IC1F flagLDAA #IC1rise
ST AA T CT L2,X ; capture ri si ng edg e
BRCLRTFLG1,X $04 * ; wait for the first rising edge
LDD TI C1,X
STD edge1 ; save the first edge
BCLR TFLG1,X $FB ; clear IC1F flag
BRCLRTFLG1,X $04 * ; wait for the second edgeLDD TI C1,X
SUBD edge1
STD period ; save the period
END
8/3/2019 Timers Huang
http://slidepdf.com/reader/full/timers-huang 5/21
The 68HC11 Microcontroller
C Program for Period Measurement (Polling method)
#include <hc11.h>
#include <stdio.h>
main ( )
unsigned int edge1, period;
TFLG1 = 0x04; /* clear IC1F flag
T CT L2 = 0x10; /* c onfigure t o capture r is in g edg e * /while (!(TFLG1 & 0x04)); /* wait for the arrival of the first rising edge */
edge1 = TIC1; /* save the arriva l t ime of the firs t r is ing edge */
TFLG1 = 0x04;
while (!(TFLG1 & 0x04)); /* wait for the arrival of the second rising edge */
period = TIC1 –edge1;
printf(“\n The period of the signal is %d E clock cycles. \n”, period);
return 0;
The 68HC11 Microcontroller
Interrupt-driven Method for Period Measurement
Start
Select to capture therising edge of IC1
edge_cnt 2
Clear IC1F flag
Enable IC1 interrupt
edge_cnt = 0?
period = edge2 - edge1
Stop
Clear IC1F flag
Decrement edge_cnt
edge_cnt = 1?
Save the first edge
return from interrupt
I C 1
i n t e r r u p t
R e t u r n f ro m int e r r u p t yes
no
yes
no
Figure 8.16 Interrupt-driven method for period measurement
The 68HC11 Microcontroller
REGBAS EQU $1000 ; base address of the I/O register block
TFLG1 EQU $23 ; offset of TFLG1 fro m r egbas
TMS K1 EQU $22 ; offset of TM SK1 from r egbas
TIC1 EQU $10 ; offset of TIC1 from regbas
TCTL2 EQU $21 ; offset of TCT L2 fro m r egbas
IC1rise EQU $10 ; value to selec t the r is ing edge of IC1 to cap ture
IC1I EQU $04 ; mask to select the IC1 bit in TMSK1
IC1FM EQU $FB ; mask to c lear IC1F using the BCLR ins truc tion