Skip to main content

Command Palette

Search for a command to run...

0x02 Adding Button Support

Updated
2 min read
0x02 Adding Button Support
A

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.

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

Embedded Drivers

Part 9 of 18

This series focuses on the design of embedded drivers and high-level APIs for accessing peripheral functions. Here, I share my knowledge and approach to writing portable drivers for MCU peripherals.

Up next

Assembly Programming

such empty

More from this blog

Untitled Publication

18 posts