1 | library IEEE;
|
2 | use IEEE.std_logic_1164.all;
|
3 | use IEEE.numeric_std.all;
|
4 |
|
5 | entity debounce_fsm is
|
6 | generic( MAX_CNT : integer := 40);
|
7 | port( clk, rst, taster : in std_logic;
|
8 | debounced : out std_logic);
|
9 | end entity debounce_fsm;
|
10 |
|
11 | architecture behavorial of debounce_fsm is
|
12 |
|
13 | type state is (IDLE, WAITING);
|
14 | signal current_state : state := IDLE;
|
15 |
|
16 | signal counting, endCount : std_logic := '0';
|
17 |
|
18 | signal countval : integer range 0 to MAX_CNT := 0;
|
19 |
|
20 | begin
|
21 |
|
22 | counter : process (clk)
|
23 | begin
|
24 | if rising_edge(clk) then
|
25 | if countval = MAX_CNT then
|
26 | countval <= 0;
|
27 | endCount <= '1';
|
28 | else
|
29 | endCount <= '0';
|
30 | if counting = '1' then
|
31 | countval <= countval + 1;
|
32 | end if;
|
33 | end if;
|
34 | end if;
|
35 | end process counter;
|
36 |
|
37 |
|
38 | transfer_function : process (clk)
|
39 | begin
|
40 | if rising_edge(clk) then
|
41 | case current_state is
|
42 | when IDLE =>
|
43 | if taster = '0' then
|
44 | current_state <= WAITING;
|
45 | else
|
46 | current_state <= IDLE;
|
47 | end if;
|
48 | when WAITING =>
|
49 | if endCount = '1' then
|
50 | if taster = '1' then
|
51 | current_state <= IDLE;
|
52 | else
|
53 | current_state <= WAITING;
|
54 | end if;
|
55 | end if;
|
56 | end case;
|
57 | end if;
|
58 | end process transfer_function;
|
59 |
|
60 |
|
61 | output_function : process (current_state)
|
62 | begin
|
63 | case current_state is
|
64 | when IDLE =>
|
65 | counting <= '0';
|
66 | debounced <= '1';
|
67 | when WAITING =>
|
68 | counting <= '1';
|
69 | debounced <= '0';
|
70 | end case;
|
71 | end process output_function;
|
72 |
|
73 | end behavorial;
|