-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebouncer.v
47 lines (36 loc) · 1.12 KB
/
debouncer.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
module PushButton_Debouncer(
input clk,
input PB,
output reg PB_state
);
reg PB_sync_0; always @(posedge clk) PB_sync_0 <= ~PB;
reg PB_sync_1; always @(posedge clk) PB_sync_1 <= PB_sync_0;
reg [15:0] PB_cnt;
wire PB_cnt_max = &PB_cnt; // true when all bits of PB_cnt are 1's
always @(posedge clk)
if(PB_state==PB_sync_1)
PB_cnt <= 0; // nothing's going on
else
begin
PB_cnt <= PB_cnt + 16'd1; // something's going on, increment the counter
if(PB_cnt_max) PB_state <= ~PB_state; // if the counter is maxed out, PB changed!
end
endmodule
module SMALLDebouncer(
input clk,
input PB,
output reg PB_state
);
reg PB_sync_0; always @(posedge clk) PB_sync_0 <= ~PB;
reg PB_sync_1; always @(posedge clk) PB_sync_1 <= PB_sync_0;
reg [1:0] PB_cnt;
wire PB_cnt_max = &PB_cnt; // true when all bits of PB_cnt are 1's
always @(posedge clk)
if(PB_state==PB_sync_1)
PB_cnt <= 0; // nothing's going on
else
begin
PB_cnt <= PB_cnt + 16'd1; // something's going on, increment the counter
if(PB_cnt_max) PB_state <= ~PB_state; // if the counter is maxed out, PB changed!
end
endmodule