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

Capture timer value

Hi,

i want to start timer 2 and read its value just before the radio gets active. I've seen in the nr51 series reference manual i can use task capture to set the current count value in CC register and afterwards set the register value in a variable(counter in my code). Here is my code so far: initialise:

NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;
	NRF_TIMER2->TASKS_CLEAR = 1;               
	NRF_TIMER2->PRESCALER = 14;
	NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
	NVIC_EnableIRQ(TIMER2_IRQn);
	NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE0_Enabled<<TIMER_INTENSET_COMPARE0_Pos;
	sd_softdevice_forward_to_application();

	NRF_TIMER2->TASKS_START = 1;

main for loop:

for (;;)
    {
    	if (m_do_update)
    	        {
    		    NRF_TIMER2->TASKS_CAPTURE[0];
    		    counter = NRF_TIMER2->CC[0];
    		    nrf_gpio_pin_write(LED_6, 1);
    	            advertising_init();
    	            m_do_update = false;
    	            nrf_gpio_pin_clear(LED_6);
    	        }
    }

When debugging, i do not see the counter variable incrementing. My ble application stops working (led_6 is nog toggling anymore). When removing the timer tasks the ble application works again.

Is this way of reading back the current CC[0] wrong? I could not find any example of this in the SDK.

Parents
  • Is it possible to clear the timer at a compare event? I need the timer to reach a maximum value of 31250 and immediately clear the timer. I've tried using the following code but it results in a value sometimes over the 31250 because the mcu needs to set clear task.

    void TIMER1_IRQHandler(void)
    {
    //0,125s compare event
    if(NRF_TIMER1->EVENTS_COMPARE[0]) //compare event happened
    {
    	NRF_TIMER1->EVENTS_COMPARE[0] = 0;//set compare register again to 0
    	NRF_TIMER1->TASKS_CLEAR = 1;//set count value to 0
    	half_sec++;					//value indicating half seconds
    	NRF_TIMER1->CC[0] = 31250; //Need to update the CC[0] with new compare value
    	nrf_gpio_pin_toggle(18); //indication for compare event
    }
    }
    
    

    Is it possible implementing this without the timer getting a value above 31250?

  • i have found the problem: the bluetooth stack has higher priority causing tasks_clear to be processed later

Reply Children
No Data
Related