//控制led亮灭的状态机
//状态机流程,上电复位后进入空闲状态,然后等待大约3s时间闸门打开时进入点亮led状态,当检测到时间
//闸门信号的高电平时,将状态切换至熄灭led状态,再次检测到时间闸门时切换至亮状态,如此循环。
reg led_r;
reg [2:0]led_current_state;//当前状态
reg [2:0]led_next_state;//下一个状态
//状态跳转逻辑设计always块
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
begin
led_current_state <= `state_idle;
end
else
begin
led_current_state <= led_next_state;
end
end
//状态输出
always@(led_current_state or state_sig or rst_n)
begin
if(!rst_n)
begin
led_next_state <= `state_idle;
end
else
begin
case(led_current_state)
`state_idle:begin
if(state_sig)
begin
led_next_state <= `state_led_on;
end
else
begin
led_next_state <= `state_idle;
end
end
`state_led_on:begin
if(state_sig)
begin
led_next_state <= `state_led_off;
end
else
begin
led_next_state <= `state_led_on;
end
end
`state_led_off:begin
if(state_sig)
begin
led_next_state <= `state_led_on;
end
else
begin
led_next_state <= `state_led_off;
end
end
default:begin
led_next_state <= `state_idle;
end
endcase
end
end
//逻辑输出
always@(posedge clk_25m or negedge rst_n)
begin
if(!rst_n)
begin
led_r <= led_off;
end
else
begin
case(led_next_state)
`state_idle:begin
led_r <= led_off;
end
`state_led_on:begin
led_r <= led_on;
end
`state_led_off:begin
led_r <= led_off;
end
default:begin
led_r <= led_off;
end
endcase
end
end