1
1
-- ------------------------------------------------------------------------------
2
- -- PROJECT: FPGA MISC
2
+ -- PROJECT: SPI MASTER AND SLAVE FOR FPGA
3
3
-- ------------------------------------------------------------------------------
4
- -- NAME: BTN_DEBOUNCE
5
4
-- AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
6
- -- LICENSE: The MIT License
7
- -- WEBSITE: https://github.com/jakubcabal/fpga-misc
5
+ -- LICENSE: LGPL-3.0, please read LICENSE file
6
+ -- WEBSITE: https://github.com/jakubcabal/spi-fpga
8
7
-- ------------------------------------------------------------------------------
9
8
10
9
library IEEE;
11
10
use IEEE.STD_LOGIC_1164.ALL ;
12
11
use IEEE.NUMERIC_STD.ALL ;
13
12
14
13
entity BTN_DEBOUNCE is
14
+ Generic (
15
+ CNT_WIDTH : natural := 2 -- width of debounce counter
16
+ );
15
17
Port (
16
- CLK : in std_logic ; -- system clock
17
- CLK_EN_1K : in std_logic ; -- clock enable 1 KHz
18
- ASYNC_RST : in std_logic ; -- asynchrounous reset
19
- BTN_RAW : in std_logic ; -- button raw signal
20
- BTN_DEB : out std_logic ; -- button debounce signal
21
- BTN_DEB_EN : out std_logic -- button debounce rising edge enable
18
+ CLK : in std_logic ; -- system clock
19
+ ASYNC_RST : in std_logic ; -- asynchrounous reset
20
+ SAMPLE_EN : in std_logic ; -- sample clock enable
21
+ BTN_RAW : in std_logic ; -- button raw signal
22
+ BTN_DEB : out std_logic ; -- button debounce signal
23
+ BTN_DEB_RE : out std_logic -- rising edge of debounced signal
22
24
);
23
- end BTN_DEBOUNCE ;
25
+ end entity ;
24
26
25
27
architecture RTL of BTN_DEBOUNCE is
26
28
27
- signal btn_raw_shreg : std_logic_vector (3 downto 0 );
28
- signal btn_deb_comb : std_logic ;
29
- signal btn_deb_reg : std_logic ;
30
- signal btn_deb_en_reg : std_logic ;
29
+ signal btn_raw_sync_reg1 : std_logic ;
30
+ signal btn_raw_sync_reg2 : std_logic ;
31
+ signal btn_raw_sample_reg : std_logic ;
32
+ signal btn_raw_diff : std_logic ;
33
+ signal deb_cnt : unsigned (CNT_WIDTH- 1 downto 0 );
34
+ signal deb_cnt_max : std_logic ;
35
+ signal btn_deb_reg : std_logic ;
36
+ signal btn_deb_re_reg : std_logic ;
31
37
32
38
begin
33
39
34
40
-- -------------------------------------------------------------------------
35
- -- SHIFT REGISTER OF BUTTON RAW SIGNAL
41
+ -- BUTTON RAW SIGNAL SYNCHRONIZATION REGISTERS
36
42
-- -------------------------------------------------------------------------
37
43
38
- btn_shreg_p : process (CLK, ASYNC_RST )
44
+ btn_raw_sync_reg_p : process (CLK)
39
45
begin
40
- if (ASYNC_RST = '1' ) then
41
- btn_raw_shreg <= (others => '0' );
42
- elsif (rising_edge (CLK)) then
43
- if (CLK_EN_1K = '1' ) then
44
- btn_raw_shreg <= btn_raw_shreg(2 downto 0 ) & BTN_RAW;
45
- end if ;
46
+ if (rising_edge (CLK)) then
47
+ btn_raw_sync_reg1 <= BTN_RAW;
48
+ btn_raw_sync_reg2 <= btn_raw_sync_reg1;
46
49
end if ;
47
50
end process ;
48
51
49
52
-- -------------------------------------------------------------------------
50
- -- DEBOUNCE REGISTER OF BUTTON RAW SIGNAL
53
+ -- BUTTON RAW SIGNAL SAMPLE REGISTERS
51
54
-- -------------------------------------------------------------------------
52
55
53
- btn_deb_comb <= btn_raw_shreg(0 ) and btn_raw_shreg(1 ) and
54
- btn_raw_shreg(2 ) and btn_raw_shreg(3 );
56
+ btn_raw_sample_reg_p : process (CLK)
57
+ begin
58
+ if (rising_edge (CLK)) then
59
+ if (SAMPLE_EN = '1' ) then
60
+ btn_raw_sample_reg <= btn_raw_sync_reg2;
61
+ end if ;
62
+ end if ;
63
+ end process ;
64
+
65
+ btn_raw_diff <= btn_raw_sample_reg xor btn_raw_sync_reg2;
55
66
56
- btn_deb_reg_p : process (CLK, ASYNC_RST)
67
+ -- -------------------------------------------------------------------------
68
+ -- DEBOUNCE COUNTER
69
+ -- -------------------------------------------------------------------------
70
+
71
+ deb_cnt_p : process (CLK, ASYNC_RST)
57
72
begin
58
73
if (ASYNC_RST = '1' ) then
59
- btn_deb_reg <= '0' ;
74
+ deb_cnt <= ( others => '0' ) ;
60
75
elsif (rising_edge (CLK)) then
61
- btn_deb_reg <= btn_deb_comb;
76
+ if (SAMPLE_EN = '1' ) then
77
+ if (btn_raw_diff = '1' ) then
78
+ deb_cnt <= (others => '0' );
79
+ else
80
+ deb_cnt <= deb_cnt + 1 ;
81
+ end if ;
82
+ end if ;
83
+ end if ;
84
+ end process ;
85
+
86
+ deb_cnt_max <= '1' when (deb_cnt = (2 ** CNT_WIDTH)- 1 ) else '0' ;
87
+
88
+ -- -------------------------------------------------------------------------
89
+ -- BUTTON DEBOUNCE SIGNAL REGISTER
90
+ -- -------------------------------------------------------------------------
91
+
92
+ btn_deb_reg_p : process (CLK)
93
+ begin
94
+ if (rising_edge (CLK)) then
95
+ if (deb_cnt_max = '1' ) then
96
+ btn_deb_reg <= btn_raw_sample_reg;
97
+ end if ;
62
98
end if ;
63
99
end process ;
64
100
@@ -68,15 +104,15 @@ begin
68
104
-- RISING EDGE DETECTOR OF BUTTON DEBOUNCE SIGNAL
69
105
-- -------------------------------------------------------------------------
70
106
71
- sseg_an_cnt_p : process (CLK, ASYNC_RST)
107
+ btn_deb_re_reg_p : process (CLK, ASYNC_RST)
72
108
begin
73
109
if (ASYNC_RST = '1' ) then
74
- btn_deb_en_reg <= '0' ;
110
+ btn_deb_re_reg <= '0' ;
75
111
elsif (rising_edge (CLK)) then
76
- btn_deb_en_reg <= btn_deb_comb and not btn_deb_reg;
112
+ btn_deb_re_reg <= deb_cnt_max and btn_raw_sample_reg and not btn_deb_reg;
77
113
end if ;
78
114
end process ;
79
115
80
- BTN_DEB_EN <= btn_deb_en_reg ;
116
+ BTN_DEB_RE <= btn_deb_re_reg ;
81
117
82
- end RTL ;
118
+ end architecture ;
0 commit comments