-
Notifications
You must be signed in to change notification settings - Fork 1
/
debounce.v
50 lines (44 loc) · 1.06 KB
/
debounce.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Button debouncer
// by Tomek Szczesny 2023
//
// This module acts as a crude, simple low pass filter, which in particular is
// useful for button debouncing. Without it, button presses are often
// registered more than once.
// It requires a clock. Any clock will do, the slowest one available in the
// system usually will suffice.
// Debounce period of ~10ms is more than enough in most applications.
//
// +----------------+
// clk --->| |
// in --->| debounce |---> out
// | |
// +----------------+
//
// Parameters:
// n - filter length (in clk periods) (1024)
//
// Ports:
// clk - clock input
// in - Input
// out - Output
//
`ifndef _debounce_v_
`define _debounce_v_
module debounce(
input wire in,
input wire clk,
output reg out = 0
);
parameter n = 1024;
localparam max = n-1;
reg [$clog2(n-1):0] ctr = 0;
always@(posedge clk)
begin
if (ctr == 0) out <= 0;
if (ctr == max) out <= 1;
if ( in & (ctr != max)) ctr <= ctr+1;
if (~in & (ctr != 0 )) ctr <= ctr-1;
end
endmodule
`endif