时钟:50MHz(周期为20ns)
计数值:2500*2(由0到2499)
计数周期:20ns*5000=100000ns=100us=0.1ms
模块代码:
module counter(Clk,led,Rst_n);
input Clk;
input Rst_n;
output reg led;
reg [24:0] cnt;
always@( posedge Clk or negedge Rst_n )
begin
if( Rst_n==0 )
cnt<=0;
else if( cnt==2499 )
cnt<=0;
else
cnt<=cnt+1'b1;
end
always@( posedge Clk or negedge Rst_n )
begin
if( Rst_n==0 )
led<=0;
else if( cnt==2499 )
led=~led;
end
endmodule
testbench代码:
`timescale 1ns/1ns
`define time_periord 20
module counter_tb;
reg clk;
reg rst;
wire led;
counter u1
(
.Clk(clk),
.led(led),
.Rst_n(rst)
);
initial
clk=0;
always
#10 clk=~clk;
initial
begin
rst=0;
#100;
rst=1;
#(`time_periord*20000);
$stop;
end
endmodule
RTL仿真结果:
本文详细介绍了使用50MHz时钟实现一个从0计数到2499再复位的计数器,并通过RTL仿真验证其功能。包括模块代码、测试bench代码以及仿真结果分析。
4041

被折叠的 条评论
为什么被折叠?



