When to use it
A mechanical button doesn't switch cleanly, its contacts bounce for a few milliseconds, so a raw press can look like a dozen presses to your logic. A debouncer only accepts a new level once the input has held steady long enough.
How it works
First the noisy input is run through a synchronizer. Then a counter watches it: if
the level differs from the current clean output, count; once it has been stable for
COUNT clocks, commit the new level. Any bounce shorter than COUNT clocks resets
the counter and is ignored.
Gotchas
- Size
COUNTfor your clock: pick a few milliseconds (e.g. at 50 MHz, ~10 ms is 500,000 clocks). The demo uses a tiny value so you can see it on the waveform. - Always synchronize the raw pin first (done here), a button is asynchronous to your clock, see the synchronizer recipe.