Altifigence Academy

47 / 63 · Concept

Real button inputs: synchronization, debouncing and one event

Separate asynchronous-input synchronization from contact debouncing and design a consecutive-sample debouncer.

One button press can contain many transitions

A mechanical button's contacts can repeatedly make and break before settling. Its signal is also asynchronous to the system clock. Synchronization brings a signal into a clock domain; debouncing interprets short repeated transitions as one stable state. Two flip-flopFlip-flop An element that captures its input at a specified clock edge and holds it until the next edge. The input must meet setup and hold requirements. Learn more stages do not automatically remove bounce.

Separate the roles of the three processing stages
  1. Asynchronous button: Both contact bounce and clock asynchrony are present
  2. Synchronization: Receive a single-bit level into the clock domain
  3. Debouncing: Accept a state change only after consecutive stable samples
  4. Edge detection: Generate a one-cycle event for an accepted 0→1

This flow assumes a single-bit button level held sufficiently long. Short pulses, coherent multibit transfers and asynchronous clocks themselves require separate designs.

The N-consecutive-samples rule

Let q be the currently accepted state, x the synchronized input, and count the number of consecutive samples differing from q. At each sample, clear count to 0 if x=q. Otherwise increment it; at the Nth consecutive opposite sample, update q=x and clear count.

For N=3 and initial q=0, each column below is a sampling instant of the synchronized input, not a physical contact-voltage trace.

Sample012345678
x010111101
count after010120010
q after000001111
rise event000001000
N=3: accept after consecutive opposite samples
View waveform data
Wave data: each character is one interval; a dot holds the previous state; p is a clock cycle.
SignalWaveBus values
sample2345234520 → 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8
x0101...01
q after0....1...
rise event0....10..

Specify latency and counter width

With sample interval TsT_s, acceptance takes approximately (N−1)Ts(N-1)T_s to NTsNT_s, depending on transition phase; add upstream synchronization latency separately. A conservative “stable for 10 ms” requirement must account for the interval between the first and last samples when choosing N. Short transitions between samples are invisible, so matching consecutive samples does not prove the physical voltage never changed in between. Specify switch characteristics and sampling conditions together.

If count stores only 0 through N−1, its width is max⁡(1,⌈log⁡2N⌉)\max(1,\lceil\log_2 N\rceil). Handle N=1 too: the first opposite sample must be accepted immediately, so the ordering of the N−1 comparison matters.

Turn one press into one event

For accepted next state q_next, rise=q‾ qnextrise=\overline q\,q_{next}. The table emits the event at the same sample that accepts the change. An implementation comparing q with a one-cycle-delayed q in a separate registerRegister A circuit that stores multiple bits of state. The synchronous registers in this course store their specified inputs at a clock edge. Learn more may add latency depending on observation location, so also specify when the event is consumed.

Try it yourself

For N=3 and initial q=0, apply 1,1,0,1,1,1. At which sample does state change? What is the minimum width of an N=8 counter storing 0–7?

Read the explanation

The third sample, 0, clears the consecutive count, so 1 is accepted at the sixth sample. Storing count 0–7 needs three bits. An implementation storing through 8 needs four, so first determine the stored range.

Your choice applies to this browser. Change it any time using the footer.