VHDL时钟分频器:反占空比

Fat*_*ima 1 counter frequency clock vhdl

我是VHDL的新手.我得到了关于如何从24 MHz的输入时钟信号产生1Hz(50%占空比)的时钟信号的代码.我有一些问题需要进一步澄清.

  1. 如何选择反限制?在下面的例子中,12000000.如果我想生成8Hz时钟信号,这个限制是什么.
  2. 如果我想将占空比改为80%,应如何调整代码?

    library IEEE;
    use IEEE.STD_LOGIC_1164.ALL;
    use IEEE.STD_LOGIC_ARITH.ALL;
    use IEEE.STD_LOGIC_UNSIGNED.ALL;
    entity clock is
       port ( CLKin: in std_logic;
              reset: in std_logic;
              CLKout: out std_logic);
    end clock;
    architecture arch of clock is
    
      signal counter: integer:=0;
      signal temp : std_logic := '1';
    begin
    process(CLKin,counter,reset)
    begin
       if(reset='0') then counter<=0; temp<='1';
        elsif(CLKin'event and CLKin='1') then counter <=counter+1;
         if (counter = 12000000) then temp <= NOT temp; counter<=0;
         end if;
        end if;
       CLKout <= temp;
    end process;
    end arch;
    
    Run Code Online (Sandbox Code Playgroud)

ver*_*ete 8

您想将24MHz的时钟(24000000 Hz)分频为1 Hz.因此,您创建一个计数器,计算CLKin的每个上升沿(24 MHz).在24000000/2 = 12000000计数之后,您处于中间位置.这就是你改变输出信号(temp <= not temp)的电平值的地方.因为你的规格说它必须是50%的占空比.这样做时,您也会从头开始重新计数.

对于8MHz,你有一个计数器(24000000/8)/ 2 = 1500000.

只是一个小小的评论:最好使用ieee.numeric_std库iso ieee_logic_arith和theieee.std_logic_unsigned

注意:代码首先分配给信号temp.然后temp输出信号clkout.这背后的原因是在VHDL中不允许从输出端口(clkout)读取.你应该在做的时候阅读它output <= not output;.根据信号,允许阅读.

还有一点需要注意:在过程的灵敏度列表中,没有必要在counter那里有信号.

process(CLKin, reset) -- no counter needed
Run Code Online (Sandbox Code Playgroud)

而另一件事......计算12000000个周期是:0 - >(12000000-1)= 11999999