不要分别算vhdl
Dont count respectively vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity A is
Port (
clk : in STD_LOGIC;
reset: in std_logic; -- reset input
counter: out std_logic_vector(3 downto 0) := "0000"-- output 4-bit counter
);
end A;
architecture Behavioral of A is
signal counter_up: std_logic_vector(3 downto 0) := "0000";
signal countOf : integer := -1;
signal count : integer := 0;
signal temp : std_logic := '0';
begin
-- up counter
process(clk)
begin
if(clk'event and clk='1') then
count <=count+1;
if(count = 50000000) then
temp <= not temp;
count <=1;
end if;
end if;
end process;
process(temp,reset)
begin
if(reset = '1') then
countOf <= 0;
else
if(temp = '1') then
if( countOf < 4) then countOf <= countOf + 1;
else countOf <= 0; end if;
if(countOf = 0) then counter_up <= "0000"; end if;
if(countOf = 1) then counter_up <= "0001"; end if;
if(countOf = 2) then counter_up <= "0010"; end if;
if(countOf = 3) then counter_up <= "0100"; end if;
if(countOf = 4) then counter_up <= "1000"; end if;
end if;
end if;
end process;
counter <= counter_up;
end Behavioral;
您好,我想制作一个从 0 到 4 的计数器。如您所见,我将我的时钟分频为 1 HZ,并且在临时和重置过程中,我分配了所有条件,但它并没有按照我想要的方式计算。这很奇怪。我该如何解决?提前致谢。
你的 process(temp, reset)
不能工作,因为你把它变成了组合,这不是你在这种情况下需要的。在模拟中工作正常,在硬件中根本不起作用。
你写的在硬件中产生了一个循环:countOf
的状态依赖于temp
的状态和[=12的状态=] 本身。由于您的进程不包含时钟,因此 countOf
的状态会在门延迟允许的情况下以最快的速度变化(如果此代码将完全正确合成)。
如果你的软件不错,它会给你这样的警告:
[Synth 8-614] signal 'countOf' is read in the process but is not in the sensitivity list
你应该永远不要忽略,因为在 99% 的情况下,这意味着你会得到不希望的行为 and/or 模拟不匹配。
您需要做的是 temp
您的时钟。它由时钟分频器生成,因此从逻辑上讲,您几乎肯定希望它也是一个时钟。如果将 if (temp = '1') then
替换为 if rising_edge(temp) then
,您的代码将按预期工作。
旁注:请删除 use IEEE.STD_LOGIC_ARITH.ALL;
和 use IEEE.STD_LOGIC_UNSIGNED.ALL;
。您的代码不仅根本不需要任何未签名的库,而且 IEEE.STD_LOGIC_ARITH
已 弃用,不应再使用 。已经很多年了。请改用 IEEE.NUMERIC_STD
。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity A is
Port (
clk : in STD_LOGIC;
reset: in std_logic; -- reset input
counter: out std_logic_vector(3 downto 0) := "0000"-- output 4-bit counter
);
end A;
architecture Behavioral of A is
signal counter_up: std_logic_vector(3 downto 0) := "0000";
signal countOf : integer := -1;
signal count : integer := 0;
signal temp : std_logic := '0';
begin
-- up counter
process(clk)
begin
if(clk'event and clk='1') then
count <=count+1;
if(count = 50000000) then
temp <= not temp;
count <=1;
end if;
end if;
end process;
process(temp,reset)
begin
if(reset = '1') then
countOf <= 0;
else
if(temp = '1') then
if( countOf < 4) then countOf <= countOf + 1;
else countOf <= 0; end if;
if(countOf = 0) then counter_up <= "0000"; end if;
if(countOf = 1) then counter_up <= "0001"; end if;
if(countOf = 2) then counter_up <= "0010"; end if;
if(countOf = 3) then counter_up <= "0100"; end if;
if(countOf = 4) then counter_up <= "1000"; end if;
end if;
end if;
end process;
counter <= counter_up;
end Behavioral;
您好,我想制作一个从 0 到 4 的计数器。如您所见,我将我的时钟分频为 1 HZ,并且在临时和重置过程中,我分配了所有条件,但它并没有按照我想要的方式计算。这很奇怪。我该如何解决?提前致谢。
你的 process(temp, reset)
不能工作,因为你把它变成了组合,这不是你在这种情况下需要的。在模拟中工作正常,在硬件中根本不起作用。
你写的在硬件中产生了一个循环:countOf
的状态依赖于temp
的状态和[=12的状态=] 本身。由于您的进程不包含时钟,因此 countOf
的状态会在门延迟允许的情况下以最快的速度变化(如果此代码将完全正确合成)。
如果你的软件不错,它会给你这样的警告:
[Synth 8-614] signal 'countOf' is read in the process but is not in the sensitivity list
你应该永远不要忽略,因为在 99% 的情况下,这意味着你会得到不希望的行为 and/or 模拟不匹配。
您需要做的是 temp
您的时钟。它由时钟分频器生成,因此从逻辑上讲,您几乎肯定希望它也是一个时钟。如果将 if (temp = '1') then
替换为 if rising_edge(temp) then
,您的代码将按预期工作。
旁注:请删除 use IEEE.STD_LOGIC_ARITH.ALL;
和 use IEEE.STD_LOGIC_UNSIGNED.ALL;
。您的代码不仅根本不需要任何未签名的库,而且 IEEE.STD_LOGIC_ARITH
已 弃用,不应再使用 。已经很多年了。请改用 IEEE.NUMERIC_STD
。