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

Parents Reply Children
  • 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

  • I just did it and it is not working for me i have an error:


    Compiling ‘main.c’
    14> In file included from ../../../../../../modules/nrfx/nrfx.h:45,
    14> from ../../../../../../modules/nrfx/hal/nrf_gpio.h:44,
    14> from ../../../../../../components/boards/boards.h:43,
    14> from C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:2:
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:72:39: error: 'NRFX_TIMER0_INST_IDX' undeclared here (not in a function); did you mean 'NRFX_TWIM0_INST_IDX'?
    14> ../../../../../../modules/nrfx/drivers/nrfx_common.h:113:37: note: in definition of macro 'NRFX_CONCAT_3_'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:72:25: note: in expansion of macro 'NRFX_CONCAT_3'
    14> C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:46:37: note: in expansion of macro 'NRFX_TIMER_INSTANCE'
    14> In file included from C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:14:
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: error: 'NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY' undeclared (first use in this function); did you mean 'NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: each undeclared identifier is reported only once for each function it appears in
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:110:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:111:45: error: 'NRFX_TIMER_DEFAULT_CONFIG_MODE' undeclared (first use in this function); did you mean 'NRFX_TIMER_DEFAULT_CONFIG'?
    14> C:\Users\koron\Desktop\nRF5_SDK_17.1.0_ddde560\examples\peripheral\twi_scanner\main.c:55:37: note: in expansion of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:112:50: error: 'NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH' undeclared (first use in this function); did you mean 'NRFX_TIMER_DEFAULT_CONFIG'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:112:50: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:113:27: error: 'NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY' undeclared (first use in this function); did you mean 'NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY'?
    14> ../../../../../../modules/nrfx/drivers/include/nrfx_timer.h:113:27: note: in definition of macro 'NRFX_TIMER_DEFAULT_CONFIG'
    Build failed

    And i do not know how to fix this 
    I attached "nrfx_timer.h"

Related