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
  • 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.

  • 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"

  • Have you enabled the TIMER instance you are going to use in the sdk_config.h file?
    Please also remove or comment out any legacy definitions, such as TIMER_ENABLED. You should have NRFX_TIMER_ENABLED defined in your sdk_config, and TIMER_ENABLED should not be present as this may cause issues.

    Please also note that some peripheral instances are blocked or restricted by the SoftDevice.

    Ceranthor said:
    I attached "nrfx_timer.h"

    I do not see any attached file - but you should not modify the driver directly, in any case, since this might break it.

    Best regards,
    Karl

  • I do not have this option in sdk_config.h but i have it on another example "simple timer" and i not know how to add this part to sdk config.

  • You can add the entire nrfx_timer_* section from the other project into your sdk_config for your main project. Make sure to look through the configurations to ensure that they are configured to your requirements.

    // <e> NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver
    //==========================================================
    #ifndef NRFX_TIMER_ENABLED
    #define NRFX_TIMER_ENABLED 0
    #endif
    // <q> NRFX_TIMER0_ENABLED  - Enable TIMER0 instance
     
    
    #ifndef NRFX_TIMER0_ENABLED
    #define NRFX_TIMER0_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER1_ENABLED  - Enable TIMER1 instance
     
    
    #ifndef NRFX_TIMER1_ENABLED
    #define NRFX_TIMER1_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER2_ENABLED  - Enable TIMER2 instance
     
    
    #ifndef NRFX_TIMER2_ENABLED
    #define NRFX_TIMER2_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER3_ENABLED  - Enable TIMER3 instance
     
    
    #ifndef NRFX_TIMER3_ENABLED
    #define NRFX_TIMER3_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER4_ENABLED  - Enable TIMER4 instance
     
    
    #ifndef NRFX_TIMER4_ENABLED
    #define NRFX_TIMER4_ENABLED 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY  - Timer frequency if in Timer mode
     
    // <0=> 16 MHz 
    // <1=> 8 MHz 
    // <2=> 4 MHz 
    // <3=> 2 MHz 
    // <4=> 1 MHz 
    // <5=> 500 kHz 
    // <6=> 250 kHz 
    // <7=> 125 kHz 
    // <8=> 62.5 kHz 
    // <9=> 31.25 kHz 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY
    #define NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_MODE  - Timer mode or operation
     
    // <0=> Timer 
    // <1=> Counter 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_MODE
    #define NRFX_TIMER_DEFAULT_CONFIG_MODE 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH  - Timer counter bit width
     
    // <0=> 16 bit 
    // <1=> 8 bit 
    // <2=> 24 bit 
    // <3=> 32 bit 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH
    #define NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY
    #define NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6
    #endif
    
    // <e> NRFX_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module.
    //==========================================================
    #ifndef NRFX_TIMER_CONFIG_LOG_ENABLED
    #define NRFX_TIMER_CONFIG_LOG_ENABLED 0
    #endif
    // <o> NRFX_TIMER_CONFIG_LOG_LEVEL  - Default Severity level
     
    // <0=> Off 
    // <1=> Error 
    // <2=> Warning 
    // <3=> Info 
    // <4=> Debug 
    
    #ifndef NRFX_TIMER_CONFIG_LOG_LEVEL
    #define NRFX_TIMER_CONFIG_LOG_LEVEL 3
    #endif
    
    // <o> NRFX_TIMER_CONFIG_INFO_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_TIMER_CONFIG_INFO_COLOR
    #define NRFX_TIMER_CONFIG_INFO_COLOR 0
    #endif
    
    // <o> NRFX_TIMER_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_TIMER_CONFIG_DEBUG_COLOR
    #define NRFX_TIMER_CONFIG_DEBUG_COLOR 0
    #endif
    
    // </e>


    Best regards,
    Karl

Reply
  • You can add the entire nrfx_timer_* section from the other project into your sdk_config for your main project. Make sure to look through the configurations to ensure that they are configured to your requirements.

    // <e> NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver
    //==========================================================
    #ifndef NRFX_TIMER_ENABLED
    #define NRFX_TIMER_ENABLED 0
    #endif
    // <q> NRFX_TIMER0_ENABLED  - Enable TIMER0 instance
     
    
    #ifndef NRFX_TIMER0_ENABLED
    #define NRFX_TIMER0_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER1_ENABLED  - Enable TIMER1 instance
     
    
    #ifndef NRFX_TIMER1_ENABLED
    #define NRFX_TIMER1_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER2_ENABLED  - Enable TIMER2 instance
     
    
    #ifndef NRFX_TIMER2_ENABLED
    #define NRFX_TIMER2_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER3_ENABLED  - Enable TIMER3 instance
     
    
    #ifndef NRFX_TIMER3_ENABLED
    #define NRFX_TIMER3_ENABLED 0
    #endif
    
    // <q> NRFX_TIMER4_ENABLED  - Enable TIMER4 instance
     
    
    #ifndef NRFX_TIMER4_ENABLED
    #define NRFX_TIMER4_ENABLED 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY  - Timer frequency if in Timer mode
     
    // <0=> 16 MHz 
    // <1=> 8 MHz 
    // <2=> 4 MHz 
    // <3=> 2 MHz 
    // <4=> 1 MHz 
    // <5=> 500 kHz 
    // <6=> 250 kHz 
    // <7=> 125 kHz 
    // <8=> 62.5 kHz 
    // <9=> 31.25 kHz 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY
    #define NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_MODE  - Timer mode or operation
     
    // <0=> Timer 
    // <1=> Counter 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_MODE
    #define NRFX_TIMER_DEFAULT_CONFIG_MODE 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH  - Timer counter bit width
     
    // <0=> 16 bit 
    // <1=> 8 bit 
    // <2=> 24 bit 
    // <3=> 32 bit 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH
    #define NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH 0
    #endif
    
    // <o> NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY
    #define NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6
    #endif
    
    // <e> NRFX_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module.
    //==========================================================
    #ifndef NRFX_TIMER_CONFIG_LOG_ENABLED
    #define NRFX_TIMER_CONFIG_LOG_ENABLED 0
    #endif
    // <o> NRFX_TIMER_CONFIG_LOG_LEVEL  - Default Severity level
     
    // <0=> Off 
    // <1=> Error 
    // <2=> Warning 
    // <3=> Info 
    // <4=> Debug 
    
    #ifndef NRFX_TIMER_CONFIG_LOG_LEVEL
    #define NRFX_TIMER_CONFIG_LOG_LEVEL 3
    #endif
    
    // <o> NRFX_TIMER_CONFIG_INFO_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_TIMER_CONFIG_INFO_COLOR
    #define NRFX_TIMER_CONFIG_INFO_COLOR 0
    #endif
    
    // <o> NRFX_TIMER_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_TIMER_CONFIG_DEBUG_COLOR
    #define NRFX_TIMER_CONFIG_DEBUG_COLOR 0
    #endif
    
    // </e>


    Best regards,
    Karl

Children
Related