Skip to content

Commit

Permalink
Added counter based debouncer.
Browse files Browse the repository at this point in the history
  • Loading branch information
seldridge committed Mar 21, 2012
0 parents commit b2bca11
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README
@@ -0,0 +1,6 @@
Repository for Verilog building blocks with a high chance of reuse
across different hardware projects (e.g. debouncers, display
drivers). This should not contain any sensitive data. Likewise, all
modules should be written and maintained at an excellent quality using
the best practices and sufficiently commented.
-se
55 changes: 55 additions & 0 deletions button_debounce.v
@@ -0,0 +1,55 @@
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Schuyler Eldridge
// button_debounce.v
// Created: 10/10/2009
//
// Counter based debounce circuit originally written for EC551 (back
// in the day).
//////////////////////////////////////////////////////////////////////////////////
module button_debounce(
input clk,
input button,
output debounce
);

parameter
count_value = 25000000;
// values of count_value correspond to max output frequency of debounce:
// count_value (clock cycles) | debounce (Hz)|
// ------------------------------------------|
// 200,000,000 | 0.25 |
// 100,000,000 | 0.50 |
// 50,000,000 | 1.00 |
// 25,000,000 | 2.00 |
// 12,500,000 | 4.00 |
// 6,250,000 | 8.00 |
// ------------------------------------------|
// *NOTE* This assumes a 50MHz clock

reg count_en;
reg debounce;
reg [25:0] count;

always @ (posedge clk) begin
// exit counting state
if (count >= count_value) begin
count_en <= 0;
count <= 0;
debounce <= 0;
// counting state
end
else if (count_en == 1) begin
count_en <= 1;
count <= count + 1;
debounce <= 0;
end
// output generation state
else if (button == 1) begin
count_en <= 1;
count <= 0;
debounce <= 1;
end
end

endmodule

0 comments on commit b2bca11

Please sign in to comment.