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

How to get the value of a timer.

Hello everyone.
I want to turn the LED on and off with the push button, if I want to hold it down after 3 seconds the LED will turn off, if it has been pressed for less than 3, the LED will blink.

I am using TIMER2, but after 3 seconds the LED still does not turn off.
Please help me. B

#define led 17
#define button 13
#define DEBOUNCE_MS 150

unsigned long waitTime = 1000; 
bool ledStatus = 0;
bool lastButtonStatus = 0;
bool buttonLongPress = 0;
unsigned long lastChangedTime;

#define MY_TIMER            NRF_TIMER2
#define MY_TIMER_IRQn       TIMER2_IRQn
#define MY_TIMER_IRQHandler TIMER2_IRQHandler

static uint32_t my_timer_seconds;

void MY_TIMER_IRQHandler(void)
{
if ((MY_TIMER->EVENTS_COMPARE[0] != 0) && ((MY_TIMER->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0))
    {
        MY_TIMER->EVENTS_COMPARE[0] = 0;

        // Increment the second variable
        my_timer_seconds++;
    }
}

static void my_timer_start(void)
{
    // Reset the second variable
    my_timer_seconds = 0;
    
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;  // Set the timer in Counter Mode // 0x4000A000
  NRF_TIMER2->TASKS_CLEAR = 1;               // clear the task first to be usable for later
	NRF_TIMER2->PRESCALER = 6;                             //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
	NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;		 //Set counter to 16 bit resolution
	NRF_TIMER2->CC[0] = 10;                                //Set value for TIMER2 compare register 0
	//NRF_TIMER2->CC[1] = 250;                               //Set value for TIMER2 compare register 1

	NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos);
  NVIC_SetPriority(TIMER2_IRQn, 6);
	NVIC_EnableIRQ(TIMER2_IRQn);
	NRF_TIMER2->TASKS_START = 1;  	  
}

static uint32_t my_timer_get_ms(void)
{
    // Store the current value of the timer in the CC[1] register, by triggering the capture task
    MY_TIMER->TASKS_CAPTURE[1] = 1;
    
    // Combine the state of the second variable with the current timer state, and return the result
    return (my_timer_seconds * 1000) + (MY_TIMER->CC[1] / 1000);
}

static uint64_t my_timer_get_us(void)
{
    // Store the current value of the timer in the CC[1] register, by triggering the capture task
    MY_TIMER->TASKS_CAPTURE[1] = 1;
    
    // Combine the state of the second variable with the current timer state, and return the result
    return (uint64_t)my_timer_seconds * 1000000 + MY_TIMER->CC[1];
}

static void gpio_init () 
{ 
    nrf_gpio_cfg_input (button, NRF_GPIO_PIN_PULLUP); 
    nrf_gpio_cfg_output (led); 

}

int main (void) 
{
		uint32_t err_code;
    bool     erase_bonds;
		int t=0;
		err_code = app_timer_init();
		APP_ERROR_CHECK(err_code);
		my_timer_start();
		//my_timer_get_ms();
	// err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
	//	MY_TIMER_IRQHandler();
    gpio_init (); 
	
    while (true) 
    { 
			
       bool reading = nrf_gpio_pin_read(button); 
  if (!reading ) 
		{ 
		nrf_delay_ms (DEBOUNCE_MS); 
	if (!reading )
		{
		nrf_gpio_pin_toggle(led);
			
		}
  lastButtonStatus = reading; 
  lastChangedTime = my_timer_get_ms();
	
  }	
 
	if (my_timer_get_ms() - lastChangedTime > waitTime) 
{ 
    buttonLongPress = reading;
    lastChangedTime = my_timer_get_ms();
		
  }
  if (buttonLongPress == true) 
		{ 
		
		
				ledStatus = !ledStatus;
				buttonLongPress = false; 
				if(buttonLongPress==false)
						{
			
								nrf_gpio_pin_set(led);
								nrf_delay_ms(10000);
								nrf_gpio_pin_clear(led);
						}
  }
		
    } 
}
elow is my complete code.

  • Hello,

    I want to turn the LED on and off with the push button, if I want to hold it down after 3 seconds the LED will turn off, if it has been pressed for less than 3, the LED will blink.

    I am using TIMER2, but after 3 seconds the LED still does not turn off.

    Have you seen the BSP library? The BSP library implements button support, and has already implemented long_push functionality.
    You can then assign a button push to an action using the bsp_event_to_button_action_assign to assign an action to a button action, and the button action could then be BSP_BUTTON_ACTION_LONG_PUSH.
    It is especially easy to use if you are already developing from a BLE example, since it is most likely in use already.

    This way, you do not have to implement this functionality again on your own.

    Please let me know if this is not what you were looking for!

    Best regards,
    Karl

Related