当前位置: 首页 > article >正文

状态机按键消抖(学习笔记)

状态转移图:

module key_filter(
    Clk,
    Reset_n,
    Key,
//    Key_P_Flag,
//    Key_R_Flag,
    Key_Flag,
    Key_state
    );
    input Clk;
    input Reset_n;
    input Key;
//    output reg Key_P_Flag;
//    output reg Key_R_Flag;
    output Key_Flag;
    output reg Key_state;    
    
    
     reg Key_P_Flag;
     reg Key_R_Flag;
     assign Key_Flag = Key_P_Flag | Key_P_Flag;
     
    reg [1:0] r_Key;
    always@(posedge Clk)
        r_Key <= {r_Key[0],Key};
        
//     always@(posedge Clk)begin       
//        r_Key[0] <= Key;
//        r_Key[1] <= r_Key[0];
//      end

//上升沿下降沿
        wire pedge_key;
        assign pedge_key = r_Key == 2'b01;
        wire nedge_key;
        assign nedge_key = r_Key == 2'b10;
        
        reg [19:0] cnt;
        
        reg [1:0] state;
        always@(posedge Clk or negedge Reset_n)
        if(!Reset_n)begin
            state <= 0;
            Key_R_Flag <= 1'b0;
            Key_P_Flag <= 1'b0;
            cnt <= 0;
            Key_state <= 1;
         end
         else begin
            case(state)
                0:
                    begin
                        Key_R_Flag <= 1'b0;
                        if(nedge_key)
                            state<=1;
                         else
                            state<=0;
                         end
                1:
                    if( (pedge_key)&&(cnt < 1000000-1))
                        state<=0;
                     else if(cnt >= 1000000-1)begin
                        state<=2;
                        cnt <= 0;
                        Key_P_Flag <= 1;
                        Key_state <=0;
                        end
                        else begin
                            cnt <= cnt + 1'b1;
                            state<=1;
                         end
                         
                     2:
                         begin
                            Key_P_Flag <= 0;
                             if(pedge_key)
                                state <= 3;
                             else
                                state <= 2;  
                             end                     
                      3:
                           if((nedge_key)&&(cnt<1000000-1))
                                state<=2;
                             else if(cnt>=1000000-1)begin
                                state<=0;
                                cnt <= 0;
                                Key_R_Flag <= 1'b1;
                                Key_state<=1;
                            end
                            else begin
                                 cnt <= cnt +1'b1;
                                 state <=3;
                              end
                           endcase
                        end

endmodule
`timescale 1ns / 1ns
//



module key_filter_tb();

    reg Clk;
    reg Reset_n;
    reg Key;
//    wire Key_P_Flag;
//    wire Key_R_Flag;
    wire Key_Flag;
    wire Key_state;
    
     key_filter key_filter(
        Clk,
        Reset_n,
        Key,
//        Key_P_Flag,
//        Key_R_Flag,
        Key_Flag,
        Key_state
        );
        
     initial Clk = 1;
     always#10 Clk = ~Clk;
     
     initial begin
        Reset_n = 0;
        Key = 1;
        #201;
        Reset_n = 1;
        
        #3000;
        
        Key = 0;
        #20000;
        Key = 1;
        #30000;
        
        Key = 0;
        #20000;
        Key = 1;
        #30000;
        
        Key = 0;
        #20000;
        Key = 1;
        #30000;
         
         Key = 0;
        #20000;
        Key = 1;
        #30000;
        
        Key = 0;
        #50000000;

        Key = 1;
        #30000;
        Key = 0;
        #20000;
        
        Key = 1;
        #30000;
         Key = 0;
        #20000;
        Key = 1;
        #500000000;
                Key = 0;
        #20000;
        Key = 1;
        #30000;
        
        Key = 0;
        #20000;
        Key = 1;
        #30000;
        
        Key = 0;
        #20000;
        Key = 1;
        #30000;
         
         Key = 0;
        #20000;
        Key = 1;
        #30000;
        
        Key = 0;
        #50000000;

        Key = 1;
        #30000;
        Key = 0;
        #20000;
        
        Key = 1;
        #30000;
         Key = 0;
        #20000;
        Key = 1;
        #500000000;
        
        $stop;
        
     end
     
endmodule

   


http://www.kler.cn/a/306585.html

相关文章:

  • 1.两数之和-力扣(LeetCode)
  • Excel筛选的操作教程
  • Dockerfile的使用
  • python制作一个简单的端口扫描器,用于检测目标主机上指定端口的开放状态
  • WordPress 6.7 “Rollins”发布
  • 基于Python的网上银行综合管理系统
  • Flink有界流实现(1)
  • 【Python】谷歌浏览器总是自动更新,使用selenium跟chromedriver版本不匹配怎么办?
  • 01,大数据总结,zookeeper
  • 算法练习题27——疫情下的电影院(模拟)
  • AI辅助癌症诊断取得了进展
  • Angular面试题一
  • 大模型 LLM(Large Language Models)如今十分火爆,对于初入此领域的新人小白来说,应该如何入门 LLM 呢?是否有值得推荐的入门教程呢?
  • 深度学习自编码器 - 引言篇
  • java基于PDF底层内容流的解析对文本内容进行编辑
  • 象过河手机进销存,外出办公更方便,随时了解经营情况
  • C# 静态static
  • 基于HTML5的下拉刷新效果
  • 如何避免长距离遗忘问题
  • HarmonyOS NEXT 封装实现好用的网络模块(基于最新5.0的API12)
  • Android 12 Launcher3 去掉Hotseat
  • JVM 调优篇7 调优案例3- gc overhead limit exceed
  • ListBox显示最新数据、左移和右移操作
  • K8s中HPA自动扩缩容及hml
  • idea2024.2永久使用
  • MFC工控项目实例之十五定时刷新PC6325A模拟量输入