Altera_Forum
Honored Contributor
15 years agoDE2 push buttons question
I have a simple counter in VHDL for DE2-115 (should work with any DE2). KEY0 starts counting, KEY1 stops, KEY2 resets. The output is displayed on 7-seg LEDs HEX0 - HEX3 in hexa digits (to be simple manner).
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity led_counter_ent is
port ( CLOCK_50: in std_logic;
-- LEDR : out std_logic_vector(15 downto 0);
HEX0 : out std_logic_vector(6 downto 0);
HEX1 : out std_logic_vector(6 downto 0);
HEX2 : out std_logic_vector(6 downto 0);
HEX3 : out std_logic_vector(6 downto 0);
HEX4 : out std_logic_vector(6 downto 0) := (others => '1');
HEX5 : out std_logic_vector(6 downto 0) := (others => '1');
HEX6 : out std_logic_vector(6 downto 0) := (others => '1');
HEX7 : out std_logic_vector(6 downto 0) := (others => '1');
KEY : in std_logic_vector(3 downto 0);
LEDG : out std_logic_vector(1 downto 0));
end led_counter_ent;
architecture behavior of led_counter_ent is
signal count : std_logic_vector(24 downto 0) := (others => '0');
signal beat_count: std_logic_vector(15 downto 0) := (others => '0');
signal run : std_logic := '0';
signal reset : std_logic := '0';
procedure segment_proced (signal data : in std_logic_vector(3 downto 0); signal digit : out std_logic_vector(6 downto 0)) is
begin
case data is
when "0000" => digit <= "1000000"; -- 0
when "0001" => digit <= "1111001"; -- 1
when "0010" => digit <= "0100100"; -- 2
when "0011" => digit <= "0110000"; -- 3
when "0100" => digit <= "0011001"; -- 4
when "0101" => digit <= "0010010"; -- 5
when "0110" => digit <= "0000010"; -- 6
when "0111" => digit <= "1111000"; -- 7
when "1000" => digit <= "0000000"; -- 8
when "1001" => digit <= "0010000"; -- 9
when "1010" => digit <= "0001000"; -- A
when "1011" => digit <= "0000011"; -- b
when "1100" => digit <= "1000110"; -- C
when "1101" => digit <= "0100001"; -- d
when "1110" => digit <= "0000110"; -- E
when "1111" => digit <= "0001110"; -- F
when others => digit <= "1111111"; -- switch off
end case;
end segment_proced;
begin
buttons_process:process (key(0), key(1), key(2), key(3))
begin
-- signals run and reset are set due the first expression in the process - why ??
if (key(1) = '0') then
run <= '0';
hex5 <= "1000000"; -- 0
else
hex5 <= "1111001"; -- 1
end if;
if (key(0) = '1') then
hex4 <= "1111001"; -- 1
else
hex4 <= "1000000"; -- 0
reset <= '0';
run <= '1';
--run <= not run; -- switch ON/OFF does't work properly - why ??
end if;
if (key(3) = '0') then
hex7 <= "1000000"; -- 0
run <= not run; -- switch ON/OFF does't work properly - why ??
else
hex7 <= "1111001"; -- 1
end if;
if (key(2) = '0') then
hex6 <= "1000000"; -- 0
reset <= '1';
else
hex6 <= "1111001"; -- 1
end if;
end process;
counter_process:process (clock_50)
begin
if clock_50 = '1' and clock_50'event then
count <= count + 1;
--if count = "1111111111001011010000000" then -- 33.527.424
--if count = "111111111001011010000000" then -- 16.750.208
--if count = "110110001001011010000000" then -- 14.194.304
--if count = "100110001001011010000000" then -- 10.000.000 - 200ms
if count = "10011000100101101000000" then -- 5.000.000 - 100ms
--if count = "111101000010010000000" then -- 2.000.000 - 40ms
--if count = "1111010000100100000" then -- 500.000 - 10ms
--if count = "1111010000100011111" then -- 499.999 - 10ms proper
count <= (others => '0');
-- tik <= not tik;
if (run = '1') then
beat_count <= beat_count + 1;
end if;
if (reset = '1') then
beat_count <= (others => '0');
end if;
end if;
end if;
end process;
segment_proced(beat_count(3 downto 0), hex0);
segment_proced(beat_count(7 downto 4), hex1);
segment_proced(beat_count(11 downto 8), hex2);
segment_proced(beat_count(15 downto 12), hex3);
-- ledr <= beat_count;
ledg(0) <= run;
ledg(1) <= reset;
end behavior;I want to have only one button to start/stop the counter (not KEY0 for starting and KEY1 for stopping). But if I use the solution as it is used with KEY3, start/stop is not working properly ('run' is signal to indicate whether to count). run <= not run; Counting will not start/stop reliably with every button push which is not clear to me. I appreciate any advise, thank you.