我想用兩個按鈕遞增和遞減。該算法進展順利,但我有一個小問題。假設我正在遞增,當我嘗試遞減累加器時,它會再次遞增,只有在它開始後,它纔會開始遞減。如果我先嚐試減少,也是一樣。如果是某人來幫助我,我會非常感激。VHDL-遞增和遞減按鈕
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use IEEE.std_logic_unsigned.all;
entity my_offset_controller is
port(clk : in std_logic;
input : in std_logic_vector(15 downto 0);
add_button : in std_logic;
sub_button : in std_logic;
output_res : out std_logic_vector(15 downto 0)
);
end my_offset_controller;
architecture Behavioral of my_offset_controller is
signal buttonState_up : std_logic:='0';
signal accumulator : std_logic_vector(15 downto 0);
signal lastButtonState_up : std_logic:='0';
signal buttonState_down : std_logic:='0';
signal lastButtonState_down : std_logic:='0';
signal buttonPushCounter : integer range 0 to 512 :=0;
process(clk)
begin
if rising_edge(clk) then
buttonState_up <= add_button;
buttonState_down <= sub_button;
if (buttonState_up /= lastButtonState_up) then
if (buttonState_up ='1') then
buttonPushCounter <= buttonPushCounter + 1;
accumulator <= std_logic_vector(to_unsigned(buttonPushCounter,accumulator'length));
end if;
lastButtonState_up <= buttonState_up;
elsif (buttonState_down /= lastButtonState_down) then
if (buttonState_down ='1') then
buttonPushCounter <= buttonPushCounter - 1;
accumulator <= std_logic_vector(to_unsigned(buttonPushCounter,accumulator'length));
end if;
lastButtonState_down <= buttonState_down;
end if;
end if;
end process;
output_res<= accumulator + input ;
這個特別的模塊,我用來控制我在vga屏幕上繪製的信號的偏移量。
如果您使'accumulator'具有'unsigned'類型,您將不需要所有這些尷尬的類型轉換。也不要'使用IEEE.std_logic_unsigned.all;',你需要的所有東西都在'numeric_std'中。 –
是的,你是對的。我會修改它。 –