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

How to get a accurate millisecond?

Hi ,

What we want is a accurate ms.

below is what we do
1. Set the prescaler
config.prescaler = 31;

2.When I need the Elapsed ms,
First ,I get the elapsed rtc counter by nrf_drv_rtc_counter_get(&m_rtc)
And ,the elapsed ms =elapsed rtc *1000/1024.


3.I test it like this:
1). I set a REPEATED apptimer by value 10 000(10 s)

2)And I get the ms in the apptimer event,
It work well , I get 10000ms !
test_timer=76386
test_timer=86386
... ...
3)But when I add some code (include some other apptimer、interrupt....)
test_timer=126386
(add code .................)
test_timer=136375

the Elapsed ms is 136375-126386=9989,

3.My question
1)why the Elapsed ms (Elapsed rtc counter )become smaller?
2)How can I get a Elapsed ms?

3)I donot know wether the apptimer or rtc counter is wrong  or both?

thanks and regards!

Parents
  • Hi,

    1) I cannot say for sure without seeing your code. However, doing processing in CPU introduces some latency so you will typically get some "jitter" in the time, particularly if higher priority interrupts happen when there should have been some time related CPU activity. If you do it correctly, the average time should be correct though. The app timer is tested and verified to be correct on average.

    2) If you can live with some jitter, then using the app timer is probably the best as it allows you to share a single RTC instance between an arbitrary number of "soft" timers. If you cannot accept the jitter, then you should use a dedicated RTC instance and configure it using PPI and/or shorts so that everything happens in HW.

    3) Both the RTC and app_timer can be trusted. For the RTC, it is always as accurate as your 32 kHz clock source. The app_timer is tested and verified and will be equally accurate to the RTC in the long run but have more jitter. But you need to be aware of those and adjust accordingly. Also, you have not concluded your code, so it could also be that you have a logic flaw in how you do things (for instance a typical "wrong" way of doing things is to read the RTC tick value then clear the RTC. You will typically lose some ticks between these two operations. Even worse - the number of ticks you lose could be difficult to predict, as you risk bad luck and getting a higher priority interrupt between reading the RTC value and clearing it. Therefore, you should use the app_timer if you want to do things in software, as that is a tested and verified implementation).

Reply
  • Hi,

    1) I cannot say for sure without seeing your code. However, doing processing in CPU introduces some latency so you will typically get some "jitter" in the time, particularly if higher priority interrupts happen when there should have been some time related CPU activity. If you do it correctly, the average time should be correct though. The app timer is tested and verified to be correct on average.

    2) If you can live with some jitter, then using the app timer is probably the best as it allows you to share a single RTC instance between an arbitrary number of "soft" timers. If you cannot accept the jitter, then you should use a dedicated RTC instance and configure it using PPI and/or shorts so that everything happens in HW.

    3) Both the RTC and app_timer can be trusted. For the RTC, it is always as accurate as your 32 kHz clock source. The app_timer is tested and verified and will be equally accurate to the RTC in the long run but have more jitter. But you need to be aware of those and adjust accordingly. Also, you have not concluded your code, so it could also be that you have a logic flaw in how you do things (for instance a typical "wrong" way of doing things is to read the RTC tick value then clear the RTC. You will typically lose some ticks between these two operations. Even worse - the number of ticks you lose could be difficult to predict, as you risk bad luck and getting a higher priority interrupt between reading the RTC value and clearing it. Therefore, you should use the app_timer if you want to do things in software, as that is a tested and verified implementation).

Children
No Data
Related