0x02 Adding Button Support
This article is a follow-up to the previous one, where we blinked the onboard LEDs. Here, we add pushbutton support to the project. We will use the Board support package(BSP) APIs as in the previous section.
Procedures
Open the "Hello World!" project from the last discussion. To add the Board Support Package for the onboard pushbutton, open the "Manage Runtime Environment" from the taskbar and expand "Board Support". In this case, expand the Buttons(API) and checkmark "Buttons" as shown below:
Click "OK" to add the package, and if any dependencies are pending, proceed as in the previous section to resolve them.
In the project pane, open the header file "Board_Buttons.h" under Board Support > Buttons_xxxx.c(Buttons) for inspection of the function prototypes the API exposes.
Open main.c in your Source Group folder for editing. We will add code to turn on the buttons in succession when the user button is pressed.
#include "Board_LED.h" #include <stdint.h> #include "Board_Buttons.h" //Simulate a delay void delay(void){ for(volatile uint32_t i = 0;i<500000;i++); } int main(void){ LED_Initialize(); Buttons_Initialize(); //Turn ON and OFF the two onboard LEDS upon Button press while(1){ if(Buttons_GetState()){ LED_On(0U); delay(); LED_Off(0U); delay(); LED_On (1U); delay(); LED_Off(1U); } } return 0; }
Compile the code, connect your board and upload. Verify that the button is working as intended.
Further reading
Subscribe to my newsletter
Read articles from Asogwa Emmanuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Asogwa Emmanuel
Asogwa Emmanuel
As an Electronics and Communications undergrad, I am deeply interested in the design of embedded systems in programmable logic, robotics and problem-solving. These domains excite me and drive my passion for learning and creating innovative solutions.