vhdl的分频问题

来源:百度知道 编辑:UC知道 时间:2024/06/24 16:43:16
entity fenpin is
port(clk:in std_logic; --输入的2MHz时钟信号
cp:out std_logic); --输出的500Hz时钟信号
end fenpin;

architecture fenpin_arc of fenpin is
begin
process(clk)
variable num:integer:=0;
begin
if(clk'event and clk='1') then
if(num=3999) then
cp<='1';
num:=0;
else num:=num+1; cp<='0';
end if;
end if
这是里面variable num:integer:=0;
begin
if(clk'event and clk='1') then
if(num=3999) then
cp<='1';
num:=0;
else num:=num+1; cp<='0';不清楚意思啊,哪个给我解释下,我自己想的是integer控制计数器常量为0,实现一个4000的计数器,感觉上是这样,但是不清楚怎么来的。

variable num:integer:=0;
定义 num为变量 初始值为0
begin
进程开始
if(clk'event and clk='1') then
时钟上升沿到来时
if(num=3999) then
cp<='1';
如果满足num=3999 则 cp置高 然后num变零 (从0000到3999 正好4000个时钟周期 ---num后面自加)
else num:=num+1; cp<='0';
否则 num自加1 cp为低电平

程序意思是 4000个输入时钟 才 输出一个输出时钟 cp就是这个时钟 你看cp只有到num为4000次时 才输出1次! 然后等下一个4000次后 在输出一个cp

你这个程序严格的讲 占空比为1/4000 如果占空比为50% 则应该是计时到1999时 cp输出1 ;计时到3999时 cp为0 然后num清零 不知道你理解了没有

num没有限定范围,应该是 num :integer 3999 downto 0;
周期是4000实现有2M到500的分频
4000*500=2M