【作业要求】
1、串联电阻的作用是什么?
2、请画出利用状态机实现1s流水灯的状态转化图并写出详细的代码。
【我的做法】1、串联电阻主要起限流作用,保证LED安全点亮。
2、module led (
input wire clk, // 50MHz时钟
input wire rst_n, // 异步复位,低有效
output reg [3:0] led // 4位LED输出,高电平点亮
);
// ---------- 1s定时使能产生 ----------
parameter CNT_1S = 26'd50_000_000 - 1; // 50M时钟,1s计数
reg [25:0] cnt;
wire en_1s; // 1秒周期使能脉冲
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
cnt <= 26'd0;
else if (cnt == CNT_1S)
cnt <= 26'd0;
else
cnt <= cnt + 1'b1;
end
assign en_1s = (cnt == CNT_1S); // 每1s产生一个时钟周期的高脉冲
// ---------- 状态定义 ----------
localparam S0 = 4'b0001, // LED0亮
S1 = 4'b0010, // LED1亮
S2 = 4'b0100, // LED2亮
S3 = 4'b1000; // LED3亮
reg [3:0] state, next_state;
// ---------- 第一段:状态寄存器 ----------
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
state <= S0; // 复位回到初始状态
else
state <= next_state;
end
// ---------- 第二段:次态逻辑 ----------
always @(*)
begin
case (state)
S0: if (en_1s) next_state = S1; else next_state = S0;
S1: if (en_1s) next_state = S2; else next_state = S1;
S2: if (en_1s) next_state = S3; else next_state = S2;
S3: if (en_1s) next_state = S0; else next_state = S3;
default: next_state = S0;
endcase
end
// 第三段:输出逻辑(摩尔型:输出仅取决于当前状态)
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
led <= 4'b0000;
else
led <= state;
end
endmodule【结果/截图说明】
评论 0