在设计FPGA时只要用到了按键那么就需要使用按键消抖模块,目的就是为了消除按键的抖动。关于按键的抖动不再多说,直接给出设计的模块。
module keyboard_module(
input i_clk_5MHz,
input i_rstn, //复位
input i_key1, i_key2, i_key3, //三个按键 在本次实验中三个按键要实现的功能为功能切换、校时+1、确定ok。
output reg [2:0] kb_status //{i_key3, i_key2, i_key1} , 1 = on
);
reg [2:0] kb_d1;
reg [4:0] keep_cnt[2:0];
reg [12:0] clk_cnt;
always@(posedge i_clk_5MHz) begin
if(~i_rstn) begin
kb_d1 <= 0;
keep_cnt[2] <= 0;
keep_cnt[1] <= 0;
keep_cnt[0] <= 0;
clk_cnt <= 0;
end
else if(clk_cnt < 13'd5000) begin
clk_cnt <= clk_cnt+1;
end
else begin//a?T:F表示当a为逻辑1时则输出T如果a为逻辑0时则输出F
clk_cnt <= 0;//本代码段实现的是按键防抖的目的
kb_d1 <= {i_key3, i_key2, i_key1};
keep_cnt[2] <= ~(kb_d1[2]==i_key3) ? 0 :
(keep_cnt[2]==5'd20) ? 5'd20 : keep_cnt[2]+1; //表示在5000×20个时钟内达到稳定的效果
keep_cnt[1] <= ~(kb_d1[1]==i_key2) ? 0 :
(keep_cnt[1]==5'd20) ? 5'd20 : keep_cnt[1]+1;
keep_cnt[0] <= ~(kb_d1[0]==i_key1) ? 0 :
(keep_cnt[0]==5'd20) ? 5'd20 : keep_cnt[0]+1;
kb_status[2] <= (keep_cnt[2]==5'd20)? kb_d1[2] : kb_status[2];
kb_status[1] <= (keep_cnt[1]==5'd20)? kb_d1[1] : kb_status[1];
kb_status[0] <= (keep_cnt[0]==5'd20)? kb_d1[0] : kb_status[0];
end
end
endmodule