Saturday, 16 July 2016

Switch De-bouncing in VHDL for the Mimas V2 FPGA Development Board

It's been a while since I blogged about my adventures in relearning VHDL. I am slowly regaining the knowledge I used to have when I studied at University (That seems a very long time ago now). Slowly and steadily I'm building up the knowledge needed to use an FPGA for something useful.

I intend writing a couple of connected blog posts over the next week or so in order to demonstrate something more involved (and useful).

This post will concentrate on reading input from a momentary switch. The Mimas V2 board has six momentary switches on the right side of the board below the micro-SD card slot. Lets write some code which reads in one these switches with de-bounce code. We can then use this code to implement a counter which we can display on the seven segment display. The counter and the seven segment display section will be in later posts. For now lets concentrate on reading in the switch properly. For now we can display the output onto an LED

Mimas V2 Momentary Switches

Here is the general idea:

Start

User Presses a momentary switch.

The FPGA reads in the button switch.

The button read is de-bounced.

The corresponding LED is illuminated until the button is released.

Go back to step 1.

End.

Lets fire up Xilinx WebISE and start a new project:

I called mine Mimas_switchDebounce - use any name you like! Click next when ready:

Make sure all the above settings are entered so that the project is setup correctly for the Mimas V2. Click next when you are ready:

Click Finish to return to the main project screen.

Now left click on the project menu and select 'add new source':

In the dialog window that appears select VHDL Module and give it a suitable name - I called mine switchDebounce:

Click 'Next' when you are ready to continue:

We need to define the inputs and outputs for the debounce code. We need to read in the raw button presses and then we need to display the results on an LED. This all needs to be synchonised with the 100 MHz clock on the Mimas V2.

Complete the form with the above settings and then click 'Next' to continue:

Click 'Finish' to return to the main project screen in WebISE:

Xilinx Webise has helpfully automatically generated some code for us....Let's examine the code.

The entity statement defines the inputs and outputs to the FGPA - three standard logic pins, the switch input, the LED output and the 100 MHz system clock.

The architecture statement is where the 'behaviour' of the code will be defined. There isn't anything in it yet but there will be! Lets decide how we are going to achieve things. There is more than one way of reading in buttons, checking that the button state has changed and then producing a result.

Lets make things easier for ourselves and draw a flow diagram:

This should make writing the code a little easier for us. Every diamond is an if statement, every rectangle is a part of a process....simples!

SIGNAL inputFlipFlop : STD_LOGIC_VECTOR(1DOWNTO0); -- input flip flopsCONSTANT countMax : INTEGER := 100000000; -- 100 MHz clock, delay is 100 ms and 100 ms = 10,0000000 nsSIGNAL count : INTEGERRANGE0TO countMax := 0;
The above lines create internal signals. The first signal inputFlipFlop is a simple flip flop. It is used to check if the button has been pressed or released (changed state). The constant countMax is the delay period that is set to ensure that a button has actually been pressed. It has been set to 100 ms although any number can be used. It was calculated from the 100 MHz source clock. 1/100,000,000 = 1 ns and 10,000,000 ns is equivalent to 100 ms. If a second was needed then the constant should be set to 1000000000 etc. The signal count is the value of the number of clock pulses that has passed. It is dynamically set to the maximum delay period set by countMax.

Click on the green 'implement top module' arrow and wait for the code to compile. There should not be any errors or warnings.

Now connect up the Mimas V2 board to your computer via a suitable USB cable and then fire up the Mimas V2 configuration tool. If you are using Linux then use the appropriate python tool - I don't use linux often....only so much time to learn new things at the moment! Select the appropriate COM port for your board and then navigate to the project...select the newly created bin file and click program!

Once programming is complete you should see something similar to the following:

The more astute among us will notice that the LED starts high and is turned low when a button press is detected. That is because the switches on the Mimas V2 board are active low....the code logic will need to be inverted in order to take the LED from low to high. It's a simple enough edit. We could also remove the seven segment display ghosting if needed...

That's all for now though people - more to come on this - next it's count the button press and display the result on the seven segment!

Hint change the line of code from LED_0 <= inputFlipFlop(1); to LED_0 <= not inputFlipFlop(1);

About Me

I'm an electronics engineer and uber geek from the UK. I live in the North West of England in Manchester. I mostly spend my time working in electronics and developing custom electronic solutions to problems as well as a few fun projects. I do a lot of development using microcontrollers - particularly using the arduino