时钟: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仿真结果: