八分频意味着时钟在一半的时刻就要取反(1~7的计数,在3就要判断发生翻转)
方式1 最常见的方式,但是在高精度情况使用容易出现误差
module oufenpin(
input wire sys_clk,
input wire rst_n,
output reg clk
);
parameter M=4'd8; //分频数
reg [2:0] count;
always@(posedge sys_clk or negedge rst_n)
if (!rst_n)
count <=1'b0;
else if (count == M/2-1'b1)
count <= 1'b0;
else
count <= count +1'b1;
always @(posedge sys_clk or negedge rst_n)
if (!rst_n)
clk <= 0;
else if (count == M/2-1'b1)
clk <= ~clk;
else
clk <= clk;
endmodule
方式2 用标志位的形式做分频,具体有点像八分频,高低电平有不同占空比(7低1高)
module oufenpin(
input wire sys_clk,
input wire rst_n,
output reg clk_flag
);
parameter M=4'd8;
reg [2:0] count;
always@(posedge sys_clk or negedge rst_n)
if (!rst_n)
count <=1'b0;
else if (count == M-1)
count <= 1'b0;
else
count <= count +1'b1;
always @(posedge sys_clk or negedge rst_n) //在每一个计数周期的最后一个时钟周期clk_flag置为高电平
if (!rst_n)
clk_flag <= 0;
else if (count == M-2)
clk_flag <= 1'b1;
else
clk_flag <= 0;
endmodule
module vtf_oufenpin;
// Inputs
reg sys_clk;
reg rst_n;
// Outputs
wire clk_flag;
// Instantiate the Unit Under Test (UUT)
oufenpin uut (
.sys_clk(sys_clk),
.rst_n(rst_n),
.clk_flag(clk_flag)
);
initial begin
// Initialize Inputs
sys_clk = 0;
rst_n = 0;
// Wait 100 ns for global reset to finish
#100;
rst_n <= 1'b1;
// Add stimulus here
end
always #10 sys_clk = ~sys_clk;
endmodule