Verilog 奇数分频实现

目录

奇数分频的方法

相或实现

相与实现

异或实现

奇数分频的方法

奇数分频有占空比50%和非50%的两种,非50%的奇数分频可以直接通过计数器计数实现,而占空比为50%的奇数分频主要通过双边沿逻辑进行相与、相或、想异或实现。

参考文章:奇偶分频器(简介和Verilog实现)-CSDN博客

相或实现

2N+1 计数分频 

代码实现

单计数器实现:

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/11/26 12:22:50
// Design Name: 
// Module Name: clock_odd_div
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module clock_odd_div #(
    parameter DIV_CLK     = 9
)(
    input clk ,
    input rst_n ,
    output wire clk_div_odd 
);

reg [3:0] cnt ;
reg clkp_odd_r ;
reg clkn_odd_r ;

//cnt 
always @(posedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt <= 1'b0;
    else if(cnt==DIV_CLK - 1)
    begin 
        cnt <= 1'b0 ;
    end 
    else 
        cnt <= cnt +1'b1 ;
end

//posedge clk_div 

always @( posedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkp_odd_r <= 1'b0;
    else if(cnt== (DIV_CLK>>1) - 1)
        clkp_odd_r <= 1'b0;
    else if(cnt==DIV_CLK -1)
        clkp_odd_r <= 1'b1;
    else 
        clkp_odd_r <= clkp_odd_r ;

    
end

//negedge clk_div
always @( negedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkn_odd_r <= 1'b0;
    else if(cnt== (DIV_CLK>>1) - 1)
        clkn_odd_r <= 1'b0;
    else if(cnt==DIV_CLK -1)
        clkn_odd_r <= 1'b1;
    else 
        clkn_odd_r <= clkn_odd_r ;

    
end

//or 

assign clk_div_odd = clkp_odd_r | clkn_odd_r ;

endmodule

仿真图:

 

双计数器实现(上升沿计数器和下降沿计数器) 

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/11/26 12:57:43
// Design Name: 
// Module Name: clock_odddiv_2cnt
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module clock_odddiv_2cnt #(
    parameter DIV_CLK     = 9
)(
    input clk ,
    input rst_n ,
    output wire clk_div_odd 
);

reg [3:0] cnt_p ;
reg [3:0] cnt_n ;
reg clkp_odd_r ;
reg clkn_odd_r ;

//上升沿cnt 
always @(posedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt_p <= 1'b0;
    else if(cnt_p==DIV_CLK - 1)
    begin 
        cnt_p <= 1'b0 ;
    end 
    else 
        cnt_p <= cnt_p +1'b1 ;
end

//下降沿cnt 
always @(negedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt_n <= 1'b0;
    else if(cnt_n==DIV_CLK - 1)
    begin 
        cnt_n <= 1'b0 ;
    end 
    else 
        cnt_n <= cnt_n +1'b1 ;
end


//posedge clk_div 

always @( posedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkp_odd_r <= 1'b0;
    else if(cnt_p == (DIV_CLK>>1) - 1)
        clkp_odd_r <= 1'b0;
    else if(cnt_p == DIV_CLK -1)
        clkp_odd_r <= 1'b1;
    else 
        clkp_odd_r <= clkp_odd_r ;

    
end

//negedge clk_div
always @( negedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkn_odd_r <= 1'b0;
    else if(cnt_n == (DIV_CLK>>1) - 1)
        clkn_odd_r <= 1'b0;
    else if(cnt_n ==DIV_CLK -1)
        clkn_odd_r <= 1'b1;
    else 
        clkn_odd_r <= clkn_odd_r ;

    
end

//or 

assign clk_div_odd = clkp_odd_r | clkn_odd_r ;

endmodule

仿真图:

相与实现

2N+1次分频,对于相与逻辑,上升沿和下降沿的分频时钟高比重应该较大

单计数器实现:

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/11/26 12:22:50
// Design Name: 
// Module Name: clock_odd_div
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module clock_odddiv_1cnt_and #(
    parameter DIV_CLK     = 9
)(
    input clk ,
    input rst_n ,
    output wire clk_div_odd 
);

reg [3:0] cnt ;
reg clkp_odd_r ;
reg clkn_odd_r ;

//cnt 
always @(posedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt <= 1'b0;
    else if(cnt==DIV_CLK - 1)
    begin 
        cnt <= 1'b0 ;
    end 
    else 
        cnt <= cnt +1'b1 ;
end

//posedge clk_div 

always @( posedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkp_odd_r <= 1'b0;
    else if(cnt== (DIV_CLK>>1) - 1)
        clkp_odd_r <= 1'b1;
    else if(cnt==DIV_CLK -1)
        clkp_odd_r <= 1'b0;
    else 
        clkp_odd_r <= clkp_odd_r ;

    
end

//negedge clk_div
always @( negedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkn_odd_r <= 1'b0;
    else if(cnt== (DIV_CLK>>1) - 1)
        clkn_odd_r <= 1'b1;
    else if(cnt==DIV_CLK -1)
        clkn_odd_r <= 1'b0;
    else 
        clkn_odd_r <= clkn_odd_r ;

    
end

//or 

assign clk_div_odd = clkp_odd_r & clkn_odd_r ;

endmodule

仿真图:

双计数器:

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/11/26 12:57:43
// Design Name: 
// Module Name: clock_odddiv_2cnt
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module clock_odddiv_2cnt_and #(
    parameter DIV_CLK     = 9
)(
    input clk ,
    input rst_n ,
    output wire clk_div_odd 
);

reg [3:0] cnt_p ;
reg [3:0] cnt_n ;
reg clkp_odd_r ;
reg clkn_odd_r ;

//上升沿cnt 
always @(posedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt_p <= 1'b0;
    else if(cnt_p==DIV_CLK - 1)
    begin 
        cnt_p <= 1'b0 ;
    end 
    else 
        cnt_p <= cnt_p +1'b1 ;
end

//下降沿cnt 
always @(negedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt_n <= 1'b0;
    else if(cnt_n==DIV_CLK - 1)
    begin 
        cnt_n <= 1'b0 ;
    end 
    else 
        cnt_n <= cnt_n +1'b1 ;
end


//posedge clk_div 

always @( posedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkp_odd_r <= 1'b0;
    else if(cnt_p == (DIV_CLK>>1) - 1)
        clkp_odd_r <= 1'b1;
    else if(cnt_p == DIV_CLK -1)
        clkp_odd_r <= 1'b0;
    else 
        clkp_odd_r <= clkp_odd_r ;

    
end

//negedge clk_div
always @( negedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkn_odd_r <= 1'b0;
    else if(cnt_n == (DIV_CLK>>1) - 1)
        clkn_odd_r <= 1'b1;
    else if(cnt_n ==DIV_CLK -1)
        clkn_odd_r <= 1'b0;
    else 
        clkn_odd_r <= clkn_odd_r ;

    
end

//or 

assign clk_div_odd = clkp_odd_r & clkn_odd_r ;

endmodule

仿真图:

异或实现

2N+1次分频,上升沿时钟和下降沿时钟高电平和高电平之间应该相差(N+0.5)个时钟周期

代码实现:

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/11/26 13:41:57
// Design Name: 
// Module Name: clock_odddiv_xor
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module clock_odddiv_xor #(
    parameter DIV_CLK     = 9
)(
    input clk ,
    input rst_n ,
    output wire clk_div_odd 
);

reg [3:0] cnt ;
reg clkp_odd_r ;
reg clkn_odd_r ;

//cnt 
always @(posedge clk ,negedge rst_n) begin
    if(!rst_n)
        cnt <= 1'b0;
    else if(cnt==DIV_CLK - 1)
    begin 
        cnt <= 1'b0 ;
    end 
    else 
        cnt <= cnt +1'b1 ;
end

//posedge clk_div 

always @( posedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkp_odd_r <= 1'b0;
    else if(cnt== (DIV_CLK-1))
        clkp_odd_r <= ~clkp_odd_r;
    else 
        clkp_odd_r <= clkp_odd_r ;

    
end

//negedge clk_div
always @( negedge clk ,negedge rst_n) begin
    if(!rst_n)
        clkn_odd_r <= 1'b0;
    else if(cnt== (DIV_CLK >>1))
        clkn_odd_r <= ~clkn_odd_r;
    else 
        clkn_odd_r <= clkn_odd_r ;

    
end

//xor 

assign clk_div_odd = clkp_odd_r ^ clkn_odd_r ;

endmodule

仿真:

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值