-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
switchDebouncer.v
44 lines (39 loc) · 1.08 KB
/
switchDebouncer.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
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2020 Akilesh Kannan <[email protected]>
//
// File: switchDebouncer.v
// Modified: 2020-07-15
// Description: Switch Debouncing Circuit
//
//
// License: MIT
//
////////////////////////////////////////////////////////////////////////
`default_nettype None
`timescale 1ns/1ps
module switchDebouncer #(parameter integer N = 16) (output reg switchOut, input switchIn, input clk);
wire rst, add;
reg dFF1, dFF2;
reg[N-1:0] timeReg, nextReg;
assign rst = dFF1 ^ dFF2;
assign add = ~timeReg[N-1];
always @(rst, add, timeReg) begin
case({rst, add})
2'b00: nextReg <= timeReg;
2'b01: nextReg <= timeReg + 1;
default: nextReg <= {N{1'b0}};
endcase
end
always @(posedge clk) begin
dFF1 <= switchIn;
dFF2 <= dFF1;
timeReg <= nextReg;
end
always @(posedge clk) begin
if(timeReg[N-1] == 1'b1)
switchOut <= dFF2;
else
switchOut <= switchOut;
end
endmodule