-
Notifications
You must be signed in to change notification settings - Fork 0
/
debouncer.vhd
60 lines (54 loc) · 1.38 KB
/
debouncer.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
--
library ieee;
use ieee.std_logic_1164.all;
--
entity debouncer is
generic(
DEBOUNCE_TIME: integer := 5--50_000 -- .1 milisecond for 50MHz clock
);
port(
button, clock: in std_logic;
buttonDebounced: out std_logic;
buttonDebouncedPulse: out std_logic
);
end entity;
--
architecture arch of debouncer is
signal innerDebounced: std_logic := '0';
signal buttonDidDrop: std_logic;
signal innerDebouncedPulse: std_logic := '0';
begin
buttonDebounced <= innerDebounced;
buttonDebouncedPulse <= innerDebouncedPulse;
process(clock, button)
variable counter: integer range 0 to DEBOUNCE_TIME - 1 := 0;
begin
if rising_edge(clock) then
if button /= innerDebounced then
counter := counter + 1;
if counter = DEBOUNCE_TIME - 1 then
counter := 0;
innerDebounced <= not innerDebounced;
end if;
else
counter := 0;
end if;
end if;
end process;
process(clock)
variable detectDown: std_logic := '0';
begin
if rising_edge(clock) then
if innerDebounced = '1' and innerDebouncedPulse = '0' and detectDown = '0' then
innerDebouncedPulse <= '1';
detectDown := '1';
elsif innerDebounced = '0' then
detectDown := '0';
innerDebouncedPulse <= '0';
else
detectDown := detectDown;
innerDebouncedPulse <= '0';
end if;
end if;
end process;
end architecture;