题解 | 任意小数分频

任意小数分频

https://www.nowcoder.com/practice/24c56c17ebb0472caf2693d5d965eabb

`timescale 1ns/1ns

module div_M_N(
 input  wire clk_in,
 input  wire rst,
 output wire clk_out
);
parameter M_N = 8'd87; 
parameter c89 = 8'd24; // 8/9时钟切换点-->先输出3个八分频,再输出7个九分频
parameter div_e = 5'd8; //偶数周期
parameter div_o = 5'd9; //奇数周期
//*************code***********//
//87个clk_in=10个clk_out;
//8<8.7<9-->可以拆解成x个8分频+y个九分频
//x+y=10(10个clk_out周期)
//8x+9y=87(87个clk_in周期)
//x=3,y=7
reg [6:0] cnt_r;
reg [3:0] cnt_sub_r;
reg clk_out_r;

always@(posedge clk_in or negedge rst)
if(!rst)
  cnt_r <= 0;
else
  cnt_r <= (cnt_r==M_N-1)?0:(cnt_r+1);

always@(posedge clk_in or negedge rst)
if(!rst) begin
    cnt_sub_r <= 0;
    clk_out_r <= 0;
end
else if(cnt_r < c89)begin
    cnt_sub_r <= (cnt_sub_r==div_e-1)?0:(cnt_sub_r+1);//0-7计数
    clk_out_r <= (cnt_sub_r<(div_e>>1))?1:0;//div_e>>1=div_e/2=4;
//0-3:clk_out为1,4-7:clk_out为0;
    end
else begin
    cnt_sub_r <= (cnt_sub_r==div_o-1)?0:(cnt_sub_r+1);//0-8计数
    clk_out_r <= (cnt_sub_r<(div_o>>1))?1:0;//0-3:clk_out为1;4-8:clk_out为0
    end

assign clk_out = clk_out_r;


  
//*************code***********//
endmodule

大家的题解都写的挺清楚的,这里借用一个楼主的图,主要给我自己看

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-19 14:56
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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