当计数器达到一定值时如何重置计数器?

时间:2019-01-23 10:33:21

标签: vhdl counter

我有这段代码,它计算两个异步(和随机)触发事件之间的时钟周期。如果计数器达到1200d并重置为零,我希望使计数器停止。 提供该代码时,我没有尝试重置计数器。

我试图在时钟过程中添加一个(如果计数器达到1200,则计数器为零)语句,但是没有结果。

我模拟两种情况。第一:我有一对触发事件,它们之间有85个时钟周期,第二对是触发事件,它们之间有1500个时钟周期。在第二种情况下,我希望计数器在达到1200时停止并回到0。

我很抱歉,但还无法发布模拟图片 预先感谢您的帮助。

模块

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

entity main is
  Port ( clk : in  STD_LOGIC;
  trigger : in  STD_LOGIC;
  output : out  STD_LOGIC_VECTOR(11 DOWNTO 0)  );
end main;
architecture Behavioral of main is
signal counter : STD_LOGIC_VECTOR(11 DOWNTO 0) := "000000000000";
signal enable : STD_LOGIC:='0'; 
begin
  trigger_proc : process(trigger)  
    variable state : std_logic := '0';
    begin   
      if(falling_edge(trigger))then
        if(state = '0')then
          enable <= '1'; 
          state := '1';
        elsif(state = '1')then
          enable <= '0';
          state := '0';
        end if;
      end if;
    end process;
  counter_proc : process(clk)
    begin
      if(rising_edge(clk))then
        if(enable =  '1')then
          counter <= counter + "1"; 
        elsif(enable = '0'  )then
          counter <= "000000000000";
        end if; 
      end if;  
    end process;
  output <= counter;
end Behavioral;

测试台

LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY test IS
END test;
ARCHITECTURE behavior OF test IS 
  COMPONENT main
    PORT(
      clk : IN  std_logic;
      trigger : IN  std_logic;
      output : OUT  std_logic_vector(11 downto 0)
    );
  END COMPONENT;
  signal clk : std_logic := '0';
  signal trigger : std_logic := '1';
  signal output : std_logic_vector(11 downto 0);
  constant clk_period : time := 25 ns;
  BEGIN
    kyklwma: main PORT MAP (
      clk => clk,
      trigger => trigger,
      output => output 
    );
    clk_process :process
      begin
        clk <= '0';
        wait for clk_period/2;
        clk <= '1';
        wait for clk_period/2;
    end process;
    trigger_stim: process 
      begin      
        wait for 100 ns;    
        trigger <= '0';-- first trigger event with  counter <1200
        wait for clk_period;
        trigger <= '1';
        wait for clk_period*85;
        trigger <= '0';--second trigger event with  counter <1200
        wait for clk_period;
        trigger <= '1';
        wait for 10000 ns;
        trigger <= '0';-- first trigger event with counter > 1200
        wait for clk_period;
        trigger <= '1';
        wait for clk_period*1500;
        trigger <= '0';--second   trigger event with counter > 1200
        wait for clk_period;
        trigger <= '1';
        wait;
    end process;
END;

0 个答案:

没有答案