When a button is pressed, mechanical contacts may cause the signal to "bounce" (i.e., generate multiple transitions or glitches) before settling into a stable state. The task is to implement a debouncing circuit that filters out these glitches and provides a clean signal indicating a valid button press.
| Parameter | Comment |
|---|---|
CLK_FREQ_MHZ |
Frequency of the clk_i signal in MHz. |
GLITCH_TIME_NS |
Maximum expected duration for which bounce (glitches) can be observed, in nanoseconds. |
| Signal Name | Direction | Width | Comment |
|---|---|---|---|
clk_i |
input | 1 | Clock signal. |
key_i |
input | 1 | Button signal (0 - pressed, 1 - not pressed). |
key_pressed_stb_o |
output | 1 | Strobe (pulse) for one clock cycle, indicating a valid button press. |
-
Debouncing Logic:
- The button input
key_iis synchronized using two flip-flops (key1andkey2) to mitigate metastability issues that arise due to asynchronous input signals. - The signal
key2represents the debounced signal, which is delayed by two clock cycles.
- The button input
-
Glitch Filtering:
- The
GLITCH_CLKSparameter is calculated by multiplying the glitch time (GLITCH_TIME_NS) in nanoseconds by the clock frequency (CLK_FREQ_MHZ) in MHz, and dividing by 1000 to convert nanoseconds into clock cycles. - The counter
cntincrements each clock cycle when the button is pressed (~key2). If the button is released or the strobe signal (key_pressed_stb_o) is asserted, the counter resets to zero.
- The