| 1 |
20 |
Agner |
//////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
// Engineer: Agner Fog
|
| 3 |
|
|
//
|
| 4 |
|
|
// Create date: 2020-05-03
|
| 5 |
|
|
// Last modified: 2020-12-15
|
| 6 |
|
|
// Module name: debounce
|
| 7 |
|
|
// Project name: ForwardCom soft core
|
| 8 |
|
|
// Tool versions: Vivado 2020.1
|
| 9 |
|
|
// License: CERN-OHL-W v. 2 or later
|
| 10 |
|
|
// Description: Push button debouncer
|
| 11 |
|
|
//
|
| 12 |
|
|
// Input from one or more pushbuttons is filtered to remove contact noise.
|
| 13 |
|
|
// buttons_out is 1 when buttons_in is stable at 1 over a period.
|
| 14 |
|
|
// buttons_out is 0 when buttons_in is stable at 0 over a period.
|
| 15 |
|
|
// pulse_out is 1 for one clock cycle when the button is pressed
|
| 16 |
|
|
//
|
| 17 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
| 18 |
|
|
|
| 19 |
|
|
|
| 20 |
|
|
// pushbutton debouncer
|
| 21 |
|
|
module debounce
|
| 22 |
|
|
#(parameter num=2) // number of buttons to debounce
|
| 23 |
|
|
(
|
| 24 |
|
|
input clock, // system clock 50 - 100 MHz
|
| 25 |
|
|
input [num-1:0] buttons_in, // input from pushbutton
|
| 26 |
|
|
output reg[num-1:0] buttons_out, // debounced output
|
| 27 |
|
|
output reg[num-1:0] pulse_out // a single pulse of 1 clock duration when button is pressed
|
| 28 |
|
|
);
|
| 29 |
|
|
reg [19:0] count = 0; // clock divider
|
| 30 |
|
|
reg [2:0] shift [num-1:0]; // shift registers
|
| 31 |
|
|
genvar i;
|
| 32 |
|
|
generate
|
| 33 |
|
|
for (i=0; i
|
| 34 |
|
|
always_ff @(posedge clock) begin
|
| 35 |
|
|
pulse_out[i] <= 0;
|
| 36 |
|
|
count <= count + 1; // divide clock by 2**20
|
| 37 |
|
|
if (count == 0) begin
|
| 38 |
|
|
shift[i] <= {shift[i][1:0], buttons_in[i]}; // serial in parallel out shift register
|
| 39 |
|
|
if (shift[i] == 3'b111) begin // accept as stable high after 3 consecutive high samples
|
| 40 |
|
|
if (buttons_out[i] == 0) begin
|
| 41 |
|
|
pulse_out[i] <= 1; // set pulse_out high only in the first clock cycle after button press is stable
|
| 42 |
|
|
end
|
| 43 |
|
|
buttons_out[i] <= 1; // button is stable high
|
| 44 |
|
|
end else if (shift[i] == 3'b000) begin // accept as stable low after 3 consecutive low samples
|
| 45 |
|
|
buttons_out[i] <= 0; // button is stable low
|
| 46 |
|
|
end
|
| 47 |
|
|
end
|
| 48 |
|
|
end
|
| 49 |
|
|
end
|
| 50 |
|
|
endgenerate
|
| 51 |
|
|
endmodule
|
| 52 |
|
|
|