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

How can get current time in microsecond same as micros() function arduino

Hello Everyone,

I am working on nRF51 can any one tell me how can i get current time in microsecond? micros() return current microsecond time in arduino. I wants same functionality in nRF51.

Kindly, Any one help me.

Regards, Rajneesh

  • Micros() in Arduino will give you the number of microseconds since the Arduino board began running the current program.

    You can do this with TIMER1 or TIMER2:

    • At startup, initialize the timer at 1MHz (counting microseconds) and start it.
    • In the Micros() function, trigger the CAPTURE task and return the captured value.

    TIMER1 and TIMER2 on nRF51 are 16 bit timers, so you will overflow after 65.5ms (unlike the one in Arduino that will overflow after about 70 minutes (32 bit)). You can handle this by for example enabling interrupt and clear the timer (using shorts) on compare event. In the interrupt increment a variable that will be added in the micros function. The amount of time added will depend on the compare value that will trigger the interrupt/clear.

    Running the timer peripheral continuously will increase the current by a significant amount (about 500-600uA compared to sleep current). Therefore I would suggest using RTC instead. The problem with this is that the maximum resolution of the RTC is about 30.5us as it uses the 32KHz (actually 32768Hz) clock. If you are fine with this resolution you should use it. The app_timer module has functions for getting the current value of the RTC. The RTC is 24 bits, so with 30.5us resolution the RTC will overflow after 512 seconds.

    For more info see the Reference Manual TIMER and RTC chapters.

  • Thanks for replay,

    One more question, What is return value of app_timer_cnt_get(); in microsecond or milisecond?

  • app_timer_cnt_get() will give you the current value of the RTC1 counter. If the prescaler is 0 (lowest, highest resolution), then the time since the RTC was started is 30.5us * RTC counter value, or RTC counter value / 32768 seconds.

  • Any idea about primary osillator? how to configute? start and capture?

  • You start the timer with NRF_TIMER1->TASKS_START = 1 and capture with NRF_TIMER1->TASKS_CAPTURE = 1.

    If you don't start the crystal, the timer will use the internal RC oscillator. On nRF51 the crystal use slightly less power than the RC oscillator, in addition it is much more accurate. Since you plan on running the timer all the time I would use the crystal. You start the crystal with this code (when not using SoftDevice):

    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    // Wait for the external oscillator to start up
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {} 
    

    If you are using SoftDevice, use this code:

    bool is_running = false;
    sd_clock_hfclk_request();
    while(is_running == false)
    {
        sd_clock_hfclk_is_running(&is_running); 
    }
    
Related