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

Detect External Pin Interrupts with nRF51

Hello, I am using external interrupt from an ADXL345 . Can you tell me how to detect that external interrupt with the nRF51 ?

  • The following code will detect a Low-to-High transition on a given Interrupt Pin( needs to be defined by you).

    GPIOTE Event Handler:

    void gpiote_evt_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        switch(action)
        {
            case NRF_GPIOTE_POLARITY_LOTOHI:
                //do something
                break;
            default:
                //do nothing
                break;
        }
    }
    

    Main Function:

    int main(void)
    {
        ret_code_t ret_code;
        
        ret_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(ret_code);
        
        nrf_drv_gpiote_in_config_t config =
        {
            .sense = NRF_GPIOTE_POLARITY_LOTOHI,
            .pull = NRF_GPIO_PIN_NOPULL ,
            .is_watcher = false,
            .hi_accuracy = false
        };
            
        
        ret_code = nrf_drv_gpiote_in_init(INTERRUPT_PIN, &config, gpiote_evt_handler);
        APP_ERROR_CHECK(ret_code);
        
        nrf_drv_gpiote_in_event_enable(INTERRUPT_PIN,true);
    }
    

    Update:

    Project files for an example that toogles a led when a button is pressed

    toggle_led_fixed.zip

  • #include <stdbool.h>

    #include <stdint.h>
    #include "nrf.h"
    #include "nrf_gpiote.h"
    #include "nrf_gpio.h"
    #include "boards.h"
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    #include "nrf_drv_gpiote.h"
    #include "app_error.h"
    #define INTERRUPT_PIN   BUTTON_1
    
    
    void gpiote_evt_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        switch(action)
        {
            case NRF_GPIOTE_POLARITY_HITOLO:
                //do something
                    // LEDS_ON(LED_1);
    				//nrf_gpio_pin_toggle(LED_2);
    				nrf_drv_gpiote_out_toggle(LED_2);
                break;
            default:
                //do nothing
                break;
        }
    }
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
         ret_code_t ret_code;
     //LEDS_CONFIGURE(LEDS_MASK);
    
        ret_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(ret_code);
    	
    //nrf_gpio_cfg_output(LED_2);
    //	nrf_gpio_pin_set(LED_2);
    	nrf_drv_gpiote_out_config_t led_config = GPIOTE_CONFIG_OUT_TASK_LOW;
    
    ret_code =  nrf_drv_gpiote_out_init(LED_2, &led_config);
    APP_ERROR_CHECK(ret_code);
        nrf_drv_gpiote_in_config_t config =
        {
            .sense = NRF_GPIOTE_POLARITY_HITOLO,
            .pull = NRF_GPIO_PIN_PULLUP ,
            .is_watcher = false,
            .hi_accuracy = false
        };
    
    
        ret_code = nrf_drv_gpiote_in_init(INTERRUPT_PIN, &config, gpiote_evt_handler);
        APP_ERROR_CHECK(ret_code);
    
        nrf_drv_gpiote_in_event_enable(INTERRUPT_PIN,true);
    }
    
  • here when i am pressing button 1, my LED don't turn on

  • If its a button you have to change NRF_GPIOTE_POLARITY_LOTOHI to NRF_GPIOTE_POLARITY_HITOLO.

Related