Forum Discussion
Altera_Forum
Honored Contributor
17 years agoIn case it's useful, this is a general purpose filter I use for IO pins. Debouncing is a special case. The logic is very tight and fast. You'll need to adjust the freq, limit, and N to suit your needs.
module filter(input clock,
input in,
output reg out, // out a filtered version of in
output reg strobe); // true for one cycle synchronous with a change
parameter freq = 25_000_000; // 25MHz
parameter limit = 50_000; // 50kHz
parameter dur = freq / limit;
parameter N = 14; // INV: 2**N > dur;
reg countdown;
// Filter out any meta stability
reg in_, in__;
always @(posedge clock) begin
strobe <= 0;
in_ <= in;
in__ <= in_;
if (in__ == out)
countdown <= dur - 1;
// Note, we depend on countdown being reset here.
else if (~countdown)
countdown <= countdown - 1;
else begin
out <= ~out;
strobe <= 1;
end
end
endmodule