1 //让LED灯按照亮0.25秒,灭0.5秒,亮0.75秒,灭1秒的状态循环亮灭。
2 源代码
3 module counter_led_2(
4 clk,
5 reset_n,
6 led
7 );
8 input clk;
9 input reset_n;
10 output reg led;
11 parameter MCNT=125000000;
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<=1;
23 else if(counter==(MCNT/10)-1)
24 led<=0;
25 else if(counter==(MCNT/10+MCNT/5)-1)
26 led<=1;
27 else if(counter==(MCNT/10+MCNT/5)*2-1)
28 led<=0;
29 else if(counter==MCNT-1)
30 led<=1;
31 endmodule
32 仿真代码
33 `timescale 1ns/1ns
34 module counter_led_2_tb();
35 reg clk;
36 reg reset_n;
37 wire led;
38 defparam counter_led_2_inst0.MCNT=125000;
39 counter_led_2 counter_led_2_inst0(
40 .clk(clk),
41 .reset_n(reset_n),
42 .led(led)
43 );
44 initial clk=1;
45 always #10 clk=!clk;
46
47 initial begin
48 reset_n=0;
49 #201;
50 reset_n=1;
51 #2000000000;
52 $stop;
53 end
54
55 endmodule