VHDL时钟LED序列

时间:2016-10-17 22:54:56

标签: vhdl clock led

这是我在这个网站上的第一篇文章, 我正在研究我的电子学位的VHDL,并且必须编写一个程序,在一个序列的每个时钟脉冲处改变LED序列。我想我已经破解了它,但这是我第一次使用VHDL语言,所以我不确定iv是否使用了最有效的方法。我的代码是。

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;


entity REG_LED is
 PORT(CLK:      IN  std_logic;              -- CLK input
     LEDS:      Out std_logic_vector (4 downto 0):= "11111"); -- initialise      output
End REG_LED;

ARCHITECTURE behavioral OF REG_LED IS
 SIGNAL Temp:   std_logic_vector (3 downto 0):= "0000"; -- initailise comparison signal
  BEGIN
    CLK_0_Process:  PROCESS (CLK)   -- begin 
    BEGIN 
    if Temp <= "0000" Then          -- State 0          
        if rising_edge(CLK) Then
            Temp <= "0001" ;
            LEDS <= "00001";    
        END IF;
     ELSIF Temp <= "0001" Then  -- State 1
        if rising_edge(CLK) Then
            Temp <= "0010" ;
            LEDS <= "00001";
     END IF;
     ELSIF Temp <= "0010" Then  -- State 2
        if rising_edge(CLK) Then
            Temp <= "0011" ;
            LEDS <= "11111";
     END IF;
     ELSIF Temp <= "0011" Then  -- State 3  
        if rising_edge(CLK) Then
            Temp <= "0100" ;
            LEDS <= "00000";
     END IF;
     ELSIF Temp <= "0100" Then  -- State 4
        if rising_edge(CLK) Then
            Temp <= "0101" ;
            LEDS <= "11111";
     END IF;
     ELSIF Temp <= "0101" Then  -- State 5
        if rising_edge(CLK) Then
            Temp <= "0110" ;
            LEDS <= "00100";
     END IF;
     ELSIF Temp <= "0110" Then  -- State 6
        if rising_edge(CLK) Then
            Temp <= "0111" ;
            LEDS <= "01010";
     END IF;
     ELSIF Temp <= "0111" Then  -- State 7
        if rising_edge(CLK) Then
            Temp <= "1000" ;
            LEDS <= "10001";
     END IF;
     ELSIF Temp <= "1000" Then  -- State 8
        if rising_edge(CLK) Then
                LEDS <= "10001";
     END IF;
    END IF;
    END PROCESS ;
END behavioral;

有人可以告诉我是否有其他途径错过了吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

嗯,我说这是一种效率低下的方法,但是,就像使用任何语言一样,你学到的越多,你能编写的代码就越有效。更重要的是,您的代码不是可合成的:您将无法直接从中生成硬件。

如果您希望代码可合成,则应该坚持使用模板。这是一个用于顺序逻辑的模板,没有异步复位,所有综合工具都应该理解:

process(CLK)  -- nothing else should go in the sensitivity list
begin
    -- never put anything here
    if rising_edge(CLK) then  -- or falling_edge(CLK)
        -- put the synchronous stuff here
        -- ie the stuff that happens on the rising or falling edge of the clock
    end if;
     -- never put anything here
end process;        

因此,您需要重构代码以适应此模板,即:

process(CLK)
begin
  if rising_edge(CLK) then
    if Temp <= "0000" Then          -- State 0          
      Temp <= "0001" ;
      LEDS <= "00001";    
    ELSIF Temp <= "0001" Then  -- State 1
      Temp <= "0010" ;
      LEDS <= "00001";
    ELSIF Temp <= "0010" Then  -- State 2

    -- etc etc

    end if;
  end if;
end process;

它看到Temp只是在计算,在代码中使用行Temp <= Temp + 1;并驱动它会更有效(在优雅的解决方案和更少的代码行方面) LEDS使用case语句发出信号(在单独的过程中)。但是,您必须了解类型转换以及numeric_std包和case声明,这些声明可能是您尚未了解的。