2014-01-17 3 views
0

Как изменить этот код, чтобы заставить его работать с несколькими (2, 3 или 4) кнопками?Несколько кнопок

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; 

Я хочу, чтобы несколько кнопок работали и выполняли некоторые работы по нажатию 'state'. Эта часть кода работает только для 1 кнопки.

Благодаря

ответ

1

std_logic_vector типа может быть использован для нескольких кнопок. Код может выглядеть :

constant BUTTONS  : positive := 4;    -- Number of buttons 
subtype buttons_t  is std_logic_vector(1 to BUTTONS);      -- Type for buttons 
signal buttonsState  : buttons_t;      -- Input from buttons (remember to debounce) 
signal lastButtonsState : buttons_t := (others => '0'); -- Last state 
... -- End of declarations, and start of concurrent code (processes etc.) 
process (clk) 
    variable buttonsRise_v : buttons_t; -- Support variable to make writing easier 
begin 
    if rising_edge(clk) then 
    buttonsRise_v := buttonsState and (not lastButtonsState); -- Make '1' for each button going 0 to 1 
    if buttonsRise_v(1) = '1' then -- Detect button 1 going 0 to 1 
     -- Do some work... 
    end if; 
    -- More code reacting on buttons... 
    lastButtonsState <= buttonsState; 
    end if; 
end process; 
+0

Благодарим за быстрый ответ. Я попробую ваше решение скоро :) – lbartolic

Смежные вопросы