Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using the MicroZed Button for Input: Adam Taylor’s MicroZed Chronicles Part 11

Xilinx Employee
Xilinx Employee
0 0 46.2K

By Adam Taylor


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.



 Figure 1 MicroZed LED and Pushbutton Schematic.jpg


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.



 Figure 2 Typical Switch Bounce.jpg


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

GPIOConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);

    Status = XGpioPs_CfgInitialize(&Gpio, GPIOConfigPtr,GPIOConfigPtr->BaseAddr);

      if (Status != XST_SUCCESS) {

             print("GPIO INIT FAILED\n\r");

            return XST_FAILURE;



    //set direction and enable output- Existing from the last blog


    XGpioPs_SetDirectionPin(&Gpio, ledpin, 1);

    XGpioPs_SetOutputEnablePin(&Gpio, ledpin, 1);


    //set direction input pin – New in this blog

    XGpioPs_SetDirectionPin(&Gpio, pbsw, 0x0);



Reading the input pin is also very simple using the ReadPin function, which is provided by xgpiops.h. This value can be stored within a u32 (unsigned 32-bit integer) result as demonstrated below:



      sw = XGpioPs_ReadPin(&Gpio, pbsw); //read pin


      if (sw == 1) { //sw=1 when switch is pushed

        toggle = !toggle; //invert value stored in toggle



      printf("switch value %lu \n\r",sw); //output the sw value

      XGpioPs_WritePin(&Gpio, ledpin, toggle); //set the LED



When this program was run on the MicroZed, a button press toggled the LED each time it as pressed the picture below shows the results from the RS-232 output:


Figure 3 Pushbutton Code Output.jpg




The source code for the entire program is attached to this post. See below.



Note: Please see the previous entries in this MicroZed series by Adam Taylor:


Driving the Zynq SoC's GPIO: Adam Taylor’s MicroZed Chronicles Part 10


Meet the Zynq MIO: Adam Taylor’s MicroZed Chronicles Part 9


MicroZed XADC Software: Adam Taylor’s MicroZed Chronicles Part 8


Getting the XADC Running on the MicroZed: Adam Taylor’s MicroZed Chronicles Part 7


A Boot Loader for MicroZed. Adam Taylor’s MicroZed Chronicles, Part 6 


Figuring out the MicroZed Boot Loader – Adam Taylor’s MicroZed Chronicles, Part 5


Running your programs on the MicroZed – Adam Taylor’s MicroZed Chronicles, Part 4


Zynq and MicroZed say “Hello World”-- Adam Taylor’s MicroZed Chronicles, Part 3


Adam Taylor’s MicroZed Chronicles: Setting the SW Scene


Bringing up the Avnet MicroZed with Vivado




Tags (1)