Codiode

Continuous Counter Debounce

MediumVerilog / SystemVerilogBuildFree2 solved

A mechanical switch bounces for several milliseconds when pressed. A debouncer accepts a new level only after the input has held it for N clock cycles in a row.

A continuous counter debounce circuit monitors an asynchronous input signal. It requires the input to remain at a new, stable logic level for a consecutive number of clock cycles before updating the output. If the input glitches or reverts to its previous state before the threshold is reached, the internal counter completely resets. The output only transitions once the input has held the new state for the full duration.

The module operates on a positive clock edge posedge clk and uses an asynchronous, active-low reset rst_n. On reset, both the internal counter and the debounced output clean_out must be forced to 0. A parameter THRESHOLD defines the number of consecutive stable clock cycles required to register a change. When noisy_in differs from clean_out, the counter increments. If noisy_in matches clean_out, the counter synchronously resets to 0. When the counter reaches THRESHOLD, clean_out updates to match noisy_in and the counter resets to 0.

Cycle 1: rst_n=0 → clean_out=0, count=0
Cycle 2: rst_n=1, noisy_in=1 → clean_out=0, count=1
Cycle 3: rst_n=1, noisy_in=0 → clean_out=0, count=0
Cycle 4: rst_n=1, noisy_in=1 → clean_out=0, count=1
Cycle 5: rst_n=1, noisy_in=1 → clean_out=0, count=2
Cycle 6: rst_n=1, noisy_in=1 → clean_out=0, count=3
Cycle 7: rst_n=1, noisy_in=1 → clean_out=1, count=0
Cycle 8: rst_n=1, noisy_in=1 → clean_out=1, count=0

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; sets clean_out and internal counter to 0
noisy_in input 1 The raw bouncing input signal
clean_out output 1 The debounced output signal; resets to 0

Constraints

  • Output clean_out and the internal counter must asynchronously reset to 0 when rst_n is 0
  • The counter must increment on posedge clk only when noisy_in differs from clean_out
  • If noisy_in equals clean_out, the counter must synchronously reset to 0
  • When the counter reaches THRESHOLD, clean_out must update to noisy_in on the same clock edge, and the counter must reset to 0
  • The module must include a parameter THRESHOLD with a default value of 4

Topics

FSMTimingInterfaces

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

Free to solve. A Codiode account keeps your progress.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Interfaces and Protocols

Problem 2 of 19 in the Interfaces and Protocols track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.