EE 2361 - Lab # 3
The timescale is very important for this discussion, notice how all the bouncing occurs in a <2ms
window. From the observational point of view of a typical person, this switch is ideal. It would
turn on and off an LED with no noticeable bounce.
However, for a microcontroller making observations at a rate of one every 62.5ns, it is
completely unclear what the state of this switch is for 1.2ms (or about 20 cycles.): is
Superwoman sending a secret message in Morse code? Or a regular person is trying to turn a
switch on? It should also be made clear, these “tactile” microswitches are some of the best and
fastest switches available.
During the EE1301 lab, we were able to safely hide behind the fact that the Photon (in
Automatic Mode) waits at least 1ms between iterations of loop(). From the user code point of
view, the switch state was never unknown.
For the PIC24FJ64GA002, however, this is not the case. We must develop some type of filter to
determine the state of our buttons during the “bounce” period. There are three methods usually
used to debounce switches:
Existing program delay
Hardware RC filter
The simplest of these filters is to utilize existing program delay to sample the switch state once
during an extended period of time. The Photon from EE1301, used this method, loop()’s max
update rate was 1ms in Automatic mode. Generally speaking, if you are interfacing with human
beings, then it is likely some other part of your code also updates at a relatively slow rate. In the
case of this lab, the display frame rate is also in the millisecond range. This method has the
disadvantage that all user input will be delayed by the sample rate of the switch.
State-based debouncing utilizes interrupts (or a fast polling loop) to
run an internal state machine. The state machine effectively “locks
out” further changes in the value of the pin after it has initially
transitioned. This allows for the input to be observed within about
one clock cycle. The disadvantage of this method is it requires the
use of semi-limited resources (interrupts and timers). This method
is often used in simple hardware (like FPGAs where timers and