繁体   English   中英

Quartus II:简单的计数器,但行为怪异

[英]Quartus II : simple counter but weird behaviour

首先,很抱歉打扰我的菜鸟问题,但是我对我的(ModelSim模拟)电路发生的事情一无所知。

这是我的代码,可能很简单:

LIBRARY ieee;
    use ieee.std_logic_1164.all;
    use ieee.numeric_std.all;

ENTITY Counter IS
    PORT(
    enable  : in std_logic;
    clk     : in std_logic;
    count       : out integer range 0 to 255);
END Counter;

ARCHITECTURE LogicFunction OF Counter IS

    signal count_i : integer range 0 to 255;

begin

    cnt : process(clk, enable, count_i)
    begin
        count <= count_i;
        if (enable = '0') then
            count_i <= 0;
        else
            count_i <= count_i + 1;
        end if;
    end process;

end LogicFunction;

我的问题是:当我使用ModelSim通过时钟信号执行时序仿真时,“启用”首先是“ 0”,然后是“ 1”,输出(“计数”)始终保持为零。 我尝试了很多不同的方法,例如将“ count”设置为向量,进行各种类型的转换,但是仍然保持不变。

增量“ count_i <= count_i + 1;” 似乎是问题所在:我尝试用“ count_i <= 55”之类的东西替换它,然后输出更改(在上一示例中为“ 55”)。

我在该网页上的代码中看到了完全相同的增量,例如: http : //surf-vhdl.com/how-to-connect-serial-adc-fpga/我创建了一个项目,并对其进行了仿真。 .. 有用 ! 我确实不了解那个家伙所做的我没有做过的事情,只是我的代码中不需要一堆“如果”。

任何帮助将不胜感激,我花了3个小时的尝试和错误...

提前感谢!

除了不使用时钟沿来增加i_count之外,您还使用enable作为清除对象,因为它既在敏感性列表中,又在if语句条件下首先遇到。

library ieee;
use ieee.std_logic_1164.all;
-- use ieee.numeric_std.all;

entity counter is
    port(
    enable  : in std_logic;
    clk     : in std_logic;
    count       : out integer range 0 to 255);
end counter;


architecture logicfunction of counter is

    signal count_i : integer range 0 to 255;

begin

    cnt : process (clk) -- (clk, enable, count_i)
    begin
        -- count <= count_i;   -- MOVED 
        -- if (enable = '0') then  -- REWRITTEN
        --     count_i <= 0;
        -- else
        --     count_i <= count_i + 1;
        -- end if;
        if rising_edge(clk) then
            if enable = '1' then
                count_i <= count_i + 1;
            end if;
        end if;
    end process;

    count <= count_i;  -- MOVED TO HERE

end architecture logicfunction;

您的代码已修改为使用clk的上升沿,并要求在i_count递增之前使能enable ='1'。 引用软件包numeric_std的多余use子句已被注释掉。 您要执行的唯一数字运算是在整数上,并且这些运算符在软件包标准中已预定义。

请注意,如果if语句不用括号括起来,则替换该语句。 这不是编程语言,不需要它们。

计数分配移至并发信号分配。 这样就无需在灵敏度列表中仅具有i_count即可更新计数。

放入测试台以完成一个Miminal完整且可验证的示例

library ieee;
use ieee.std_logic_1164.all;

entity counter_tb is
end entity;

architecture foo of counter_tb is
    signal enable:  std_logic := '0';
    signal clk:     std_logic := '0';
    signal count:   integer range 0 to 255;
begin
DUT:
    entity work.counter
        port map (
            enable => enable,
            clk => clk,
            count => count
        );
CLOCK:
    process
    begin
        wait for 5 ns;  -- 1/2 clock period
        clk <= not clk;
        if now > 540 ns then
            wait;
        end if;
    end process;
STIMULUS:
    process
    begin
        wait for 30 ns;
        enable <= '1';
        wait for 60 ns;
        enable <= '0';
        wait for 30 ns;
        enable <= '1';
        wait;

    end process;
end architecture;

这给出了:

counter_tb.png

这表明当enable为0时计数器不计数,enable = 0也不会重置i_count的值。

Quartus II手册第1卷“设计和综合”没有给出使用时钟沿和没有异步清除或加载信号的使能的示例。

这里的秘密是使用时钟沿指定的if语句条件内的任何内容都将与时钟同步。 外部的任何条件都是异步的。

符合合成要求的顺序逻辑的形式来自现已撤销的IEEE Std 1076.6-2004 VHDL寄存器传输级别(RTL)合成的IEEE标准。 使用这些行为描述可以保证您可以通过与仿真匹配的综合来生产硬件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM