-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebounce.vhd
More file actions
84 lines (75 loc) · 2.38 KB
/
Copy pathdebounce.vhd
File metadata and controls
84 lines (75 loc) · 2.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- filename: debounce.vhd
-- kind: vhdl file
-- first created: 05.03.2012
-- created by: zas
--------------------------------------------------------------------------------
-- History:
-- v0.1 : zas 05.03.2012 -- Initial Version
--------------------------------------------------------------------------------
-- Description:
-- Debounces a button, two different modes are possible.
-- Mode = 0 (reactive on rising edge)
-- _ _ ____________________ _ _
-- input ____/ \ / \ / \_/ \ / \______
-- _________________________________________
-- output ____/ \_
--
-- Mode = 1 (reactive on falling edge)
-- _ _ ____________________ _ _
-- input ____/ \ / \ / \_/ \ / \______
-- _______________
-- output _________________/ \______________
--
--------------------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.numeric_std.all;
ENTITY debounce IS
GENERIC(
counterBitNb : positive := 18;
mode : integer := 0 --range (0 to 1) := 0
);
PORT(
input : IN std_ulogic;
debounced : OUT std_ulogic;
clock : IN std_ulogic;
reset : IN std_ulogic
);
END debounce ;
ARCHITECTURE rtl OF debounce IS
signal cnt : unsigned(counterBitNb -1 downto 0);
signal debounced_m0 : std_ulogic;
signal debounced_m1 : std_ulogic;
BEGIN
counter: process(reset, clock)
begin
if reset = '1' then
cnt <= (others => '0');
elsif rising_edge(clock) then
if mode = 0 then
if input = '1' then
cnt <= (others => '1');
else
if cnt /= 0 then
cnt <= cnt - 1;
end if;
end if;
else
if input = '0' then
cnt <= (others => '1');
else
if cnt /= 0 then
cnt <= cnt - 1;
end if;
end if;
end if;
end if;
end process counter;
-- Output
debounced_m0 <= '1' when cnt /= 0
else '0';
debounced_m1 <= '1' when cnt = 0
else '0';
debounced <= debounced_m0 when mode = 0
else debounced_m1;
END ARCHITECTURE rtl;