module key_filter(clk, rst_n, key_n, click_n);
parameter MASK_TIME = 100;
input clk;
input rst_n;
input key_n;
output reg click_n;
localparam s0 = 1'b0;
localparam s1 = 1'b1;
reg temp;
reg [31:0] count;
reg state;
always @ (posedge clk, negedge rst_n)
begin
if (!rst_n)
begin
count <= 0;
click_n <= 1;
temp <= 1;
state <= s0;
end
else
case (state)
s0 : if (key_n == temp)
state <= s0;
else
begin
temp <= key_n;
state <= s1;
end
s1 : if (key_n == temp)
if (count < MASK_TIME)
begin
count <= count + 1;
state <= s1;
end
else
begin
click_n <= temp;
count <= 0;
state <= s0;
end
else
begin
count <= 0;
temp <= key_n;
state <= s1;
end
endcase
end
endmodule
|