This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

TIMER counting micros

Hi

I am struggling with timer in counter mode. What i need is the counter to calculate time between each loop in microseconds. I do not want time interrupts i just need a function which will return me time in microseconds (like on arduino). 

Greetings

  • Hello,

    The easiest thing to do here would be to just read out the value of the TIMER CC register at the different places and then do a comparison between these to see how long time has passed (you will need to convert from ticks to µs).
    If you intend to keep this for more than debugging purposes you will need to make sure to handle potential wrap-arounds of the CC register (depending on the application and the TIMER frequency and BITMODE configuration).

    Best regards,
    Karl

  • uint32_t counter = 0 ;
    
    void start_timer(void)
    {		
    
    	NRF_TIMER1->TASKS_START = 1;	
            NRF_TIMER1->MODE = 2;
            NRF_TIMER1->PRESCALER = 4;
    	NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos);
    	NRF_TIMER1->TASKS_CLEAR = 1;
    }
    		
    
    static void read_TIM(void)
    {
     NRF_TIMER1->TASKS_COUNT = 1;
    NRF_TIMER1->TASKS_CAPTURE[1] = 1 ; 
    counter = NRF_TIMER1->CC[1];
    while(counter < 10000)
    {
    NRF_TIMER1->TASKS_COUNT=1;
    NRF_TIMER1->TASKS_CAPTURE[1] = 1 ; 
    counter = NRF_TIMER1->CC[1];
    }
    
    NRF_TIMER1->TASKS_CLEAR = 1;
    }
    
    int main(void)
    {
    	
      start_timer();                            
    	NRF_TIMER1->TASKS_START = 1;
    	while(true) 
    	{
    		 read_TIM();
                     NRF_LOG_INFO(counter);
                     NRF_LOG_FLUSH();;
    	}
    }

    I tried to use this code but the value is always 10000 and i do not know what is wrong with it.

  • Hello,

    It seems like you are configuring the TIMER instance after having started it - this may not work as expected.
    The simplest way for you to do this would be to instance and configure the timer as demonstrated in some of the example, and then either read the CC register value directly, or using the drivers nrfx_timer_capture function. I would perhaps recommend just using the nrfx driver, so you do not have to work with the registers directly.

    In this case, it could look something like this:

    static const nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(0);
    ..
    void timer_handler(nrf_timer_event_t event_type, void * p_context)
    {
    
    }
    ..
    void timer_init_and_start()
    {
        nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
        timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
        err_code = nrfx_timer_init(&m_timer, &timer_cfg, timer_handler);
        APP_ERROR_CHECK(err_code);
    
        /* Setting the CC to trigger at an arbitrary value of 2000 ms, since I do not know your application's requirements */
        uint32_t ticks = nrfx_timer_ms_to_ticks(&m_timer, 2000);
        nrfx_timer_compare(&m_timer,
                            NRF_TIMER_CC_CHANNEL0,
                            ticks,
                            false);
        nrfx_timer_enable(&m_timer);
    }
    ..
    uint32_t ticks = nrfx_timer_capture(&m_timer, NRF_TIMER_CC_CHANNEL0);
    ..
    


    Then compare the outputted ticks values and covert the difference into ms / µs to see how long the execution took.

    Best regards,
    Karl

  • My application need to calculate quaterions based on the time between each cycle (getting data from sensor and update function) and i already know - better time resolution and more precise then the position is better. It will be in use after button click and it stops after certain event after 5 to 20 mins. it cannot be based on time interrupt due to sensor refrest rate and etc. This alghoritm works well on arduino nano 33 ble and using micros() function (in arduino IDE). I want ot use 1 us Resolution (timer with 1 Mhz clock) but i do not know how to do it because of misleading informations on forum and not working programs. I just need to calculate time betweend each cycle i can start and stop timer each time and read the value.

  • Thank you for elaborating.

    Ceranthor said:
    I want ot use 1 us Resolution (timer with 1 Mhz clock) but i do not know how to do it because of misleading informations on forum and not working programs.

    What misleading information are you here referring to, and which non-working programs?

    Ceranthor said:
    I just need to calculate time betweend each cycle i can start and stop timer each time and read the value.

    Please try the approach I described in my earlier comment, and call nrfx_timer_capture whenever you would like to capture the current ticks of the timer.
    If you have configured the timer to use a 1 MHz frequency, each tick will indeed represent 1 µs, so you will not need to do any ticks -> µs conversion in that case.
    You might need to change the configured frequency of the TIMER as well.

    Best regards,
    Karl

Related