MOTOROLA
10-20
MAIN TIMER AND REAL-TIME INTERRUPT
M68HC11
REFERENCE MANUAL
ing. Instruction timing tells which cycle within the instruction actually reads or writes a
register. Instruction details can be found in
APPENDIX A INSTRUCTION SET DE-
TAILS
. Detailed information concerning timer captures is given in
10.5 Timing Details
For The Main Timer System
. For most applications, it is not necessary to study the
timing in this much detail, but at least one detailed example should be studied.
Example 10–1(b), which converts the period from Example 10–1(a) into frequency, is
more of a demonstration of FDIV and XGDX than anything else. In a real application,
the user would normally work with the period value rather than converting it to a fre-
quency. Some shortcuts were taken since the results were not that critical. For exam-
ple, the partial sums were truncated rather than rounding or extending the precision of
the calculations. Although these shortcuts lead to small errors in the results, these er-
rors were not important, considering the resolution of the measurements. To measure
the frequency of an incoming signal, a user could accumulate the time of many cycles
of the signal and calculate an average period, which would yield much finer resolution
than the single-cycle measurement taken in Example 10–1.
After working with the timer and pulse accumulator for a while, a good exercise might
be to develop a way to measure the signal frequency to five digits of accuracy. The
following discussion presents a proposed technique that could be tried, although it has
not been proven. First, connect the signal to the pulse accumulator input and to an in-
put-capture input. Capture the time of a first edge and start the pulse accumulator at
a count of 256 minus 200. While waiting for 199 cycles of the incoming signal, monitor
timer overflows, like Example 10–3 does, because 200 cycles of the signal are very
likely to take longer than one timer overflow. When the pulse accumulator has counted
199 cycles of the signal, clear the input-capture flag and wait for a capture of the time
of the 200th cycle. From the number of overflows and the difference between the final
capture value and the first capture value, the user can determine the period of 200 cy-
cles of the incoming signal. Finally, convert this into frequency. The accuracy is basi-
cally one E cycle in 200 cycles of the signal, which corresponds to about
±
1 Hz at 20
kHz, and the accuracy improves for lower frequencies. For very low frequencies, it is
not necessary to measure many periods to get the accuracy. Try to include a prelimi-
nary trial measurement to decide how many cycles should be measured to get the
needed accuracy (auto ranging).
10.3.3 Using Input Capture to Measure Pulse Width
Timer Example 10–2 shows how to measure a pulse width with an input capture, which
is almost the same as measuring a period, except that the input-capture edge sensi-
tivity must be reconfigured between the capture of the first edge and the second edge.
Since this particular program measures the period of a high-going pulse, the input cap-
ture is first configured to capture on a rising edge at the input. After detecting the first
edge, the input capture is reconfigured to detect a falling edge.
Since this program is interrupt driven, it must have an interrupt service routine, which
is automatically called as a result of an interrupt, an initialization portion, and a main-
line program portion. Since this example is only demonstrating the input-capture func-
tion, the mainline program will be a trivial two-instruction loop that repeats until a pulse
has been measured. In a practical application, the mainline portion would be every-