Page 271 - Programming Microcontrollers in C
P. 271

256    Chapter 5  Programming Large 8-Bit Systems

                          this value will be correct over the lifetime of the contact. As the
                          mechanism ages, it is possible that the bounce time will change, and
                          it will always change in the worst possible way. Therefore, be safe,
                          and keep the debounce time at least twice the bounce time, and perhaps
                          longer for the sake of safety. The range of values used for debounce
                          times ranges from 2 to 30 milliseconds.
                              This system is measuring the time of an input. How can we stick
                          an arbitrary time for debounce into our measurement equation and be
                          reliable? It is possible to do the debounce and not have the time as part
                          of the measured interval. The contact closure will cause an observable
                          input. Our real concern with debounce is to guarantee that no additional
                          inputs occur after the initial input has been processed. Therefore, we
                          can implement a debounce by merely not re-enabling the input capture
                          interrupt until the debounce time has passed. This way, the time of the
                          first edge seen in the input sequence is processed, and all other inputs
                          caused by bounce will not be processed because the input capture
                          interrupt is disabled. The time that the interrupt is disabled can also be
                          adjusted to meet the particular needs of the program. For example, in
                          the system we will construct below, the speed is measured with a reed
                          switch and a magnet on the motor shaft. The circuit will be built such
                          that the closing of the switch will cause a voltage to fall from +12 volts
                          to 0 volts. There is a resistor between the 12-volt supply and the switch.
                          The bottom side of the switch is grounded, and as the switch is closed
                          and opened, the voltage measured at the top of the switch will change
                          from 12 volts to ground. When the switch closes, it is expected that it
                          will bounce and the voltage will “chatter” between 0 and 12 volts.
                          Surprisingly, the opening of the switch, while it will exhibit less bounce,
                          will also generate these unwanted signals. If we want to block out all
                          of the error signals that can be introduced by the bounce, it is necessary
                          that the time following the initial signal drop be protected, as well as a
                          time around the signal rise that occurs midway during the shaft rotation.
                          In the first example, we will provide a fixed time for the debounce.
                          Later when the program is measuring the speed of the motor, we will
                          provide a debounce time that is somewhat longer than one half the
                          measured period to block out all bounce signals that occur on both the
                          rising and falling edge of the input signal.
                              The input capture subsystem will capture any specified input when
                          enabled. These inputs are captured whether the interrupts are enabled
                          or not. In other words, if an input occurs after the one that was captured,
   266   267   268   269   270   271   272   273   274   275   276