新闻  |   论坛  |   博客  |   在线研讨会
按键消抖心得
502593045 | 2012-06-30 00:30:34    阅读:4813   发布文章


按键消抖代码:

module debounce(clk_1K,rst_n,sig_in,sig_out);

       input clk_1K;

       input rst_n;

       input sig_in;

       output sig_out;

      

       reg [3:0] cnt;

       reg [2:0] q;

             

       always @ (posedge clk_1K or negedge rst_n)

       begin

              if(!rst_n)

                     cnt <= 0;

              else

                     begin

                            cnt <= cnt + 1;

                            if(cnt == 10)   //每延时10ms检测一次

                                   q <= {q[1:0],sig_in};

                     end

       end

      

       wire push;

 

       assign push = q[0] & q[1] & q[2];       //检测两次

      

       reg push_r;

      

       always @ (posedge clk_1K or negedge rst_n)

              if(!rst_n)

                     push_r <= 0;

              else

                     push_r <= push;

                           

       assign sig_out = push_r & (~push);

      

endmodule

 

 

按键消抖心得:

两点:1、蓝色部分:延时检测;2、红色部分:只取一个周期的按键信号

延时检测每个人都有自己的方法,或代码不同或延时长短;这里要强调的非常非常重要的一点,是只采用一个周期的按键信号。如下面代码的add和subtract是由两个按键产生的信号:

       always @ (posedge clk_1K or negedge rst_n)

       begin

              if(!rst_n)

                     count <= 0;

              else if(add)

                     count <= count + 1;

              else if(subtract)

                     count <= count - 1;      

       end

如果按键按下产生的add和subtract信号保持超过了一个周期,就会进行多次count值的变化。这是违背设计初衷的。

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
haipiao  2013-06-19 11:19:42 

assign push = q[0] & q[1] & q[2]; //检测两次 请问是不是写错了? assign push = (~q[0]) & q[1] & q[2]; //检测两次

虾虽在江湖,江湖却没有关于虾的传说!
推荐文章
最近访客