Page 270 - Programming Microcontrollers in C
P. 270
Timer Operations 255
clear reason otherwise, it is usually best to operate a control system
at the maximum possible speed. One reason that is important is the
operation of the PWM DAC that accompanies the operation of the
control system. It is usually best to have a PWM run at the highest
practical speed. If the PWM is slow, then conversion of the pulse
output from the system to a DC voltage is difficult and not very
accurate on an instantaneous basis.
The following code segment might be used as an interrupt service
routine to handle the input capture operation.
@port void IC1_Isr( void)
{
TFLG1=IC1F; /* reset IC1 interrupt flag */
measured_period=TIC1-time1;
time1=TIC1;
}
Here it is assumed that the maximum time between input captures is
less than the time of a timer overflow. In this case, the time is merely
the difference between the current value and the preceding value
which is stored in time1.
This approach has only one major problem. Most inputs such as
will be obtained from reed switches, push buttons, and even optical
interrupt type devices will be noisy when the contact is closed. This
noise is called switch bounce, and it will always be present with a
contact closure. Therefore, the switch must be debounced in some
manner before its data are reliable.
The most common way of debouncing a contact closure is to
observe the closure, and then wait a time and observe if the contact is
still closed. If closed, it is assumed that the contact is good; otherwise,
another wait period in allowed to elapse and the contact is observed
again. This procedure is repeated until the contact is closed on a pair
of successive observations. Only then is it assumed that the contact is
closed. The time between observations is the subject of much
engineering debate. Often the designer can place an oscilloscope on
the contact and repeatedly close and open it. With proper
synchronization of the instrument, it is possible to see the signal caused
by the bouncing contacts. If this time can be measured, then having
a debounce time of perhaps twice the bounce time will probably give
a safe time for the debounce. However, you should not assume that