Skip to main content

Subsection 9.3.6 Bounce, Now That Edges Matter

Contact bounce was a nuisance for polling and is a correctness problem for edge-triggered interrupts. A polling loop reads a level, and by the time it looks again the contacts have long since settled, so bounce is invisible to it unless the loop is very fast. The EXTI’s edge detector has no such luck: it fires on every falling edge, and an undebounced switch can deliver several within a few milliseconds. The result is one press that sets the pending flag repeatedly and one ISR that runs several times.
The hardware fix is the one already on your breadboard, the debouncing capacitor (Subsection 2.5.1), which slows the recovery enough that the voltage never climbs back above the input threshold during the bounce window. Figure 9.3.4 is what a debounced press looks like at the pin: one transition, 20 µs per division, no excursions. The software fix, when hardware debouncing is not an option, is for the ISR to record the time of the edge and ignore any further edge within a few tens of milliseconds, the same idea as the software debouncing we saw earlier, moved into the handler.
Figure 9.3.4. A debounced button press captured at the pin, 20 µs per division: a single clean fall from 3.3 V to 0 V with no excursions back above threshold. This is the edge the EXTI’s detector sees, and it produces exactly one interrupt. Without the capacitor the same press can produce a burst of falling edges, and a burst of ISR calls. A second channel is shown offset below.