题解 | #任意小数分频#

任意小数分频

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 [4:0]cnt_p;
reg [6:0]cnt;
reg clk;
assign clk_out=clk;


always@(posedge clk_in or negedge rst)
if(!rst)begin
    clk<=0;
    cnt_p<=0;
    cnt<=0;
end else begin
    if(cnt==M_N)begin
    cnt<=1;
    clk<=1;
    end else if(cnt<M_N)cnt<=cnt+1'b1;
end

always@(posedge clk_in or negedge rst)
if(!rst)begin
    clk<=0;
    cnt_p<=0;
    cnt<=0;
end else begin
    if(cnt<c89)begin
        if(cnt_p==(div_e>>1))begin
            cnt_p<=cnt_p+1'b1;
            clk<=0;  
        end else if(cnt_p==(div_e))begin
            cnt_p<=1'b1;
            clk<=1;  
        end else if(cnt_p<(div_e>>1))begin
            cnt_p<=cnt_p+1'b1;
            clk<=1;
        end else if(cnt_p>(div_e>>1)&(cnt_p<div_e))begin
            cnt_p<=cnt_p+1'b1;
            clk<=0;
        end
    end if(cnt==c89)begin
            cnt_p<=1'b1;
            clk<=1; 
    end else if(cnt>c89)begin
        if(cnt_p==((div_o-1'b1)>>1))begin
            cnt_p<=cnt_p+1'b1;
            clk<=0;  
        end else if(cnt_p==(div_o))begin
            cnt_p<=1'b1;
            clk<=1;  
        end else if(cnt_p<((div_o-1'b1)>>1))begin
            cnt_p<=cnt_p+1'b1;
            clk<=1;
        end else if(cnt_p>((div_o-1'b1)>>1)&(cnt_p<div_o))begin
            cnt_p<=cnt_p+1'b1;
            clk<=0;
        end
    end
   

end

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

全部评论

相关推荐

北枳的南橘:建议大家务必去和他沟通一下,像极了小学生对你侃侃而谈国际局势😂
找AI工作可以去哪些公司...
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
03-19 10:38
实力求职者:真的绷不住了,第一张霸总人设,第二张求生欲拉满
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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