-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebounce.sv
More file actions
55 lines (45 loc) · 1.34 KB
/
debounce.sv
File metadata and controls
55 lines (45 loc) · 1.34 KB
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
51
52
53
54
module debounce #(
parameter DELAY_COUNTS = 2500/*FILL-IN*/ // 50us with clk period 20ns is ____ counts
) (
input clk, button,
output reg button_pressed
);
// Use a synchronizer to synchronize `button`.
wire button_sync; // Output of the synchronizer. Input to your debounce logic.
synchroniser button_synchroniser (.clk(clk), .x(button), .y(button_sync));
// Note: Use the synchronized `button_sync` wire as the input signal to the debounce logic.
/*** Fill in the following scaffold: ***/
// Create a register for the delay counts
reg [$clog2(DELAY_COUNTS) -1:0] count;
reg prev_button;
// Set the count flip-flop:
always @(posedge clk) begin
if (button_sync != prev_button) begin
count <= 0;
end
else if (count == DELAY_COUNTS) begin
count <= count;
end
else begin
count <= count + 1;
end
end
// Set the prev_button flip-flop:
always @(posedge clk) begin
if (button_sync != prev_button) begin
prev_button <= button_sync;
end
else begin
prev_button <= prev_button;
end
end
// Set the button_pressed flip-flop:
always @(posedge clk) begin
if (button_sync == prev_button && count == DELAY_COUNTS) begin
button_pressed <= prev_button;
end
else begin
button_pressed <= button_pressed;
end
end
endmodule