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

Rajneesh gravatar image

asked 2016-09-30 08:29:09 +0200

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

edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
olba gravatar image

answered 2016-09-30 11:15:38 +0200

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.

edit flag offensive delete publish link more


Thanks for replay,

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

Rajneesh ( 2016-10-01 14:34:58 +0200 )editconvert to answer

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.

Ole Bauck ( 2016-10-03 09:27:03 +0200 )editconvert to answer

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

Rajneesh ( 2016-10-04 12:53:05 +0200 )editconvert to answer

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):

// Wait for the external oscillator to start up

If you are using SoftDevice, use this code:

bool is_running = false;
while(is_running == false)
Ole Bauck ( 2016-10-06 10:00:49 +0200 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower


Asked: 2016-09-30 08:29:09 +0200

Seen: 912 times

Last updated: Sep 30 '16