码迷,mamicode.com
首页 > 其他好文 > 详细

verilog 状态机键盘扫描

时间:2016-03-08 23:10:36      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

module key (clk,rst,key_up,led);
input clk,rst,key_up;
output reg [3:0] led;
parameter T10ms=31‘d2_000_00;
reg clk_state;
reg [31:0] cnt;
parameter state_init=2‘b00,state_click=2‘b01,state_check=2‘b10;
reg [1:0] current_state,next_state;
reg [3:0] num_click,num_check,led_on;
always @ (posedge clk or negedge rst)
if(!rst)
 begin
  cnt<=0;
  clk_state<=0;
 end
      else if(cnt==T10ms)
       begin
        cnt<=0;
        clk_state<=~clk_state;
       end
           else
            cnt<=cnt+1‘b1;
always @ (posedge clk_state or negedge rst)
if(!rst)
 current_state<=state_init;
        else 
         current_state<=next_state;
always @ (posedge clk_state)
begin
 case(current_state)
 state_init: if(key_up) next_state<=state_init;
     else begin next_state<=state_click; num_click<=0;end
 state_click: if(key_up) begin next_state<=state_init;num_click<=0; end
     else begin num_click<=num_click+1;
       if(num_click==10)
         if(key_up)
           begin
           num_click<=0;
           next_state<=state_init;
           end
           else begin next_state<=state_check; num_check<=0;end
           end 
 state_check: if(key_up) begin next_state<=state_init; num_check<=0;led_on<=0;end
                         else begin num_check<=num_check+1;
         if(num_check==10)
         begin
         if(key_up)
          begin
           next_state<=state_init;
           num_check<=0;
          end
          else
          begin
          next_state<=state_check;
          num_check<=0;
          led_on<=led_on+1;
          end
         end end
endcase
end
always @ (current_state)
begin
led<=4‘b1111;
case (current_state)
state_init: led<=4‘b0101;
state_click: led<=4‘b0111;
state_check: led<=led_on;
endcase
end
 
endmodule

verilog 状态机键盘扫描

标签:

原文地址:http://www.cnblogs.com/luxiaolai/p/5255737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!