debounce_fsm.vhd


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