LED

点亮LED

源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//	led.v
`timescale 1ns / 1ps

// 点亮LED
module led(
input clk ,
input rst_n , // 低电平复位
output reg led
);

always @(posedge clk) begin // 同步复位
if(!rst_n)
led <= 1'b0 ;
else
led <= 1'b1 ;
end

endmodule

仿真文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//	tb.v
`timescale 1ns / 1ps

module tb();
reg clk ;
reg rst_n ;
wire led ;

led led_tb(
.clk (clk ) ,
.rst_n (rst_n ) ,
.led (led )
);

initial begin
clk = 1'b0 ;
rst_n = 1'b0 ;
#100
rst_n = 1'b1 ;
end
always #10 clk = ~clk;

endmodule

仿真结果

image-20230901171316985

流水灯

源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//	led_water
`timescale 1ns / 1ps

module led_water(
input clk ,
input rst_n ,
output reg [3:0] led
);

parameter TIME_S = 26'd50_000_000 ;
reg [25:00] cnt_s ;

//1s计时
always @(posedge clk) begin
if(!rst_n)
cnt_s <= 26'd0 ;
else if(cnt_s == TIME_S - 26'd1)
cnt_s <= 26'd0 ;
else
cnt_s <= cnt_s + 26'd1 ;
end

always @(posedge clk) begin
if(!rst_n)
led <= 4'b1000 ;
else if(cnt_s == TIME_S - 26'd1)
led <= {led[0], led[3:1]} ;
end

endmodule

仿真文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//	tb.v
// 为方便观察仿真结果,缩小源文件中TIME_S = 26'd5
`timescale 1ns / 1ps

module tb();
reg clk ;
reg rst_n ;
wire [3:0] led ;

led_water led_water_tb(
.clk (clk ) ,
.rst_n (rst_n ) ,
.led (led )
);

initial begin
clk = 1'b0 ;
rst_n = 1'b0 ;
#100
rst_n = 1'b1 ;
end
always #10 clk = ~clk;

endmodule

仿真结果

image-20230901172430225

走马灯

源文件

1

仿真文件

1

仿真结果

呼吸灯

源文件

1

仿真文件

1

仿真结果