2017-05-29 217 views
0

我想用兩個按鈕遞增和遞減。該算法進展順利,但我有一個小問題。假設我正在遞增,當我嘗試遞減累加器時,它會再次遞增,只有在它開始後,它纔會開始遞減。如果我先嚐試減少,也是一樣。如果是某人來幫助我,我會非常感激。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屏幕上繪製的信號的偏移量。

+0

如果您使'accumulator'具有'unsigned'類型,您將不需要所有這些尷尬的類型轉換。也不要'使用IEEE.std_logic_unsigned.all;',你需要的所有東西都在'numeric_std'中。 –

+0

是的,你是對的。我會修改它。 –

回答

0

沒有更多的信息很難幫助你。您應該提供一個帶有計時器的測試臺以使其更容易。然而,通過觀察你的過程中,我要說的是,問題來自於以下幾行:

buttonPushCounter <= buttonPushCounter + 1; 
accumulator  <= std_logic_vector(to_unsigned(buttonPushCounter,accumulator'length)); 

你做了什麼有在你更新accumulator同時增量buttonPushCounter。取決於最後的事件,這種方式buttonPushCounter將總是被移位+1或-1。

我可以推薦的是在每個時鐘週期更新accumulator,而不是每次發生事件。例如像這樣:

if rising_edge(clk) then 
    accumulator <= std_logic_vector(to_unsigned(buttonPushCounter,accumulator'length)); 
    ... 
+0

我很抱歉沒有提供更多信息,但是你知道我的代碼中有什麼問題。現在它運作完美。非常感謝你 :) –