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

How do I use button Push Interrupt

I want to change Variable state( 0->1 or 1->0) when I push the button.

I was searching for example relating to the GPIO.

but I did not find that i want.

Everything is a timer example

I would like to implement a function that operates when just press a button(using a interrupt).

You can do the advice would be appreciated.

I'd like to get help on how to implement.

What should I do?

I use nrf51822-DK and small button.

Parents
  • You can use the GPIOTE module (nRF51 Series Reference Manual section 14). Here is a small example that will toggle a LED when you push a button:

    #include "nrf_gpiote.h"
    #include "boards.h"
    
    #define GPIOTE_CHANNEL_0 0
    
    // Interrupt handler
    void GPIOTE_IRQHandler(){
    	  nrf_gpio_pin_toggle(BSP_LED_0);
    	  NRF_GPIOTE->EVENTS_IN[0] = 0;
    }
    
    int main(void)
    {
        nrf_gpio_cfg_output(BSP_LED_0);  //Configure LED 0 as output
    	nrf_gpio_cfg_input(BSP_BUTTON_0,BUTTON_PULL); //Configure button 0 as input
        //Configure GPIOTE channel 0, to generate an event from button 0:
    	nrf_gpiote_event_config(GPIOTE_CHANNEL_0, BSP_BUTTON_0, NRF_GPIOTE_POLARITY_HITOLO); 
    	NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Enabled; //Set GPIOTE interrupt register on channel 0
    	NVIC_EnableIRQ(GPIOTE_IRQn); //Enable interrupts
        for(;;){}
    }
    
  • I think most of the examples in the SDK (If you are using the newest version 11) already have the gpiote driver in included in the project, and then you will get a conflict. You can run the above code if you remove all .c files from the project except main.c, and it should compile. The nrf_gpio_* and nrf_gpiote_* functions are defined in nrf_gpiote.h and nrf_gpio.h, so you should include these.

    Anyways, I don't think it is a good solution to use the above code instead of the drivers, it was just provided as a simple example on how to use the GPIOTEs directly. You should use the driver and the callback functions provided by the driver.

Reply
  • I think most of the examples in the SDK (If you are using the newest version 11) already have the gpiote driver in included in the project, and then you will get a conflict. You can run the above code if you remove all .c files from the project except main.c, and it should compile. The nrf_gpio_* and nrf_gpiote_* functions are defined in nrf_gpiote.h and nrf_gpio.h, so you should include these.

    Anyways, I don't think it is a good solution to use the above code instead of the drivers, it was just provided as a simple example on how to use the GPIOTEs directly. You should use the driver and the callback functions provided by the driver.

Children
No Data
Related