We looked at driving an LED from Zynq SoC’s MIO in the previous instalment of this series (Driving the Zynq SoC's GPIO: Adam Taylor’s MicroZed Chronicles Part 10) and I mentioned that reading the status of a pushbutton would be very similar. However, I now think this topic merits a blog of its own as it completes the description of how you can use the Zynq SoC’s MIO and GPIO and it opens the door nicely on to the topic of interrupts, which I’ll describe in the next blog.
The MicroZed board’s pushbutton is connected to the Zynq SoC’s GPIO pin 51. For this example I intend to use software to connect the pushbutton’s state to the LED that we discussed in the previous blog. For this example, each press of the button will toggle the LED’s state.
The MicroZed schematic showing both the LED on pin 47 and pushbutton switch on Pin 51
The first thing we need to do is declare the pin we wish to us the input within our application:
#define pbsw 51
This is going to be a polled application so the main program loop will read the switch status. There are two methods of reading inputs from the GPIO: polled or interrupt driven. We have not looked at the Zynq’s interrupts to date so we will opt for the simpler polled method in this example. Interrupts will come later in this series.
All mechanical switches exhibit contact bounce, which is the result of the switch contacts physically striking together. The contacts’ momentum and elasticity act together to cause them to bounce apart one or more times before settling down and making steady contact. The following scope trace shows what switch bounce typically looks like.
Typical Switch Bounce
It is therefore common to employ either hardware or software to debounce the switch. As the schematic above shows, the MicroZed board design provides no debounce circuitry for the pushbutton switch, so we must perform this task with software. Otherwise, each press of the MicroZed board’s pushbutton would produce an indeterminate number of triggers to our LED control software and we would not be able to reliably control the state of the LED.
The application we are creating in this example is very simple and my approach to debouncing the switch is also going to be simple. I can debounce the switch simply by sampling the switch at a low frequency to ensure that I do not get multiple changes on the LED following a button press. I also decided that I would output the value of the LED pin over the RS232 link along with the ADC values from the Zynq SoC’s XADC mixed-signal subsystem, etc.
In the previous post in this series, we had already initialized the GPIO. Therefore, all that remains to use pin 51 as an input is to configure the pin as an input using the set direction function:
//GPIO Initilization – Existing from the last blog