Page 272 - Programming Microcontrollers in C
P. 272
Timer Operations 257
it will be captured and the original saved value will be lost. Therefore,
the very first operation in the interrupt service routine for an input
capture should be to save the contents of the input capture register.
Then subsequent inputs from bounces will not affect the measured
time. Another problem can occur, however. Whenever an input occurs
on an input capture line, not only are the time data captured, but also,
the appropriate input capture interrupt flag is set. If this flag is set, an
interrupt will be requested whenever the input capture interrupt is
enabled. Therefore, in the debounce interrupt routine, the input capture
interrupt flag for the channel being used should also be reset.
We still must control the time that the input capture is disabled. How
this control is implemented depends on the system. If the system is not
busy, one might calculate a value that equals the contents of the TCR
plus the number of timer counter ticks in the debounce time. This value
could then be compared with the contents of the TCR, and when the
TCR equals the calculated value, the input capture interrupt would be
reenabled, and control returned to the interrupted program. This approach
provides an accurate debounce time, but the processor is devoted entirely
to the measurement of this time during the debounce time. It is not wise
to tie up a microcontroller for milliseconds at a time and lock out other
important actions that might take place during that time.
If all other events that are occurring within the microcontroller
are interrupt driven, the programmer could re-enable the system
interrupts prior to entering the delay time. This approach would at
least keep the processor available for other asynchronous events that
might occur during the debounce period. Yet another approach would
be to disable the input capture interrupt and within the applications
program, provide a time measurement that would have to expire before
the input capture interrupt is reenabled. Both of these methods can
be implemented without the use of an output compare. If an output
compare is available, it could be used to execute the debounce timeout.
This output compare would be set up in the input capture interrupt
service routine. Also within the input capture interrupt service routine,
the input capture interrupt would be disabled. The output compare
interrupt service routine would disable the output compare operation
and reenable the input capture. This approach is by far the best, and
will be used here. The code for this method of debounce is included
in the listing shown in Listing 5-6. In this case, a fixed debounce
time is used. We will see the variable debounce time in a later program.