目录
1 实验1:单个灯,2条件亮灭
Q:亮0.25s,灭0.75s循环
1.1设计代码
dge clk or negedge reset_n)
if(!reset_n)
counter <= 0;
else if(counter == MCNT-1)
counter <= 0;
else
counter <= counter+1'b1;
reg [2:0]cnt;
always@(posedge clk or negedge reset_n)
if(!reset_n)
cnt <= 0;
else if(counter == MCNT-1)
cnt <= cnt+1'd1;
else if(cnt == 4)
cnt <= 0;
always@(posedge clk or negedge reset_n)
if(!reset_n)
led <= 0;
else case(cnt)
0:led = 0;
1:led = 1;
2:led = 0;
3:led = 0;
4:led = 0;
default:led = 0;
endcase
endmodule
1.2 testbench
将MCNT设置为12500,为了将循环时间从1s缩短至1ms。
`timescale 1ns / 1ps
module LSM_LED_1_tb;
reg clk;
reg reset_n;
wire led;
LSM_LED_1 LSM_LED_1(
.clk(clk),
.reset_n(reset_n),
.led(led)
);
defparam LSM_LED_1.MCNT = 12500;
initial clk = 1;
always #10 clk = ~clk;
initial begin
reset_n = 0;
#201;
reset_n = 1;
#5000000;
$stop;
end
endmodule
1.3 仿真波形
灯亮0.25ms,灭0.75ms,整个循环为1ms。
2 实验2:单个灯,4条件亮灭
Q:亮0.25s,灭0.5s,亮0.75s,灭1s循环
2.1 设计代码
module LSM_LED_2(
clk,
reset_n,
led
);
input clk;
input reset_n;
output reg led;
reg [25:0]counter;
parameter MCNT = 12500000;
always@(posedge clk or negedge reset_n)
if(!reset_n)
counter <= 0;
else if(counter == MCNT-1)
counter <= 0;
else
counter <= counter+1'b1;
reg [3:0]cnt;
always@(posedge clk or negedge reset_n)
if(!reset_n)
cnt <= 0;
else if(counter == MCNT-1)
cnt <= cnt+1'd1;
else if(cnt == 10)
cnt <= 0;
always@(posedge clk or negedge reset_n)
if(!reset_n)
led <= 0;
else case(cnt)
0:led = 0;
1:led = 1;
2:led = 0;
3:led = 0;
4:led = 1;
5:led = 1;
6:led = 1;
7:led = 0;
8:led = 0;
9:led = 0;
10:led = 0;
default:led = 0;
endcase
endmodule
2.2 testbench
同1.2testbench。
2.3 仿真波形
3 实验3:单个灯,用户指定
Q:用户指定亮暗模式,0.25s一个状态,8个状态1个循环
3.1 设计代码
`timescale 1ns / 1ps
//用户指定状态,0.25s一个状态,8个状态为1个循环
module LSM_LED_3(
clk,
reset_n,
ctrl,
led
);
input clk;
input reset_n;
input [7:0]ctrl;
output reg led;
reg [25:0]counter;
parameter MCNT = 12500000;
always@(posedge clk or negedge reset_n)
if(!reset_n)
counter <= 0;
else if(counter == MCNT-1)
counter <