entity Dip_PB_Led is
port ( clk : in std_logic; -- system clock
reset_b : in std_logic; -- system reset, active low
PBSwitch : in std_logic; -- Push Button Switch, active low
PBSwitch_two: in std_logic; -- Push Button Switch two,active low
DipSwitch : in std_logic; -- Dip Switch, active high
Led_inv : out std_logic_vector(3 downto 0) -- User Leds
);
end Dip_PB_Led;
architecture rtl of Dip_PB_Led is
signal PBSwitch_flop1 : std_logic;
signal PBSwitch_flop2 : std_logic;
signal PBSwitch_two_flop1:std_logic;
signal PBSwitch_two_flop2:std_logic;
signal PB_PulseOut : std_logic;
signal PB_two_PulseOut : std_logic;
signal PB_valid : std_logic;
signal PB_two_valid : std_logic;
signal DipSwitch_flop1 : std_logic;
signal DipSwitch_flop2 : std_logic;
signal Led : unsigned(3 downto 0);
signal count : unsigned(15 downto 0);
signal mincount : unsigned(15 downto 0) := "0000000000000000";
--********************************************************************************************
-- As clock is of 48 Mhz, 0x5DC0 is required to get debounce count of 500 microsecond
--********************************************************************************************
constant debounce_count: unsigned (15 downto 0) := "0000000000000001";-- 5DC0
--********************************************************************************************
-- Push Button Switches are floped twice 0101110111000000
--********************************************************************************************
begin
PB_flop: process (clk, reset_b)
begin
if reset_b = '0' then
PBSwitch_flop1 <= '1';
PBSwitch_flop2 <= '1';
PBSwitch_two_flop1 <= '1';
PBSwitch_two_flop2 <= '1';
elsif rising_edge (clk) then
PBSwitch_flop1 <= PBSwitch;
PBSwitch_flop2 <= PBSwitch_flop1;
PBSwitch_two_flop1 <= PBSwitch_two;
PBSwitch_two_flop2 <= PBSwitch_two_flop1;
end if;
end process;
--********************************************************************************************
-- This process module generates stable pulse
--********************************************************************************************
PB_pulse: process (PBSwitch, PBSwitch_flop1, PBSwitch_flop2)
begin
PB_PulseOut <= PBSwitch_flop2 and (not PBSwitch_flop1);
end process;
PB_two_pulse: process (PBSwitch_two, PBSwitch_two_flop1, PBSwitch_two_flop2)
begin
PB_two_PulseOut <= PBSwitch_two_flop2 and (not PBSwitch_two_flop1);
end process;
PB_debounce: process (clk, reset_b)
begin
if reset_b = '0' then
count <= (others => '0');
elsif rising_edge (clk) then
if (PBSwitch_flop1 = '0') then
if (PB_PulseOut = '1') then
count <= debounce_count;
else
if (count = mincount) then
count <= (others => '0');
else
count <= count - 1;
end if;
end if;
elsif(PBSwitch_two_flop1 = '0')then
if(PB_two_PulseOut = '1')then
count <= debounce_count;
else
if( count = mincount) then
count <= (others => '0');
else
count <= count -1;
end if;
end if;
else
count <= (others => '0');
end if;
end if;
end process;
PB_out: process (count)
begin
case count is
when "0000000000000001" => if (PB_PulseOut = '0') then
PB_valid <= '1';
else
if(PB_two_PulseOut = '0')then
PB_two_valid <= '1';
else
PB_two_valid <= '0';
end if;
PB_valid <= '0';
end if;
when others => PB_valid <= '0';
PB_two_valid <= '0';
end case;
end process;
--********************************************************************************************
-- Dip Switches are floped twice
--********************************************************************************************
Dip_flop: process (clk, reset_b)
begin
if reset_b = '0' then
DipSwitch_flop1 <= '0';
DipSwitch_flop2 <= '0';
elsif rising_edge (clk) then
DipSwitch_flop1 <= DipSwitch;
DipSwitch_flop2 <= DipSwitch_flop1;
end if;
end process;
--********************************************************************************************
-- 4-bit counter which is connected to LEDs
-- The counter will increment upon pressing Push Button Switch each time.
--********************************************************************************************
Led_counter: process (clk, reset_b)
begin
if reset_b = '0' then
Led <= "0000";
elsif rising_edge (clk) then
if (PB_valid = '1') then
Led <= Led - 1;
elsif( PB_two_valid = '1') then
Led <= Led + 1;
elsif(DipSwitch_flop2 = '1') then
Led <= "0000";
else
Led <= Led;
end if;
end if;
end process;
--********************************************************************************************
-- On ESDK board, Leds are common anode. Therefore they are inverted here
--********************************************************************************************
Led_inv <= not std_logic_vector(Led);