-
Notifications
You must be signed in to change notification settings - Fork 6
/
Debounce.vhd
55 lines (43 loc) · 1.66 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
-------------------------------------------------------------------------------
-- File downloaded from http://www.nandland.com
-------------------------------------------------------------------------------
-- This module is used to debounce any switch or button coming into the FPGA.
-- Does not allow the output of the switch to change unless the switch is
-- steady long enough time.
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity Debounce is
port (
i_Clk : in std_logic;
i_Switch : in std_logic;
o_Switch : out std_logic
);
end entity Debounce;
architecture RTL of Debounce is
-- Set for 250,000 clock ticks of 25 MHz clock (10 ms)
constant c_DEBOUNCE_LIMIT : integer := 250000;
signal r_Count : integer range 0 to c_DEBOUNCE_LIMIT := 0;
signal r_State : std_logic := '0';
begin
p_Debounce : process (i_Clk) is
begin
if rising_edge(i_Clk) then
-- Switch input is different than internal switch value, so an input is
-- changing. Increase counter until it is stable for c_DEBOUNCE_LIMIT.
if (i_Switch /= r_State and r_Count < c_DEBOUNCE_LIMIT) then
r_Count <= r_Count + 1;
-- End of counter reached, switch is stable, register it, reset counter
elsif r_Count = c_DEBOUNCE_LIMIT then
r_State <= i_Switch;
r_Count <= 0;
-- Switches are the same state, reset the counter
else
r_Count <= 0;
end if;
end if;
end process p_Debounce;
-- Assign internal register to output (debounced!)
o_Switch <= r_State;
end architecture RTL;