verilog奇数分频,三分频实例
目的:输入一个时钟信号,输出占空比为50%的三分频。
首先得到一个占空比为1/3的分频时钟,然后将输入信号取反,得到一个移相180度的占空比为1/3的分频时钟,将两个时钟相或得到目的时钟。
verilog代码
module divider_3 (
input clk, // Clock
input rst_n, // Asynchronous reset active low
output wire clk_d3
);
reg[3:0] count,count1;
reg clk_div3,clk_div3_shift;
wire clk_n;
assign clk_n=~clk;
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
count<= 0;
end else if(count==4'd2) begin
count<=0 ;
end else begin
count<=count+1;
end
end
always @(posedge clk_n or negedge rst_n) begin
if(~rst_n) begin
count1<= 0;
end else if(count1==4'd2) begin
count1<=0 ;
end else begin
count1<=count1+1;
end
end
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
clk_div3<= 0;
end else if(count==4'd0||count==4'd1) begin
clk_div3<=~clk_div3 ;
end else begin
clk_div3<=clk_div3;
end
end
always @(posedge clk_n or negedge rst_n) begin
if(~rst_n) begin
clk_div3_shift<= 0;
end else if(count1==4'd0||count1==4'd1) begin
clk_div3_shift<=~clk_div3 ;
end else begin
clk_div3_shift<=clk_div3_shift;
end
end
assign clk_d3=clk_div3||clk_div3_shift;
endmodule
testbench:
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Module Name: vtf_led_test
//////////////////////////////////////////////////////////////////////////////////
module vtf_divider3_test;
// Inputs
reg clk;
reg rst_n;
// Outputs
wire clk_div3;
//Create clock
always #5 clk=!clk;
initial
begin
rst_n=0;
clk=0;
#20 rst_n=1;
#100 $finish;
end
divider_3 uut (
.clk(clk),
.rst_n(rst_n),
.clk_d3(clk_div3)
);
endmodule
波形图: