Hi Daixiwen and thanks,
Sorry for a long time no answer. Finally solved the problem. using the following code:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity debounce is
generic(
counter_size : INTEGER := 19); --counter size (19 bits gives 10.5ms with 50MHz clock)
port(
Digit : out std_logic_vector (6 downto 0); -- 7 seg digit
hex_count : out std_logic_vector(3 downto 0); -- Hexadecimal result
clk, reset, button : in std_logic; --input signals
Digit_en : out std_logic); -- 7 seg digit enable
end debounce;
architecture logic of debounce is
signal counter : std_logic_vector(3 downto 0) :=(others => '0'); --initializing count to zero.
signal flipflops : std_logic_vector(1 downto 0); --input flip flops
signal counter_out : std_logic_vector(counter_size downto 0) := (others => '0'); --counter output
signal counter_set : std_logic; --sync reset to zero
signal result : std_logic; --debouncer result
begin
counter_set <= flipflops(0) xor flipflops(1); --determine when to start/reset counter
process(clk)
begin
if(clk'event and clk = '1') then
flipflops(0) <= button;
flipflops(1) <= flipflops(0);
if(counter_set = '1') then --reset counter because input is changing
counter_out <= (others => '0');
elsif(counter_out(counter_size) = '0') then --stable input time is not yet met
counter_out <= counter_out + 1;
else --stable input time is met
result <= not flipflops(1);
end if;
end if;
end process;
process(result)
begin
if(reset='0') then --when reset equal to '0' make count equal to 0.
counter <=(others => '0'); -- c ="0000"
elsif(result'event and result = '1') then
if(counter = "1111") then -- when count reaches its maximum(that is 15) reset it to 0
counter <="0000";
end if;
counter <= counter+'1'; --increment count at every positive edge of clk.
end if;
case counter is
when "0000"=>Digit<="1000000";-- 0
when "0001"=>Digit<="1111001";-- 1
when "0010"=>Digit<="0100100";-- 2
when "0011"=>Digit<="0110000";-- 3
when "0100"=>Digit<="0011001";-- 4
when "0101"=>Digit<="0010010";-- 5
when "0110"=>Digit<="0000010";-- 6
when "0111"=>Digit<="1111000";-- 7
when "1000"=>Digit<="0000000";-- 8
when "1001"=>Digit<="0010000";-- 9
when "1010"=>Digit<="0001000";-- A
when "1011"=>Digit<="0000011";-- b
when "1100"=>Digit<="1000110";-- C
when "1101"=>Digit<="0100001";-- d
when "1110"=>Digit<="0000110";-- E
when "1111"=>Digit<="0001110";-- F
when others=>Digit<="1111111";-- in other cases balnk
end case;
end process;
Digit_en<='0';
hex_count<=counter;
end;