【问题标题】:1Hz clock for a D FlipFlop VHDLD FlipFlop VHDL 的 1Hz 时钟
【发布时间】:2013-02-03 07:30:32
【问题描述】:

我正在尝试为 VHDL 中的 D 触发器实现 1hz 时钟。

下面是我的代码:

entity d_flip_flop is
    Port ( clk : in  STD_LOGIC;
           D : in  STD_LOGIC;
           Q : out  STD_LOGIC);
end d_flip_flop;

architecture Behavioral of d_flip_flop is
signal clk_div: std_logic; --divided clock
begin

--process to divide clock
clk_divider: process(clk) --clk is the clock port
variable clk_count: std_logic_vector(25 downto 0) := (others => '0');
begin
    if clk'event and clk = '1' then
        clk_count <= clk_count+1;
        clk_div <= clk_count(25);
    end if;
end process;

--main process  
main:process(clk_div)
    begin
        if clk'event and clk = '1' then
            Q <= D;
        end if;
end process;


end Behavioral;

但是当我尝试编译时,却报如下错误:

ERROR:HDLParsers:808 - "F:/EE4218/XQ/d_flip_flop.vhd" 第 47 行。+ 可以 在这种情况下没有这样的操作数。

我已经检查了几个参考的语法,并没有发现任何问题。谁能指出错误的原因?

提前致谢!

【问题讨论】:

标签: syntax compiler-errors vhdl


【解决方案1】:

clk_count 用于表示一个数字,而不是一袋位。

所以使用类型系统而不是与之抗衡,并将其声明为数字或至少是某种数字类型。

用于此目的的最佳工具是 numeric_std.unsigned,因为您需要从中提取一些信息。

所以在library ieee;子句之后添加use ieee.numeric_std.all;,声明为

variable clk_count: unsigned(25 downto 0) := (others => '0');

你就完成了。

【讨论】:

    【解决方案2】:

    无论如何,对于二次幂,Brian 给出了最好的答案。可以说,对于其他环绕值,您还应该将 integer 用于 clock_count 并将其包裹起来:

    signal clk_div : std_logic := '0';
    
    clk_divider: process(clk) --clk is the clock port
    subtype t_clk_count: integer range 0 to 12345678; -- for example
    variable clk_count: t_clk_count := 0;
    begin
        if clk'event and clk = '1' then
            if clk_count+1 >= t_clk_count'high then
               clk_div <= not clk_div;
               clk_count <= 0;
            else
                clk_count <= clk_count+1;
            end if;
        end if;
    end process;
    

    【讨论】:

    • 我的偏好也是整数(所以我说“将其声明为数字或......”),即使您必须明确编码环绕行为。但是对于这个需要 2 次幂溢出的应用程序,我选择了更简单的答案。 +1 加倍努力。
    【解决方案3】:

    clk_divider过程中修改以下行:

    clk_count <= clk_count +1;
    

    clk_count := std_logic_vector(UNSIGNED(clk_count) + 1);
    

    这是因为 clk_count 被定义为“std_logic_vector”类型的变量

    【讨论】:

    • 他还需要在“library ieee”后面加上“use ieee.numeric_std.all”;
    • 这是一个快速破解(或 Verilog 用户)修复恕我直言
    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多