⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dip_pb_led.vhd

📁 用VHDL写的带有防抖动功能的四位计数器
💻 VHD
字号:
-------------------------------------------------------------------------------
--
--
--         Author:      System Level Solutions (I) Pvt. Ltd.
--
--         Title:       4-bit Counter 
--
--         Filename:    Dip_PB_Led.vhd
--
--         Description: 1 Push Button (PB) and 1 Dip Switch (DP)are inputs.
--			4 Leds (common anode) are outputs.
--			Upon pressing PB(and DP should be at logic 0), a 4-bit 
--                      counter will get incremented by 1.
--                      And 4 bits of the counter are assigned to each led. 
--			So each led will glow according to the counter value.
--                      On making DP to logic 1, it will reset the counter.
--
-------------------------------------------------------------------------------


library IEEE; 
use IEEE.std_logic_1164.all; 
use IEEE.std_logic_arith.all; 
  
-----------------------------ENTITY DECLARATION--------------------------------

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
         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 PB_PulseOut     : std_logic;
  signal PB_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) := "0101110111000000";-- 5DC0
--********************************************************************************************
-- Push Button Switches are floped twice  
--********************************************************************************************
  begin   
     PB_flop: process (clk, reset_b) 
                begin
                  if reset_b = '0' then
                    PBSwitch_flop1 <= '1';
                    PBSwitch_flop2 <= '1';
                  elsif rising_edge (clk) then  
                    PBSwitch_flop1 <= PBSwitch;
                    PBSwitch_flop2 <= PBSwitch_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;    
                        
--********************************************************************************************
-- Debounce Logic  
--******************************************************************************************** 
 
     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;  
                        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
                                                PB_valid <= '0';
                                              end if;  
                   when others             => PB_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 (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);
      
 
 
 end rtl;   
                    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -