-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
debounce.vhd
63 lines (59 loc) · 1.98 KB
/
debounce.vhd
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
55
56
57
58
59
60
61
62
63
------------------------------------------------------------
-- File : debounce.vhd
------------------------------------------------------------
-- Author : Peter Samarin <peter.samarin@gmail.com>
------------------------------------------------------------
-- Copyright (c) 2019 Peter Samarin
------------------------------------------------------------
-- Description: debouncing circuit that forwards only
-- signals that have been stable for the whole duration of
-- the counter
------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
------------------------------------------------------------
entity debounce is
generic (
WAIT_CYCLES : integer := 5);
port (
signal_in : in std_logic;
signal_out : out std_logic;
clk : in std_logic);
end entity debounce;
------------------------------------------------------------
architecture arch of debounce is
type state_t is (idle, check_input_stable);
signal state_reg : state_t := idle;
signal out_reg : std_logic := signal_in;
signal signal_in_reg : std_logic;
signal counter : integer range 0 to WAIT_CYCLES-1 := 0;
begin
process (clk) is
begin
if rising_edge(clk) then
case state_reg is
when idle =>
if out_reg /= signal_in then
signal_in_reg <= signal_in;
state_reg <= check_input_stable;
counter <= WAIT_CYCLES-1;
end if;
when check_input_stable =>
if counter = 0 then
if signal_in = signal_in_reg then
out_reg <= signal_in;
end if;
state_reg <= idle;
else
if signal_in /= signal_in_reg then
state_reg <= idle;
end if;
counter <= counter - 1;
end if;
end case;
end if;
end process;
-- output
signal_out <= out_reg;
end architecture arch;