10 按键消抖;抖动小于15ms,输入时钟12MHz
参考文章:https://blog.csdn.net/Reborn_Lee/article/details/89927569
15ms/(1/12Mhz) = 180000 = 0x2BF20(16)
Verilog代码:
module presskey(
input clk,
input rst,
input key_in,
output reg key_out
);
//按键本身值为1,按下值为0;
reg sw_in_r0;
always@(posedge clk or negedge rst) begin
if(!rst)
sw_in_r0 <= 1;
else
sw_in_r0 <= key_in;
end
wire edgel, edgeh, edge_en;
assign edgel = sw_in_r0 & (~key_in);
assign edgeh = (~sw_in_r0) & key_in;
assign edge_en = edgel | edgeh;
reg [19:0] count;
always@(posedge clk or negedge rst) begin
if(!rst)
count <= 0;
else if(edge_en)
count <= 0;
else
count <= count + 1;
end
always@(posedge clk or negedge rst) begin
if(!rst)
key_out <= 1;
else if(count == 20'h2BF20)
key_out <= key_in;
else
key_out <= key_out;
end
endmodule