发个按键消抖的程序,请大家指正
包括按下抖动和松开抖动没有判断按键有无按下,只是将抖动的信号过滤掉,信号输出电平和输入一致
按键默认电平值为高
新手请大家指正
`timescale 1 ns/1 ps
`define DELAY 1
module filter
(
clk,
rst,
/*port*/
signal_in,
signal_out
);
input clk ;
input rst ;
input signal_in ;
output signal_out ;
reg signal_out ;
reg MainState ;
parameter sDefaultHigh = 1 ;
parameter sPressFlutter = 2 ;
parameter sPressNormal = 3 ;
parameter sDefaultLow = 4 ;
parameter sReleaseFlutter = 5 ;
parameter sReleaseNormal = 6 ;
//parameter DELAY_20MS = 500_000 ;//500_000, 10ms
parameter DELAY_20MS = 250_000 ;//250_000, 5ms
//parameter DELAY_20MS = 50 ;//500_000, 10ms
reg count;
always @ (posedge clk or negedge rst)
begin
if(~rst)
begin
signal_out <=#`DELAY 1 ;
count <=#`DELAY 0 ;
MainState <=#`DELAY sDefaultHigh ;
end
else
begin
case (MainState)
sDefaultHigh:
begin
signal_out <=#`DELAY 1 ;
if(~signal_in)
MainState <=#`DELAY sPressFlutter ;
else
MainState <=#`DELAY sDefaultHigh ;
end
sPressFlutter:
begin
if(count == DELAY_20MS)
begin
if(signal_in == 0)
begin
MainState <=#`DELAY sPressNormal ;
signal_out <=#`DELAY 0 ;
count <=#`DELAY 0 ;
end
else
begin
MainState <=#`DELAY sPressFlutter ;
count <=#`DELAY 0 ;
end
end
else
begin
count <=#`DELAY count + 1 ;
signal_out <=#`DELAY 1 ;
end
end
sPressNormal:
begin
signal_out <=#`DELAY 0 ;
MainState <=#`DELAY sDefaultLow ;
end
sDefaultLow:
begin
signal_out <=#`DELAY 0 ;
if(signal_in)
MainState <=#`DELAY sReleaseFlutter ;
else
MainState <=#`DELAY sDefaultLow ;
end
sReleaseFlutter:
begin
if(count == DELAY_20MS)
begin
if(signal_in == 1)
begin
MainState <=#`DELAY sReleaseNormal ;
signal_out <=#`DELAY 1 ;
count <=#`DELAY 0 ;
end
else
begin
MainState <=#`DELAY sReleaseFlutter ;
count <=#`DELAY 0 ;
end
end
else
begin
count <=#`DELAY count + 1 ;
signal_out <=#`DELAY 0 ;
end
end
sReleaseNormal:
begin
signal_out <=#`DELAY 1 ;
MainState <=#`DELAY sDefaultHigh ;
end
default:
begin
MainState <=#`DELAY sDefaultHigh ;
end
endcase
end
end
endmodule
太啰嗦了, 而且貌似不支持长短按键 7802848 发表于 2014-5-5 09:06
太啰嗦了, 而且貌似不支持长短按键
本来就是个过滤毛刺的小程序
虽然啰嗦,但我觉得流程比较清楚 看你需求,简单处理是双采样后,遇到下降沿开始计数,遇到上升沿停止计数,如果计数大于某一数值,则有按键按下。对于长按键处理如此类似
页:
[1]