MicroZed Button
In the last blog we looked at driving the LED from the MIO and I mentioned that reading in from a
push button would be very similar however, thinking about this I think it merits a blog of its own as it
completes nicely how we can use the MIO and GPIO and opens the door nicely to look at the use of
interrupts in the next blog.
On the MicroZed a push button is connected to GPIO pin 51, for this example I intend to connect the
push button to the LED we drove in the last blog such that a press of the button toggles the LEDs
state.
The schematics showing both the LED on pin 47 and switch upon 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
As this is going to be a polled application the reading of the switch will take the place within the
main loop of the programme. There are two methods of reading inputs from the GPIO either polled
or interrupt driven, as we have not looked at the interrupts on the Zynq to date we will opt for the
simpler polled example.
When any switch is pressed there is what is called contact bounce which is the result of a when the
switch contacts strike together, their momentum and elasticity act together to cause them to bounce
apart one or more times before making steady contact. It is therefore common for the engineer either
hardware or software to have to debounce the switch, as the schematic above shows no nothing
circuitry to debounce the switch we must do so in within the software.
Switch Bounce
As the application we are creating is very simple I can debounce the switch by sampling the switch at
a low frequency to ensure I do not get multiple changes on the LED following a button press.
I also decided that I would output the value of the pin over the RS232 link along with the ADC values
etc.
As in the last blog we had already initialised the GPIO, 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 pin is also very simple using the read pin function which is provided with xgpiops.h
this can be stored within an u32 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 was run on the MicroZed a button press toggled the LED each time it as pressed the
picture below shows the results from the rs232 output
The source code can be downloaded here.