【從零開始走進FPGA】非同于MCU的獨立按鍵消抖動
(2)循環(huán)n次計數(shù)消抖動
本文引用地址:http://www.ex-cimer.com/article/270304.htm同樣,此模塊也是Bingo無數(shù)次修改測試最后成型的代碼,利用了更少的資源,更適用于并行高速FPGA的性能要求。具體代碼實現(xiàn)過程請有需要的自行分析,本模塊通過相關(guān)時鐘的適配,n次計數(shù)來確認按鍵信號,Verilog代碼如下所示:
/*************************************************
* Module Name : key_scan.v
* Engineer : Crazy Bingo
* Target Device : EP2C8Q208C8
* Tool versions : Quartus II 11.0
* Create Date : 2011-6-25
* Revision : v1.0
* Description :
**************************************************/
module key_scan
#(
parameter KEY_WIDTH = 2
)
(
input clk, //50MHz
input rst_n,
input [KEY_WIDTH-1:0] key_data,
output key_flag,
output reg [KEY_WIDTH-1:0] key_value
);
//---------------------------------
//escape the jitters
reg [19:0] key_cnt; //scan counter
reg [KEY_WIDTH-1:0] key_data_r;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
key_data_r <= {KEY_WIDTH{1'b1}};
key_cnt <= 0;
end
else
begin
key_data_r <= key_data; //lock the key value
if((key_data == key_data_r) && (key_data != {KEY_WIDTH{1'b1}})) //20ms escape jitter
begin
if(key_cnt < 20'hfffff)
key_cnt <= key_cnt + 1'b1;
end
else key_cnt <= 0;
end
end
wire cnt_flag = (key_cnt == 20'hffffe) ? 1'b1 : 1'b0;//!!
//-----------------------------------
//sure the key is pressed
reg key_flag_r;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
key_flag_r <= 0;
key_value <= 0;
end
else if(cnt_flag)
begin
key_flag_r <= 1;
key_value <= key_data; //locked the data
end
else //let go your hand
key_flag_r <= 0; //lock the key_value
end
//---------------------------------------
//Capture the rising endge of the key_flag
reg key_flag_r0,key_flag_r1;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
key_flag_r0 <= 0;
key_flag_r1 <= 0;
end
else
begin
key_flag_r0 <= key_flag_r;
key_flag_r1 <= key_flag_r0;
end
end
assign key_flag = ~key_flag_r1 & key_flag_r0;
endmodule
fpga相關(guān)文章:fpga是什么
評論