|
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +-- PROJECT: SIMPLE UART FOR FPGA |
| 3 | +-------------------------------------------------------------------------------- |
| 4 | +-- MODULE: DEBOUNCER |
| 5 | +-- AUTHORS: Jakub Cabal <[email protected]> |
| 6 | +-- LICENSE: The MIT License (MIT), please read LICENSE file |
| 7 | +-- WEBSITE: https://github.com/jakubcabal/uart-for-fpga |
| 8 | +-------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +library IEEE; |
| 11 | +use IEEE.STD_LOGIC_1164.ALL; |
| 12 | +use IEEE.NUMERIC_STD.ALL; |
| 13 | + |
| 14 | +entity DEBOUNCER is |
| 15 | + Generic ( |
| 16 | + -- latency of debouncer in clock cycles, minimum value is 2, |
| 17 | + -- value also corresponds to the number of bits compared |
| 18 | + LATENCY : natural := 4 |
| 19 | + ); |
| 20 | + Port ( |
| 21 | + CLK : in std_logic; -- system clock |
| 22 | + DEB_IN : in std_logic; -- input of signal from outside FPGA |
| 23 | + DEB_OUT : out std_logic -- output of debounced (filtered) signal |
| 24 | + ); |
| 25 | +end DEBOUNCER; |
| 26 | + |
| 27 | +architecture RTL of DEBOUNCER is |
| 28 | + |
| 29 | + constant SHREG_DEPTH : natural := LATENCY-1; |
| 30 | + |
| 31 | + signal input_shreg : std_logic_vector(SHREG_DEPTH-1 downto 0); |
| 32 | + signal output_reg_rst : std_logic; |
| 33 | + signal output_reg_set : std_logic; |
| 34 | + |
| 35 | +begin |
| 36 | + |
| 37 | + -- parameterized input shift register |
| 38 | + input_shreg_p : process (CLK) |
| 39 | + begin |
| 40 | + if (rising_edge(CLK)) then |
| 41 | + input_shreg <= input_shreg(SHREG_DEPTH-2 downto 0) & DEB_IN; |
| 42 | + end if; |
| 43 | + end process; |
| 44 | + |
| 45 | + -- output register will be reset when all compared bits are low |
| 46 | + output_reg_rst_p : process (DEB_IN, input_shreg) |
| 47 | + variable or_var : std_logic; |
| 48 | + begin |
| 49 | + or_var := DEB_IN; |
| 50 | + all_bits_or_l : for i in 0 to SHREG_DEPTH-1 loop |
| 51 | + or_var := or_var or input_shreg(i); |
| 52 | + end loop; |
| 53 | + output_reg_rst <= not or_var; |
| 54 | + end process; |
| 55 | + |
| 56 | + -- output register will be set when all compared bits are high |
| 57 | + output_reg_set_p : process (DEB_IN, input_shreg) |
| 58 | + variable and_var : std_logic; |
| 59 | + begin |
| 60 | + and_var := DEB_IN; |
| 61 | + all_bits_and_l : for i in 0 to SHREG_DEPTH-1 loop |
| 62 | + and_var := and_var and input_shreg(i); |
| 63 | + end loop; |
| 64 | + output_reg_set <= and_var; |
| 65 | + end process; |
| 66 | + |
| 67 | + -- output register |
| 68 | + output_reg_p : process (CLK) |
| 69 | + begin |
| 70 | + if (rising_edge(CLK)) then |
| 71 | + if (output_reg_rst = '1') then |
| 72 | + DEB_OUT <= '0'; |
| 73 | + elsif (output_reg_set = '1') then |
| 74 | + DEB_OUT <= '1'; |
| 75 | + end if; |
| 76 | + end if; |
| 77 | + end process; |
| 78 | + |
| 79 | +end RTL; |
0 commit comments