【作业要求】
1、串联电阻的作用是什么?
保护电路,防止电流过大烧毁LED灯
2、请画出利用状态机实现1s流水灯的状态转化图并写出详细的代码。
【我的做法】
`timescale 1ns / 1ps
// 使用状态机实现流水灯
module FSM_Run_LED(
input sysclk,
input rst_n,
output reg [3:0] led
);
parameter Time_1s = 26'd50000000;
reg [25:0] cont;
always @(posedge sysclk) // 1s计时器
begin
if(rst_n == 0)
cont <= 0;
else
begin
if(cont < Time_1s)
cont <= cont+1;
else if(cont == Time_1s-1)
cont <= 0;
else
cont <= 0;
end
end
// 1、状态声明:进行状态编码(四个LED灯、5种状态)
parameter IDLE = 4'b0000;
parameter S1 = 4'b0001, //状态1:亮最低位的灯
S2 = 4'b0010, //状态2:亮第二个灯
S3 = 4'b0100, //状态3:亮第三个灯
S4 = 4'b1000; //状态4:亮最高位的灯
// 2、声明现态和次态
reg [4:0] cur_state;
reg [4:0] next_state;
// 3、创建三段式状态机
// (1)第一段:描述总状态的转换(现态和次态的时序逻辑)
always @(posedge sysclk)
begin
if(rst_n == 0)
cur_state <= IDLE;
else
cur_state <= next_state;
end
// (2)第二段:组合逻辑电路,描述各个状态之间的跳转
always @(*)
begin
if(rst_n == 0)
next_state = IDLE;
else
begin
case(cur_state)
IDLE : next_state = S1;
S1 :begin
if(cont == Time_1s-1)
next_state = S2;
else
next_state = cur_state;
end
S2 :begin
if(cont == Time_1s-1)
next_state = S3;
else
next_state = cur_state;
end
S3 :begin
if(cont == Time_1s-1)
next_state = S4;
else
next_state = cur_state;
end
S4 :begin
if(cont == Time_1s-1)
next_state = S1;
else
next_state = cur_state;
end
default:next_state=IDLE;
endcase
end
end
// (3)第三段:描述各个状态下的动作
always @(posedge sysclk)
begin
if(rst_n == 0)
led <= 4'b0000;
else
case(cur_state)
IDLE:led<=IDLE;
S1:led<=S1;
S2:led<=S2;
S3:led<=S3;
S4:led<=S4;
default:led<=IDLE;
endcase
end
endmodule【结果/时序图/实验现象(可视频)】

评论 0