0x02 Adding Button Support

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

  1. 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.

  2. 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.

  3. 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;
     }
    
  4. Compile the code, connect your board and upload. Verify that the button is working as intended.

Further reading