Reputation: 456
New to VHDL, familiarizing myself with everything.
I got my FPGA to turn on an LED when the button is pressed (code below), but the button has to be held down for the LED to stay on. I would like the LED to turn and stay on when the button is pushed and released (and turned off when pressed again) but I'm confused on how this is done.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity ButtonLED is Port (BUTTON: in STD_LOGIC:='1';
LED : out STD_LOGIC:='0');
end ButtonLED;
architecture Behavioral of ButtonLED is
begin
LED <= not BUTTON;
end Behavioral;
Upvotes: 3
Views: 5079
Reputation: 918
(Warning: I am not giving you the answer. I am only addressing your question on "how it is done")
The statement LED <= not BUTTON
defines that the LED
is directly connected to the BUTTON
through an inverting element. Therefore, your LED will always follow the opposite current state of the BUTTON
. If BUTTON
is in high logic level, then LED
will be set to a low logic level, and vice versa.
I would like the LED to turn and stay on when the button is pushed and released (and turned off when pressed again) but I'm confused on how this is done.
For implementing this feature you must be capable of (1) detecting whenever the button is pressed and released, and (2) to "save" the current state of your LED. Detecting the button "movement" is simply detecting a change in state from high to low, and low to high. As mentioned in the comments it is also important to insert a de-bouncer mechanism between your edge detector and the button. Since FPGA buttons are very sensitive, the de-bouncer will ensure that you won't interpret glitches/noises as an actual press. I advise you to check it out yourself, implementation a version with and without the de-bouncer. Depending on how you press your button you may see the LED toggling multiple times when your hardware doesn't filter the input.
Once you know how to detect when the button was pressed and released, you have to simply add a memory element that toggles every time your condition is met. Now, instead of using LED <= not BUTTON
, you'll have an internal variable that will control the LED
behavior (i.e., LED <= led_state
). This internal variable can be controlled through a process
that is sensitive to your edge detection "module". Moreover, this variable will ensure your LED
state only changes when your condition is met (i.e., pressing and releasing the button), instead of following the BUTTON inverse state.
Hope this gives you a general overview of your solution.
Upvotes: 4