三态状态机实现的流水灯

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

posted @ 2013-11-14 21:59  luxiaolai  阅读(746)  评论(0编辑  收藏  举报