三态状态机实现的流水灯

module led(CLOCK,RESET,LED,KEY_UP);
input CLOCK,RESET,KEY_UP;
output reg [3:0] LED;
parameter s1=2'b00,s2=2'b01,s3=2'b10,s4=2'b11;
reg [1:0] current_state,next_state;
parameter T1s=31'd2_000_0000;
reg [31:0] cnt;
reg t;
always @(posedge CLOCK or  negedge RESET)
if(!RESET)
 begin
  t<=0;
  cnt<=0;
 end
    else if(cnt==T1s)
     begin
      cnt<=0;
      t<=~t;
     end
        else
         cnt<=cnt+1;
always @(posedge t or negedge RESET)
if(!RESET)
 current_state<=s1;
     else
      current_state<=next_state;
always @(current_state)
begin
 next_state<=s1;
     case(current_state)
     s1:begin if(KEY_UP) next_state<=s2;else next_state<=s4;end
     s2:begin if(KEY_UP) next_state<=s3;else next_state<=s1;end
     s3:begin if(KEY_UP) next_state<=s4;else next_state<=s2;end
     s4:begin if(KEY_UP) next_state<=s1;else next_state<=s3;end
     default: next_state<=2'bzz;
     endcase
end
always @(current_state)
begin
 LED<=4'b0000;
      case(current_state)
     s1:LED<=4'b0001;
     s2:LED<=4'b0010;
     s3:LED<=4'b0100;
     s4:LED<=4'b1000;
     default:LED<=4'bzzzz;
      endcase
end
 
endmodule

转载于:https://www.cnblogs.com/luxiaolai/p/3424344.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值