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

How Can I use Multiple Interrupts for Keypad

#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf.h"
#include "nrf_drv_gpiote.h"
#include "app_error.h"
#include "nrf_drv_timer.h"
#include "app_timer.h"
#include "app_button.h"
#include "nrf_drv_ppi.h"

#define PIN_IN     BSP_BUTTON_3
#define PIN_IN_1   BSP_BUTTON_2
#define PIN_OUT    BSP_LED_3
#define PIN_OUT_1  BSP_LED_2

//void Tarama_Basla(void)
//{
//  nrf_gpio_cfg_output(PIN_OUT);
//  nrf_gpio_cfg_input(PIN_IN, NRF_GPIO_PIN_PULLUP);
//  for(;;)
//    {
//      if(nrf_gpio_pin_read(PIN_IN))
//        nrf_gpio_pin_clear(PIN_OUT);
//      else
//        nrf_gpio_pin_set(PIN_OUT);
//    }
//}
/*******************************************************************************************//////
void button_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
    if(action == NRF_GPIOTE_POLARITY_TOGGLE  && pin == PIN_IN){
        nrf_gpio_pin_toggle(PIN_OUT);
    }
}
void Tarama_Basla(void)
{
    nrf_gpio_cfg_output(PIN_OUT);
    nrf_gpio_pin_set(PIN_OUT);
    
    //Initialize GPIOTE driver
    nrf_drv_gpiote_init();
    
    //Configure button with pullup and event on both high and low transition
    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
    config.pull = NRF_GPIO_PIN_PULLUP;
    nrf_drv_gpiote_in_init(PIN_IN, &config, button_event_handler); //Assign button config to a GPIOTE channel
                                                                //and assigning the interrupt handler
    nrf_drv_gpiote_in_event_enable(PIN_IN, true);                  //Enable event and interrupt
    
    for(;;)__WFE(); //CPU sleep while waiting for event

}

Here is my code. I did it one button but i must add 4 button. How to use multiple pins for interrupt? 

  • void button_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
    if(action == NRF_GPIOTE_POLARITY_TOGGLE && pin == PIN_IN){
    nrf_gpio_pin_toggle(PIN_OUT);
    }
    }
    void button_event_handler2(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
    if(action == NRF_GPIOTE_POLARITY_TOGGLE && pin == PIN_IN_1){
    nrf_gpio_pin_toggle(PIN_OUT_1);
    }
    }
    void Tarama_Basla(void)
    {
    nrf_gpio_cfg_output(PIN_OUT);
    nrf_gpio_pin_set(PIN_OUT);
    nrf_gpio_cfg_output(PIN_OUT_1);
    nrf_gpio_pin_set(PIN_OUT_1);
    //Initialize GPIOTE driver
    nrf_drv_gpiote_init();
    
    //Configure button with pullup and event on both high and low transition
    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
    config.pull = NRF_GPIO_PIN_PULLUP;
    nrf_drv_gpiote_in_init(PIN_IN, &config, button_event_handler); //Assign button config to a GPIOTE channel
    nrf_drv_gpiote_in_init(PIN_IN_1, &config, button_event_handler2); //and assigning the interrupt handler
    nrf_drv_gpiote_in_event_enable(PIN_IN, true); //Enable event and interrupt
    nrf_drv_gpiote_in_event_enable(PIN_IN_1, true);
    for(;;)__WFE(); //CPU sleep while waiting for event
    
    }

    I found this way but it is not usefull. I think it should be other way?! The other way what is it?

  • Hi,

    Please have a look at the BSP Example. It should be close to what you need. You can change the event handler to something like this to get events from 4 buttons:

    void bsp_evt_handler(bsp_event_t evt)
    {
        uint32_t err_code;
        switch (evt)
        {
            case BSP_EVENT_KEY_0:
                if (actual_state != BSP_INDICATE_FIRST)
                    actual_state--;
                else
                    actual_state = BSP_INDICATE_LAST;
                break;
    
            case BSP_EVENT_KEY_1:
    
                if (actual_state != BSP_INDICATE_LAST)
                    actual_state++;
                else
                    actual_state = BSP_INDICATE_FIRST;
                break;
    
            case BSP_EVENT_KEY_2:
    
                if (actual_state != BSP_INDICATE_LAST)
                    actual_state++;
                else
                    actual_state = BSP_INDICATE_FIRST;
                break;
    
            case BSP_EVENT_KEY_3:
    
                if (actual_state != BSP_INDICATE_LAST)
                    actual_state++;
                else
                    actual_state = BSP_INDICATE_FIRST;
                break;
    
            default:
                return; // no implementation needed
        }
        ...
        ...

Related