The Debounce circuit

De la WikiLabs
Jump to navigationJump to search

In sequential circuits, any input variable that is not dependent of the system clock, is an asynchronuos input. The majority of these async inputs are coming from the mechanical devices like the 4 push-buttons on the devboard. Besides being async, these signals are not perfectly rectangular, because the mechanical elements inside the buttons need a small time to get stable. For this short time, the generated signal contains multiple spikes, what may prevent correct functioning of other conntected circuits (it is behaving as a high-frequency signal). We will solve this problem, by ignoring the problematic period of time (a few milliseconds). The Debounce circuit below, receives on the input, a signal similar to in from the below figure, and generates a perfect rectangular output signal (out in the below picture).

Async input and debounced output

Implementation of a debounce circuit

module Debounce(
	output out,
	input clock,
	input in
);

parameter limit = 20'd650000;

reg [19:0] counter;
reg hit;

assign out = (counter == limit);

always@(posedge clock) begin
        if(!in) begin
		counter <= 0;
		hit <= 0;        
        end else if(counter == limit) begin
		hit <= 1;
		counter <= counter + 1;
	end else if(in & !hit) begin
		counter <= counter + 1;
	end
end

endmodule