1 //让LED按照量0.25秒,灭0.75秒的循环亮灭。
2 原代码
3 module counter_led_1(
4 clk,
5 reset_n,
6 led
7 );
8 input clk;
9 input reset_n;
10 output reg led;
11 parameter MCNT=50000000;
12 reg [25:0] counter;
13 always@(posedge clk or negedge reset_n)
14 if(!reset_n)
15 counter<=0;
16 else if(counter==MCNT-1)
17 counter<=0;
18 else
19 counter<=counter+1'b1;
20 always@(posedge clk or negedge reset_n)
21 if(!reset_n)
22 led<=0;
23 else if(counter==(MCNT/2+MCNT/4)-1)
24 led<=1;
25 else if(counter==MCNT-1)
26 led<=0;
27 endmodule
28 仿真代码
29 `timescale 1ns/1ns
30 module counter_led_1_tb();
31 reg clk;
32 reg reset_n;
33 wire led;
34 defparam counter_led_1_inst0.MCNT=50000;
35 counter_led_1 counter_led_1_inst0(
36 .clk(clk),
37 .reset_n(reset_n),
38 .led(led)
39 );
40 initial clk=1;
41 always #10 clk=!clk;
42
43 initial begin
44 reset_n=0;
45 #201;
46 reset_n=1;
47 #2000000000;
48 $stop;
49 end
50
51 endmodule