关于FPGA时钟二分频输出的简单实现
clock_dive文件,这里通过计数器来计算基准时钟clk
module clock_dive(
//时钟分频器
clk,
clk1,
rst_n
);
input clk;
input rst_n;
output clk1;
reg cnt;
wire add_cnt;
wire end_cnt;
wire clk;
reg clk1;
wire rst_n;
always @(posedge clk or negedge rst_n)begin
if(!rst_n)begin
cnt <= 0;
end
else if(add_cnt)begin
if(end_cnt)
cnt <= 0;
else
cnt <= cnt + 1;
end
end
assign add_cnt =1 ;
assign end_cnt = add_cnt && cnt==2-1 ;
always @(posedge clk or negedge rst_n)begin
if(rst_n==1'b0)begin
clk1<=0;
end
else if(cnt==0&&add_cnt==1)begin
clk1<=1;
end
else begin
clk1<=0;
end
end
endmodule
测试文件clock_tb
`timescale 1 ns/1 ns
module clock_tb();
//时钟和复位
reg clk ;
reg rst_n;
//uut的输出信号
wire clk1;
//时钟周期,单位为ns,可在此修改时钟周期。
parameter CYCLE = 20;
//复位时间,此时表示复位3个时钟周期的时间。
parameter RST_TIME = 3 ;
//待测试的模块例化
clock_dive uut(
.clk (clk ),
.rst_n (rst_n ),
.clk1 (clk1)
);
//生成本地时钟50M
initial begin
clk = 0;
forever
#(CYCLE/2)
clk=~clk;
end
//产生复位信号
initial begin
rst_n = 1;
#2;
rst_n = 0;
#(CYCLE*RST_TIME);
rst_n = 1;
end
endmodule
然后打开modelsim新建工程 添加文件然后simulate
刚刚接触FPGA 可能还有很多错误和纰漏的地方 希望大家多多批评指正!