This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Configure button without BSP_EVENT_HANDLER

Hello to everyone.
I´m trying to configure the buttons of my SDK nRF52 to turn on LEDs, but all the examples I´ve found here are using BSP_EVENT_HANDLER.
So, my question is: that is the only way to correctly configure the buttons or I can configure them as a simply C++ code? I mean, directly, with only the button´s name.
I got this:

#include "nrf.h"
#include "nrf_gpio.h"

#define LED1 17
#define BTN1 13

int main (void)
{
   nrf_gpio_cfg_output(LED1);
   nrf_gpio_pin_clear(LED1);
   nrf_gpio_cfg_input(BTN1,NRF_GPIO_PIN_PULLDOWN);
   nrf_gpio_pin_clear(BTN1);

   while(1)
   {
       if(BTN1==true)
       {
          nrf_gpio_pin_toggle(LED1);
       }
   }

}

But it doesn´t work. I hope you can help me.

Parents
  • Hi.

    It is possible, but you need a bit more code.

    Here is how I did it:

    #include <stdbool.h>
    #include <stdint.h>
    
    #include "nrf.h"
    #include "nordic_common.h"
    #include "boards.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
       bool button_pressed = false;
    
       nrf_gpio_cfg_output(LED_1);
       nrf_gpio_pin_clear(LED_1);
    
       nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);
    
    
        while (true)
        { 
            button_pressed = nrf_gpio_pin_read(BUTTON_1);
    
            if(button_pressed == false)
            {
              nrf_delay_us(1000000);
              nrf_gpio_pin_toggle(LED_1);
              // Do nothing.
            }
        }
    }
    /** @} */
    

    You can't check if BTN1 is true, this is just a define, it will not change.

    I used the function nrf_gpio_pin_read, to see if the button had been pressed.

    /**
     * @brief Function for reading the input level of a GPIO pin.
     *
     * Note that the pin must have input connected for the value
     * returned from this function to be valid.
     *
     * @param pin_number Specifies the pin number to read.
     *
     * @return 0 if the pin input level is low. Positive value if the pin is high.
     */
    __STATIC_INLINE uint32_t nrf_gpio_pin_read(uint32_t pin_number);

    Best regards,

    Andreas

  • Thank you very much!
    That´s exactly what I was seraching for.
    I´ve only one question: why you use this line: " * @brief Function for application main entry. "?
    What´s its function?

Reply Children
No Data
Related