2013-01-24 31 views
2

我對VHDL非常陌生,我正試圖弄清楚如何在Altera Cyclone II上做相當基本的工作。 FPGA有四個按鈕 - 其中兩個需要編程以增加和減少所選擇的寄存器(0-F),並且需要對這兩個按鈕進行編程以增加和減少將要進入的值(從00到FF)該登記冊。這是我到目前爲止:VHDL - 在按鈕事件上遞增寄存器值

entity raminfr is 
    port (
     clk : in std_logic; 
     we : in std_logic; 
     a : in unsigned(3 downto 0); 
     di : in unsigned(7 downto 0); 
     do : out unsigned(7 downto 0) 
    ); 
end raminfr; 

architecture rtl of raminfr is 
type ram_type is array (0 to 15) of unsigned(7 downto 0); 
signal RAM : ram_type; 
signal read_a : unsigned(3 downto 0); 

begin 

process (clk) 
begin 
    if rising_edge(clk) then 
     if we = '1' then 
      RAM(to_integer(a)) <= di; 
     end if; 
     read_a <= a; 
    end if; 
end process; 

do <= RAM(to_integer(read_a)); 

end rtl; 

有人可以提供一些基本的示例代碼,如何去編程按鈕?

回答

8

你可以做一個簡單的邊緣檢測時鐘process,然後對上升沿作出反應。例如:

signal lastButtonState : std_logic := '0'; 

process(clk) 
begin 
    if(rising_edge(clk)) then 
    if(buttonState = '1' and lastButtonState = '0') then  --assuming active-high 
     --Rising edge - do some work... 
    end if; 
    lastButtonState <= buttonState; 
    end if; 
end process; 

爲了讓一切正常工作,您需要確保按鈕按某種方式消除。許多開發板都有一個硬件RC電路來實現這個功能,否則你需要在你的代碼中做這件事(儘管這並不困難 - 網上應該有很多例子)。