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

how to execute the timer interrupt during button press/release event

Hello everyone,

I am trying to implement the button /timer interrupt handler function

Running Functionalities,

  1. button(1) is pressed for 3 sec and button handler interrupt executes the Timer 3sec_handler fucntion where inside the timer handler am calling the PWM_init(); so pwm pulses are getting generated

  2. button(1) is pressed for 5 sec and then button handler interrupt executes the Timer 5sec_handler fucntion where inside the timer 5sec handler am calling the PWM_uinit(); so pwm pulses are getting stopped

function (1) is executing very well but the real problem is when i pressing the button for 5 sec then the timer _3sec handler function also getting executed and reinitialize pwm_init () while timer crossing the 3sec PWM pulses are again generated and sooner timer reaches the 5sec stops the PWM

i wanted to make pwm pulses when i press the button for 3 sec and stops the pwm pulses pressing the buton for 5sec without regenerating pwm pulses so could anyone suggest me how to overcome this issue and suggest me what are best possible logics to implement the proper button and timer handler interrupt to start and stop pwm pulses

Note. same button is used for both functionalities but if i use two buttons then they are worjing very well but trying to use only one button for both functions any ideas or suggestions would be really helpful for me

Thank you

  • the third variable x will block reinitialisation of the PWM if it is already initialised. or else you can share the code and i can modify it for u or u can mail me at [email protected]

  • I have uploaded my code please have a look and let me know am using sdk 9 without softdevice

  • /* Please try this hope it should work */
    #include <stdbool.h> #include <string.h> #include "app_button.h" #include "boards.h" #include "nrf_gpio.h" #include "app_timer.h" #include "app_pwm.h" #include "nrf_delay.h" #include "app_fifo.h" #include "app_trace.h" #include "app_uart.h" #include "nrf_drv_gpiote.h" #define TIMEOUT1 3000 //3s #define TIMEOUT2 5000 //5s

    #define APP_TIMER_PRESCALER 0 // Value of the RTC1 PRESCALER register. #define APP_TIMER_MAX_TIMERS 4 // Maximum number of simultaneously created timers. // NORDIC One for debounce, one for long press detect #define APP_TIMER_OP_QUEUE_SIZE 4 // Size of timer operation queues. #define BUTTON_DEBOUNCE_DELAY 50 // Delay from a GPIOTE event until a button is reported as pushed.

    #define LONG_BUTTON_PRESS_TIME_MS 5000 #define BUTTON_PRESS_TIME_MS 3000

    #define MS_TO_TICKS(ms) (ms*32768/1000) static app_timer_id_t m_timer_3_sec_id; static app_timer_id_t m_timer_5_sec_id;

    #define PWM_OUTPUT1 7 #define PWM_OUTPUT2 6

    #define MAX_TEST_DATA_BYTES (15U) /< max number of test bytes to be used for tx and rx. */ #define UART_TX_BUF_SIZE 256 /< UART TX buffer size. */ #define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */ APP_PWM_INSTANCE(PWM0,0); // Create the instance "PWM1" using TIMER1.

    static volatile bool ready_flag; // A flag indicating PWM status. static volatile bool pwm_flag; //PWM State indicator; 0 for not-initialised, 1 for initialised

    void pwm_ready_callback(uint32_t pwm_id) // PWM callback function { ready_flag = true; } void uart_error_handle(app_uart_evt_t * p_event) { if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR) { APP_ERROR_HANDLER(p_event->data.error_communication); } else if (p_event->evt_type == APP_UART_FIFO_ERROR) { APP_ERROR_HANDLER(p_event->data.error_code); } } static void PWM_init(void) {

    nrf_gpio_pin_set(10);//clear the LED
    nrf_gpio_pin_set(6);//clear the LED
    printf(" PWM Initialization:\n");
    ret_code_t err_code;
    
    /* 2-channel PWM */
    app_pwm_config_t pwm0_cfg = APP_PWM_DEFAULT_CONFIG_2CH(100UL,6,10);
    
    /* Switch the polarity of the second channel. */
    pwm0_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM0,&pwm0_cfg,NULL);
    APP_ERROR_CHECK(err_code);
    
    app_pwm_enable(&PWM0);
    app_pwm_channel_duty_set(&PWM0, 0, 50);
    nrf_delay_ms(500);
    app_pwm_disable(&PWM0);
    nrf_drv_gpiote_out_task_disable(6);
    nrf_gpio_pin_clear(6); //clear the LED                              
    nrf_gpio_pin_clear(10);//clear the LED
    app_pwm_uninit(&PWM0);
    

    }

    static void PWM_uinit(void) {

    app_pwm_disable(&PWM0);
    nrf_drv_gpiote_out_task_disable(6);
    nrf_gpio_pin_clear(6); //clear the LED                              
    nrf_gpio_pin_clear(10);//clear the LED
    app_pwm_uninit(&PWM0);
    

    } /*

    • Handler to be called when button is pushed.

    • param[in] pin_no The pin number where the event is genereated

    • param[in] button_action Is the button pushed or released */ static void button_handler(uint8_t pin_no, uint8_t button_action) { if(button_action == APP_BUTTON_PUSH) { switch(pin_no) { case BUTTON_1: printf("button is pressed :\n"); app_timer_start(m_timer_3_sec_id, MS_TO_TICKS(3000), NULL); // NORDIC: Start 3 second single shot timer app_timer_start(m_timer_5_sec_id, MS_TO_TICKS(5000), NULL); // NORDIC: Start 5 second single shot timer

       	default:
       		break;
       }                                                          
      

      } }

    /**

    • Initialize the clock. */ void init_clock() {

      printf(" CLOCK initialization:\n"); NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos); NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1; while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0); // Wait for clock to start }

    /**

    • Initialize all four LEDs on the nRF51 DK. */ void init_leds() { printf("LED initialization:\n"); nrf_gpio_range_cfg_input(BUTTON_1, BUTTON_2, NRF_GPIO_PIN_PULLUP);// nrf_gpio_range_cfg_output(LED_3, LED_4);

      nrf_gpio_pin_set(LED_4);//clear the LED

    }

    /*

    • Handler to be called on single shot timer event. */ static void timer_handler_3_sec(void * p_context) {

      if(!nrf_gpio_pin_read(BUTTON_1)) { if(0 == PWM_flag) { PWM_init(); printf("Button is pressed for 3 seconds:\n"); nrf_gpio_pin_clear(LED_3); //clear the LED PWM_flag = 1; } else { //Will come in this loop, when ever the PWM is initialise and the button is pressed for 5 second or more printf("PWM is already initialised\n"); }

      }

    } /*

    • Handler to be called on single shot timer event. */ static void timer_handler_5_sec(void * p_context) { if(!nrf_gpio_pin_read(BUTTON_1)) {

       if(1 == PWM_flag)
       	{
       		PWM_uinit();
       		printf("Button is pressed for 5 seconds:\n");
       		nrf_gpio_pin_set(LED_3); //clear the LED  
       		PWM_flag = 0;
       	}
       else
       	{
       		//Never expected to come in this section
       		printf("PWM is already uninitialised\n");
       	}
      

      } }

    /**@brief Function for initializing the button handler module. */ static void buttons_init(void) {

    printf("BUTTON initialization:\n");
    uint32_t err_code;
    // Button configuration structure.
    static app_button_cfg_t p_button[] = {  {BUTTON_1, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler},
    	{BUTTON_2, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}};
    
    // Macro for initializing the application timer module.
    // It will handle dimensioning and allocation of the memory buffer required by the timer, making sure that the buffer is correctly aligned. It will also connect the timer module to the scheduler (if specified).
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL);    
    app_timer_create(&m_timer_3_sec_id, APP_TIMER_MODE_SINGLE_SHOT, timer_handler_3_sec); // NORDIC Create single shot timer triggering interrupt handler timer_handler_3_sec once
    app_timer_create(&m_timer_5_sec_id, APP_TIMER_MODE_SINGLE_SHOT, timer_handler_5_sec); // NORDIC Create single shot timer triggering interrupt handler timer_handler_3_sec once
    
    
    // Initializing the buttons.
    err_code = app_button_init(p_button, sizeof(p_button) / sizeof(p_button[0]), BUTTON_DEBOUNCE_DELAY);
    APP_ERROR_CHECK(err_code);
    
    // Enabling the buttons.										
    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
    while(true)
    {
    	// Do nothing.
    }
    

    }

    /**

    • Function for application main entry. */ int main(void) { uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_ENABLED, false, UART_BAUDRATE_BAUDRATE_Baud38400 };

      APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_error_handle, APP_IRQ_PRIORITY_LOW, err_code);

      APP_ERROR_CHECK(err_code); printf("initialization all functions :\n"); nrf_gpio_pin_set(LED_3); //clear the LED
      init_leds(); init_clock(); buttons_init();

    }

  • Sorry I don't know how to upload / attach a document, so i pasted the code

  • Thank you so much its worked but i am not able understand the concept could you please describe me in detail about this third variable functionality

Related