题解 | #任意小数分频#

任意小数分频

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时钟切换点
parameter div_e = 5'd8; //偶数周期
parameter div_o = 5'd9; //奇数周期
//*************code***********//
reg [2:0]cnt_e;
reg [3:0]cnt_o;
reg [6:0]cnt;
reg clk1;
reg clk2;

always@(posedge clk_in or negedge rst)begin
    if(~rst)
      cnt<=0;
    else if(cnt==M_N-1)
      cnt<=0;
      else 
      cnt<=cnt+1;
end


always@(posedge clk_in or negedge rst)begin
    if(~rst)
      cnt_e<=0;
    else if(cnt<c89)
      cnt_e<=cnt_e+1;
    else 
      cnt_e<=0;
end

always@(posedge clk_in or negedge rst)begin
    if(~rst)
      clk1<=0;
    else if(cnt<c89)
      if(cnt_e==0||cnt_e==4)
        clk1<=~clk1;
      else 
        clk1<=clk1;
      else 
       clk1<=0;
end


always@(posedge clk_in or negedge rst)begin
    if(~rst)
      cnt_o<=0;
    else if(cnt>=c89)
       if(cnt_o==div_o-1)
        cnt_o<=0;
        else 
        cnt_o<=cnt_o+1;
    else 
      cnt_o<=0;
end

always@(posedge clk_in or negedge rst)begin
    if(~rst)
      clk2<=0;
    else if(cnt>=c89)
      if(cnt_o==0||cnt_o==4)
        clk2<=~clk2;
      else 
        clk2<=clk2;
   else 
     clk2<=0;
end

assign clk_out=clk1|clk2;

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

小数分配,无法直接精确到小数,因此采用奇偶时钟交替拼接的方法,让87个源时钟里有10个分配后时钟,设8分配a个,9分配b个,8a+9b=87 a+b=10,算出奇偶分配各需要几次。再判断一下奇偶分配的切换点,3*8=24时进行时钟的切换。完成小数分频

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务