
M68HC11
REFERENCE MANUAL
MAIN TIMER AND REAL-TIME INTERRUPT
MOTOROLA
10-25
when the signal first changes, increments the count once each time through the main
loop, and stops the count at the end of a period. This method is very simple and yields
an accuracy of
±
100 ms, which is quite reasonable for many applications.
To measure a time greater than the range of the 16-bit main timer counter with an input
capture, timer overflows must be considered. A program is presented that extends the
range of the timer to 24 bits by keeping track of overflows in an 8-bit software counter.
By increasing the size of this software counter, the user could measure even longer
periods. At 2-MHz bus speed and a divide by one prescale factor, 24 bits allow the
user to measure periods up to about 8.38 s.
The most difficult part of this procedure is deciding whether or not to count an overflow
when a capture occurs very close to a timer overflow. Given some assumptions, the
user can tell if the capture happened before or after the overflow by looking at the MSB
of the captured value. Once the user knows which happened first, the case can be
treated accordingly.
First, assume that all timer overflow conditions (TOF= 1) will be handled before the
MSB of the free-running counter becomes set again (that is, in less than half the time
between successive overflows). If TOF and ICxF are both set and the captured value
has a one in its MSB, then the user knows the capture occurred before the overflow.
Conversely, if TOF and ICxF are both set and the captured value is positive (MSB =
0), then the capture occurred after the overflow. Servicing an overflow interrupt takes
less than 15 ms, even in a busy system.
Second, assume that if a capture and an overflow happen in the vicinity of each other,
the input capture will be serviced before the overflow. Vicinity means so close together
that both are pending when the user reaches the input-capture service routine. The
input-capture routine checks for a close overflow; therefore, if an overflow occurred
just after the leading edge or just before a trailing edge of a measurement period, it
can be included correctly. This assumption is needed to avoid the possibility of missing
an overflow that should have been counted or to avoid counting one that occurred just
outside the period being measured. Again, this condition is easy to service because
timer overflow is a lower priority interrupt than input captures. The only way an over-
flow can be serviced before an input capture that occurred in the vicinity is if the over-
flow happened enough before the input capture for the stacking and vector selection
to be completed before the capture is detected. Such a case, which is no different from
having the overflow occur long before the capture, is treated correctly without taking
any special action.
Example 10–3 uses input capture and counts overflows in an 8-bit software counter to
allow measurement of periods between about 70 and 16,777,215 cycles (35 R
S
to
8.38 s). The program runs on an EVB board and displays results on the EVB terminal
display. The important parts of the program are shown in
Figure 10-8
; see
10.6 List-
ing of Timer Examples
for a complete listing. Example 10–3 was specifically struc-
tured to allow easy expansion to other functions. For example, two more period
measurement functions can be added by adding a few instructions to the overflow ser-
vice routine and basically duplicating the IC1 service routine for IC2 and IC3. The fol-
lowing lines of code are from Example 10–3: