Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

Blinking LED

Hi,

I am trying to make a design that makes the LED blink 4 times when iKEY is pressed. Right now it blinks non stop when iKEY is pressed and stops blinking when iKEY is pressed again. What changes should I make to make that happen ?

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_arith.all;

use ieee.numeric_std.all;

use ieee.std_logic_unsigned.all;

entity pract is

port (

iCLK_50 : in std_logic;

iKEY : in std_logic_vector(0 downto 0);

oLEDG: out std_logic_vector(2 downto 0)

);

end entity pract;

architecture behav of pract is

signal sel : std_logic_vector(0 downto 0) := "0";

signal newCLK : std_logic_vector (0 downto 0) := "1";

signal counting: integer := 0;

signal cycles : integer :=0 ;

type state_type is (idle, s0, s1, s2, s3);

signal current_state, next_state : state_type;

--signal

begin

-------------------------------------------------------

-----------clk code------------------------------------

clocking : process(iCLK_50, iKEY)

begin

if(rising_edge(iCLK_50)) then

counting <= counting + 1;

if (counting >= 100000000) then

counting <= 0;

end if;

end if;

end process;

newclocking: process(counting)

begin

if(counting <= 50000000) then

newCLK <= "0";

else

newCLK <= "1";

end if;

end process;

-------------end clk code------------------------------

-------------------------------------------------------

p1 : process(iKEY)

begin

if(rising_edge(iKEY(0))) then

sel <= sel + "1";

end if;

end process;

p2 : process(sel)

begin

case sel is

when "1"=>

-- go to s0

case newCLK is

when "0" =>

oLEDG <= "000";

when "1" =>

oLEDG <= "111";

end case;

when "0"=>

oLEDG <= "000";

-- restart the counter or something

end case;

end process;

end behav;

Thanks

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    First as said previously in your other thread, never use use ieee.numeric_std.all and ieee.std_logic_unsigned.all at the same time, you'll run into problems. and while you are at it, forget about the non standard ieee.std_logic_unsigned, just use ieee.numeric_std.all and its signed and unsigned types.

    Then you shouldn't use the key signal as a clock. You will run into synchronization problems and will be very sensitive to glitches problems if the key isn't debounced. You should use your iclk50 as clock, and for example compare the KEY signal to its previous value to detect a key press.

    For the rest, create an additional signal that will be a counter, set it to 4 when the key is pressed, decrease it by 1 each time you make the LED blink and stop when you reach 0.