按键消抖
按键消抖.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 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]