请选择 进入手机版 | 继续访问电脑版

集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 948|回复: 1

按键消抖

[复制链接]
zxopenljx 发表于 2019-8-1 09:54:19 | 显示全部楼层 |阅读模式
按键消抖.v文件:
module key_filter(clk,rst_n,key_n,click_n);
               
        input clk;
        input rst_n;
        input key_n;
       
        output reg click_n;

        parameter MASK_TIME = 500_000;
       
        reg [18:0] cnt;
        reg state;
       
        localparam s0 = 1'b0,
                                  s1 = 1'b1;
               
        always @(posedge clk or negedge rst_n)
       
                begin
                        if(!rst_n)
                                begin
                                        click_n <= 1'b1;
                                        cnt <= 19'd0;
                                        state <= s0;
                                end
                        else
                                begin
                                        case(state)
                                                s0 : begin
                                                                if(key_n == 1'b0)
                                                                        begin
                                                                                if(cnt < MASK_TIME-1)
                                                                                        begin
                                                                                                cnt <= cnt + 1'b1;
                                                                                        end
                                                                                else
                                                                                        begin
                                                                                                state <= s1;
                                                                                                cnt <= 19'd0;
                                                                                                click_n <= 1'b0;
                                                                                        end
                                                                        end
                                                                else
                                                                        begin
                                                                                click_n <= 1'b1;
                                                                                cnt <= 19'd0;
                                                                                state <= s0;
                                                                        end
                                                end
                                               
                                               
                                                s1 : begin
                                                                if(key_n == 1'b1)
                                                                        begin
                                                                                if(cnt < MASK_TIME-1)
                                                                                        begin
                                                                                                cnt <= cnt + 1'b1;
                                                                                        end
                                                                                else
                                                                                        begin
                                                                                                click_n <= 1'b1;
                                                                                                state <= s0;
                                                                                                cnt <= 19'd0;
                                                                                               
                                                                                        end
                                                                        end
                                                                else
                                                                        begin
                                                                                click_n <= 1'b0;
                                                                                cnt <= 19'd0;
                                                                                state <= s1;
                                                                        end
                                                end
                                       
                                          default : state <= s0;
                                        endcase
                                end
                end
endmodule
测试文件-tb.v
`timescale 1ns/1ps

module key_filter_tb;

        reg clk;
        reg rst_n;
        reg key_n;
       
        wire click_n;
       
        initial begin
                clk = 1;
                key_n = 1;
                rst_n = 0;
                #200.1
               
                rst_n = 1;
               
               
                #200
               
                key_n = 0;
                #10
                key_n = 1;
                #20
               
                key_n = 0;
                #80
                key_n = 1;
               
                #200
                key_n = 0;
                #400
               
               
                key_n = 1;
                #10
                key_n = 0;
                #20
               
                key_n = 1;
                #80
                key_n = 0;
               
                #200
                key_n = 1;
                #800 $stop;
        end
       
       
        always #10 clk = ~clk;
       
       
        key_filter
                #(
                .MASK_TIME(5)
                )
               
                key_filter(
                .clk(clk),
                .rst_n(rst_n),
                .key_n(key_n),
                .click_n(click_n)
                );
               
endmodule
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|集成电路技术分享 ( 京ICP备20003123号-1 )

GMT+8, 2024-3-29 19:42 , Processed in 0.066572 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表