和单片机用软件实现消抖是一样的原理,这里有一个采样时钟,对按键输入信号进行采样,用两个计数器对高低信号持续时间进行计时,当达到阈值时,就认为信号稳定,输出稳定的信号。
一下是代码,为了方便仿真时间,设定采样持续时间为4个时钟周期。
module key_fangdou(clk,key_in,key_out);
parameter SAMPLE_TIME = 4;
input clk;
input key_in;
output key_out;
reg [21:0] count_low;
reg [21:0] count_high;
reg key_out_reg;
always @(posedge clk)
if(key_in ==1'b0)
count_low <= count_low + 1;
else
count_low <= 0;
always @(posedge clk)
if(key_in ==1'b1)
count_high <= count_high + 1;
else
count_high <= 0;
always @(posedge clk)
if(count_high == SAMPLE_TIME)
key_out_reg <= 1;
else if(count_low == SAMPLE_TIME)
key_out_reg <= 0;
assign key_out = key_out_reg;
endmodule
从波形中可以看出,短时间的抖动对输出没有影响,key_out比key_in延时4个时钟周期输出。
用户3809271 2016-6-18 17:46