在 VHDL 中生成第二个计数器

Generating second counter in VHDL

我是 VHDL 新手,正在尝试生成 1 秒计数器。为简单起见,我使用 10 Hz 的时钟频率。为此,我使用 clk 作为输入,使用 LED 作为输出。我的 VHDL 代码如下:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
USE ieee.std_logic_unsigned.ALL;
use ieee.numeric_std.all;

entity tick_counter is
    generic(FrequencyHz : integer := 10);
    Port ( clk : in  STD_LOGIC;
           led : out  STD_LOGIC);
end tick_counter;

architecture Behavioral of tick_counter is
    signal tick :integer;
    signal counter :integer;
begin
    process(clk, tick, counter)
    begin
        if rising_edge(clk) then
            if tick = FrequencyHz - 1 then
                tick <= 0;
                counter <= counter + 1;
            else
                tick <= tick + 1;
            end if;
        end if;
    end process;

    led <= '1' when counter = 3 else '0';
end Behavioral;

我尝试以这种方式编写代码,以便在三秒后,LED 亮起。我的测试平台代码如下:

LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.ALL;
use ieee.numeric_std.all;
 
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--USE ieee.numeric_std.ALL;
 
ENTITY tick_counter_tb IS
END tick_counter_tb;
 
ARCHITECTURE behavior OF tick_counter_tb IS 
 
    -- Component Declaration for the Unit Under Test (UUT)
     -- We're slowing down the clock to speed up simulation time
    constant FrequencyHz : integer := 10; -- 10 Hz
    constant clk_period  : time    := 1000 ms / FrequencyHz;
 
    COMPONENT tick_counter
    PORT( clk : IN  std_logic;
          led : OUT  std_logic);
    END COMPONENT;

    --Inputs
    signal clk : std_logic := '0';

    --Outputs
    signal led : std_logic;

BEGIN
 
    -- Instantiate the Unit Under Test (UUT)
    uut : entity work.tick_counter
        generic map(FrequencyHz => FrequencyHz) 
        PORT MAP (clk => clk,
                  led => led);

    -- Clock process definitions
    clk_process :process
    begin
        clk <= '0';
        wait for clk_period/2;
        clk <= '1';
        wait for clk_period/2;
    end process;

    -- Stimulus process
    stim_proc: process
    begin       
        -- hold reset state for 100 ns.
        wait until rising_edge(clk);
        -- insert stimulus here 

        wait;
    end process;

END;

但在模拟结果中,我只看到如下所示的空白图(link 给定):

我不明白我在哪里犯了错误。任何帮助将不胜感激。

整数的默认初始值为 INTEGER'LOW(非常负的数)。

这意味着计数器在很长一段时间内不会变为 3(如 the busybee 所示)。

您可以约束 and/or 为 tick 提供初始值。 counter 似乎意味着模数计数器范围为 0 到 3。它的模数可以与使用 FrequencyHZtick 相同地传递。另请注意,任何整数计数器都需要显式翻转,如果加法结果不在 INTEGER'LOW 到 INTEGER'HIGH 范围内或约束范围内,则会出错。

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- USE ieee.std_logic_unsigned.ALL;
-- use ieee.numeric_std.all;

entity tick_counter is
    generic(FrequencyHz : integer := 10);
    Port ( clk : in  STD_LOGIC;
           led : out  STD_LOGIC);
end tick_counter;

architecture Behavioral of tick_counter is
    -- Initial value of integers is INTEGER'LOW (a large negative number)
    signal tick:    integer range 0 to FrequencyHz - 1 := 0;
    signal counter: integer := 0;   -- ADDED default initial value
begin
    process(clk, tick, counter)
    begin
        if rising_edge(clk) then
            if tick = FrequencyHz - 1 then
                tick <= 0;
                if counter = 3 then  -- ADDED MODULUS 4 test for counter
                    counter <= 0;
                else
                    counter <= counter + 1;
                end if;
            else
                tick <= tick + 1;
            end if;
        end if;
    end process;

    led <= '1' when counter = 3 else '0';
end Behavioral;

通过更改,模拟将导致 led 每 4 秒有 1 秒为“1”。