Skip to content

Instantly share code, notes, and snippets.

@Apocryphon-X
Last active December 18, 2024 21:38
Show Gist options
  • Save Apocryphon-X/6ad4b7c4dca0106bb79c8a0945f32cdf to your computer and use it in GitHub Desktop.
Save Apocryphon-X/6ad4b7c4dca0106bb79c8a0945f32cdf to your computer and use it in GitHub Desktop.
Every time the button is pressed, the chosen led changes from '0' to '1' or from '1' to '0' respectively. Button press takes effect only after 120μs of being held (assuming `clk` is assigned to a 100MHz clock).
-- * Author: Dante Mendoza Leyva
-- * Group: 4CV13
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity led_test is Port (
clk: in std_logic;
toggle_button: in std_logic;
target_led: out std_logic
);
end led_test;
architecture Behavioral of led_test is
signal BUTTON_MEMORY: std_logic := '0';
signal BUTTON_SENSIBILITY_COUNTER: integer range 0 to 120100 := 0;
signal button_pressed: std_logic := '0';
begin
process (clk, toggle_button)
begin
if (clk'event) and (clk = '1') then
-- Every time the button is pressed, the led changes
-- from '0' to '1' or from '1' to '0' respectively.
if (toggle_button = '1') then -- If there is a signal comming from our button
-- Increase sensibility counter if button_pressed has not been triggered yet
if (button_pressed = '0') and (BUTTON_SENSIBILITY_COUNTER < 120000) then
BUTTON_SENSIBILITY_COUNTER <= BUTTON_SENSIBILITY_COUNTER + 1;
end if;
-- If the sensibility counter has reached the trigger limit
-- then we set the 'button_pressed' signal to '1'
-- and reset the counter to 0.
if (BUTTON_SENSIBILITY_COUNTER >= 120000) then
if (button_pressed = '0') then
BUTTON_MEMORY <= not(BUTTON_MEMORY);
button_pressed <= '1';
end if;
end if;
else -- If we are not receiving any signal from our button
button_pressed <= '0';
BUTTON_SENSIBILITY_COUNTER <= 0;
end if;
target_led <= BUTTON_MEMORY;
end if;
end process;
end Behavioral;
set_property -dict { PACKAGE_PIN V14 IOSTANDARD LVCMOS33 } [get_ports {target_led}]
set_property -dict { PACKAGE_PIN U18 IOSTANDARD LVCMOS33 } [get_ports {toggle_button}]
set_property -dict { PACKAGE_PIN W5 IOSTANDARD LVCMOS33 } [get_ports clk]
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment