Switch Debounce and Edge-Detection

After a hiatus of three months, I again started playing around with my FPGA boards. Now, I want to learn how to make my projects more interactive with the use of buttons and switches.

Edge Detection

Rising or falling edge of a waveform can be detected in multiple ways:

  1. Direct method: An edge can be detected by using a “delay” register. A delay register stores the value of the waveform at the last clock cycle and if the current value and the last value do not match, an edge is detected.
  2. Using FSM: I implemented a Mealy Machine to detect the rising edge of the output from the button. It is a simple FSM with two states – zero and one. The output of this FSM is a “tick” which goes high on rising edge of the input for one clock cycle. The VHDL code is very simple and self explanatory.

Edge detection 2

In the above FSM, level is the input from the switch and tick is the output.

Debounce

From my experience with microcontroller projects, I know that mechanical contacts inside switches bounce a few times before stabilizing and providing a useful output. It is fairly easy to debounce switches with a microcontroller; all that is required is a small delay. But in the FPGA world things aren’t as simple. An FSM with eight states is presented in the book, “FPGA Prototyping by VHDL Examples” by Pong. P. Chu and I implemented that on my Elbert V2. Eight states are required to debounce both pressing and releasing actions.

debounce_FSM Output of the above FSM should be a waveform shown below:

debounce_waveform

Below is the switch output from edge detection without debounce logic. The button was pressed once and two peaks are clearly visible on the first waveform. The second waveform is the clock, displayed for reference.

switch bounce

Below is the output with the debounce and edge detection logic.

Edge Detection 1

The first waveform is the debounced output from the switch and the second waveform is the output from the edge detection logic.

Using the debounce and edge detection logic, I have created a simple counter which “advances” a series of eight LED’s for every press of a button.

ezgif.com-video-to-gif

The VHDL code and files can be found at my GitHub repository.

Next up is Microblaze soft processor and VGA.

Thank you and have a great weekend.

One Reply to “”

Leave a comment