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
  • I have one more question regarding the timers:

    In my case im initialising the timer to tick with 8 u sec and im generating an compare event each 20ms (so 2500 ticks). This works and is located in my IRQ handler, but now in the main (for loop) i want to read the current value in the count register is this just simply possible with the following code?

    for (;;)
        {
        	//NRF_TIMER2->TASKS_CAPTURE[0] = 1;
        	u_sec = (NRF_TIMER2->CC[0]); //value in us with 8us resolution
        	if (m_do_update)
        	        {
        				nrf_gpio_pin_set(LED_6);
        	            advertising_init();
        	            m_do_update = false;
        	            nrf_gpio_pin_clear(LED_6);
        	        }
    
        }
    

    Writing tasks_capture[0]=1 will stop the timer event, thats why i commented it.

    Regards, Pascal

Reply
  • I have one more question regarding the timers:

    In my case im initialising the timer to tick with 8 u sec and im generating an compare event each 20ms (so 2500 ticks). This works and is located in my IRQ handler, but now in the main (for loop) i want to read the current value in the count register is this just simply possible with the following code?

    for (;;)
        {
        	//NRF_TIMER2->TASKS_CAPTURE[0] = 1;
        	u_sec = (NRF_TIMER2->CC[0]); //value in us with 8us resolution
        	if (m_do_update)
        	        {
        				nrf_gpio_pin_set(LED_6);
        	            advertising_init();
        	            m_do_update = false;
        	            nrf_gpio_pin_clear(LED_6);
        	        }
    
        }
    

    Writing tasks_capture[0]=1 will stop the timer event, thats why i commented it.

    Regards, Pascal

Children
Related