如何在VHDL中编写一个通用计数器?

时间:2018-08-14 16:23:47

标签: generics count vhdl clock

我想编写一个随时钟上升而工作的同步计数器,为此计数器我有4个输入: clk,重置,启用,启用2。 我的计数器仅在enable ='1'和enable2 ='1'时计数 当reset ='1'时,计数将返回零。 现在我的问题是输出向量是泛型的。这意味着我们可以更改它,例如,如果计数器是8位计数器,然后我将其更改为16,它仍应继续工作(这就是为什么我需要确保如果计数器达到最大值,则需要返回到零..例如:如果counte ='11111111',然后我加1,我将得到count ='00000000'。 任何帮助pf如何使代码通用? 我已经尝试过的内容(没有geberic部分):

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity Counter_VHDL is
   port( enable : in std_logic;
     enable2 : in std_logic;
     clk: in std_logic;
     reset: in std_logic;    
     Output: out std_logic_vector(0 to 7)); -- this is wrong
end Counter_VHDL;

architecture Behavioral of Counter_VHDL is
   signal temp: std_logic_vector(0 to 7);
begin   process(clk,reset)
   begin
      if Reset='1' then
         temp <= "00000000";
      elsif(rising_edge(clk)) then
         if (enable='1' AND enable2='1')  then
            if temp="11111111" then
               temp<="00000000";
            else
               temp <= temp + 1;
            end if;
         end if;
      end if;
   end process;
   Output <= temp;
end Behavioral;

1 个答案:

答案 0 :(得分:-1)

首先,使用 std_logic_vector ,您无需担心:

   if temp = "11111111" then
       temp <= "00000000";
   ...

这是因为 std_logic_vector 会自动溢出,如果您将11添加到11111111,则会返回到00000000。

第二,为了重置信号而不必写很多零,可以替换:

    temp <= "00000000";

并写:

    temp <= (others => '0');

相反。

这样,当您更改“输出”和“温度”的大小时,计数器将起作用,而无需重新排列代码。