题解 | 任意奇数倍时钟分频

任意奇数倍时钟分频

https://www.nowcoder.com/practice/b058395d003344e0a74dd67e44a33fae

`timescale 1ns/1ns

module clk_divider
    #(parameter dividor = 5)
( 	input clk_in,
	input rst_n,
	output clk_out
);
//占空比50%,奇数倍分频 --> 上升沿上升,dividor/2个时钟周期后,下降沿下降
reg pos_flag,neg_flag;
reg [$clog2(dividor-1):0] cnt;
always@(posedge clk_in or negedge rst_n)
if(!rst_n)
  cnt <= 0;
else
  cnt <= (cnt==dividor-1) ? 0 : (cnt + 1'b1);

always@(posedge clk_in or negedge rst_n)
if(!rst_n)
  pos_flag <= 0;
else if(cnt==(dividor-1)>>1)
  pos_flag <= ~pos_flag;
else if(cnt==dividor-1)
  pos_flag <= ~pos_flag;
else
  pos_flag <= pos_flag;
 

always@(negedge clk_in or negedge rst_n)
if(!rst_n)
  neg_flag <= 0;
else if(cnt==(dividor-1)>>1)
  neg_flag <= ~neg_flag;
else if(cnt==dividor-1)
  neg_flag <= ~neg_flag;
else
  neg_flag <= neg_flag;

assign clk_out = pos_flag || neg_flag;



endmodule

借用一个友友的图

全部评论

相关推荐

达芬骐:一个月入门,一年熟悉,三年精通,五年掌握,十年会用
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务