创建模 16 计数器时的 VHDL 时钟问题

VHDL Clock problem while creating modulo 16 counter

我使用 basys3 板创建了这个简单的 mod16 计数器,但我的时钟有些不对劲。代码本身确实有效,但是一次计数(从“1”变为“2”等)持续 40 秒,而不是 1 秒!我试图将 "clk_vector" if 条件降低到 1,但它也没有帮助。

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity mod_16_k is
    Port ( switch : in STD_LOGIC_VECTOR (3 downto 0);
           CLK1 : in STD_LOGIC;
           reset : in STD_LOGIC;
           led : out STD_LOGIC_VECTOR (15 downto 0));
end mod_16_k;
architecture Behavioral of mod_16_k is

signal      clk_vector   :integer;
signal      clk_vec2      :std_logic_vector(15 downto 0); 

begin
zegar_wew : process(CLK1)
begin

if(CLK1'event and CLK1 = '1')  then
    clk_vector <= clk_vector + 1;
        if(clk_vector = 100000000) then
            clk_vec2 <= std_logic_vector(unsigned(clk_vec2)  + 1);
        end if;
end if;
end process;
led <= clk_vec2;
end Behavioral;

时钟的 .XDC 线是:

如果我们查看 basys3 数据表,时钟连接到 "W5" 端口。

你知道这里可能有什么问题吗?它可能与检测 clk 的上升沿有关,但是所有变化(从 1 到 2 等)持续约 40 秒。

这是因为您在达到 1 秒后忘记重置 clk_vector。因为是整数,是32位的,所以会算2^32,而不是100000000。

这应该有效:

If(CLK1'event and CLK1 = '1')  then
        if(clk_vector = 100000000-1) then
            clk_vector <= 0;
            clk_vec2 <= std_logic_vector(unsigned(clk_vec2)  + 1);
        else
                clk_vector <= clk_vector + 1;
        end if;
end if;

另外,请注意,要计数 1 秒,您需要计数到 100000000-1,我们从零开始计数!