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

Same button used for LED on and OFF using External Interrupt

Hello ,

I am new to nrf52832

i am using external interrupt for led on and off 

here the problem is when i press the button 1 led will turned on if i press the same button 1 led will be turned off 

kindly provide solution 

Thank you

Parents
  • Sounds like you probably want to use nrf_gpio_pin_toggle(pin) in your interrupt handler. You haven't given much detail so it is hard to say what you would need to change.

  • this my code until we press the same button again led will be on state

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {

    switch(action)
    {
    case NRF_GPIOTE_POLARITY_TOGGLE:

    {
    if(nrf_gpio_pin_read(PIN_IN) == BUTTON_PRESSED)
    {
    nrf_drv_gpiote_out_set(PIN_OUT);

    }
    else
    {
    nrf_drv_gpiote_out_clear(PIN_OUT);
    }

    break;
    }


    default:
    //do nothing
    break;

    }

    }





    /**
    * @brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output,
    * and configures GPIOTE to give an interrupt on pin change.
    */
    int main(void)
    {
    nrf_gpio_cfg_input(BUTTON_1, NRF_GPIO_PIN_PULLUP);

    ret_code_t ret_code;

    // Initialize the GPIOTE Peripheral
    ret_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(ret_code);

    // Configure LED_3 pin as output
    nrf_drv_gpiote_out_config_t led_config = GPIOTE_CONFIG_OUT_TASK_LOW;

    // Initialize LED_3 pin as output
    ret_code = nrf_drv_gpiote_out_init(PIN_OUT, &led_config);
    APP_ERROR_CHECK(ret_code);



    // Configuration for Interrupt Pin
    nrf_drv_gpiote_in_config_t config =
    {
    .sense = NRF_GPIOTE_POLARITY_TOGGLE,
    .pull = NRF_GPIO_PIN_PULLUP ,
    .is_watcher = false,
    .hi_accuracy = false
    };

    // Initialize Interrupt Pin
    ret_code = nrf_drv_gpiote_in_init(PIN_IN, &config,in_pin_handler);
    APP_ERROR_CHECK(ret_code);

    // Enable Interrupts from interrupt pin
    nrf_drv_gpiote_in_event_enable(PIN_IN,true);


    while (true)
    {
    // Do nothing.
    }

    }

Reply
  • this my code until we press the same button again led will be on state

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {

    switch(action)
    {
    case NRF_GPIOTE_POLARITY_TOGGLE:

    {
    if(nrf_gpio_pin_read(PIN_IN) == BUTTON_PRESSED)
    {
    nrf_drv_gpiote_out_set(PIN_OUT);

    }
    else
    {
    nrf_drv_gpiote_out_clear(PIN_OUT);
    }

    break;
    }


    default:
    //do nothing
    break;

    }

    }





    /**
    * @brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output,
    * and configures GPIOTE to give an interrupt on pin change.
    */
    int main(void)
    {
    nrf_gpio_cfg_input(BUTTON_1, NRF_GPIO_PIN_PULLUP);

    ret_code_t ret_code;

    // Initialize the GPIOTE Peripheral
    ret_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(ret_code);

    // Configure LED_3 pin as output
    nrf_drv_gpiote_out_config_t led_config = GPIOTE_CONFIG_OUT_TASK_LOW;

    // Initialize LED_3 pin as output
    ret_code = nrf_drv_gpiote_out_init(PIN_OUT, &led_config);
    APP_ERROR_CHECK(ret_code);



    // Configuration for Interrupt Pin
    nrf_drv_gpiote_in_config_t config =
    {
    .sense = NRF_GPIOTE_POLARITY_TOGGLE,
    .pull = NRF_GPIO_PIN_PULLUP ,
    .is_watcher = false,
    .hi_accuracy = false
    };

    // Initialize Interrupt Pin
    ret_code = nrf_drv_gpiote_in_init(PIN_IN, &config,in_pin_handler);
    APP_ERROR_CHECK(ret_code);

    // Enable Interrupts from interrupt pin
    nrf_drv_gpiote_in_event_enable(PIN_IN,true);


    while (true)
    {
    // Do nothing.
    }

    }

Children
No Data
Related