任意奇数分频器


从网上找到一段感觉写的很不错的verilog,任意奇数分频器的实现。
上代码:


//任意奇数分频器,只需要将n改为你想要的奇数即可。
module any_odd_div (clkdiv,clk);
 output clkdiv;  //输出分频信号
 input clk;      //时钟信号
 reg[2:0]cnt1,cnt2;//计数器1,计数器2
 reg clk_temp1,clk_temp2;
 parameter n = 7;  //7分频
 
 always @(posedge clk)
 begin
  if(cnt1 == n-1)
   begin cnt1 <=3'b000; end
  else
   begin cnt1 <= cnt1 +1'b1; end
  if(cnt1 ==3'b000)
   begin clk_temp1 =1'b1; end
  if(cnt1 ==(n-1)/2)
   begin clk_temp1 =0; end
 end
 
 always @(negedge clk)
 begin
  if(cnt2 == n-1)
   begin cnt2 <=3'b000; end
  else
   begin cnt2 <=cnt2 +1'b1; end
  if(cnt2 ==3'b000)
   begin clk_temp2 =1; end
  if(cnt2 ==(n-1)/2)
   begin clk_temp2 =0; end
 end
 
 assign clkdiv = clk_temp1 | clk_temp2;
 
 endmodule
 
//%%%%%%%%%%%%%
补充一下,如果是其他分频情况,不仅需要改变分频参数n,同时还需要将寄存器的位数按分频数稍微调整一下即可,很方便的小程序。
由于任意偶数分频可以分解为2*N,N若为偶数可以在分,而偶数分频可以通过寄存器最高位的翻转实现,
这样,任意整数分频都可以实现了。



猜你喜欢

转载自blog.csdn.net/wangming520liwei/article/details/80920096