目录
奇数分频的方法
奇数分频有占空比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
仿真: