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

RTC0 nrf 52 read counter value

Hi,

I am new to the nrf52. I want to implement the code that gives me current date and time value based on default set values.

for this i need to read the current counter value. Can any one tell me the simplest way to read the current counter value in RTC0?

Also is there any sample code related to date and time calculation in nrf52??

Please give reply.!!!!

Parents
  • Hi,

    First I must stress that the real time counter (RTC) is a timer. It is not, as the abbreviation "RTC" may suggest, a real time clock. However, it runs off the 32 kHz clock and can be used to keep track of time within the accuracy of that clock signal.

    You will find that the Real Time Counter Example of the latest SDK uses RTC on the nRF52.

    For date and time calculation I suggest that you use an existing time and date library, since there are a lot of pitfalls when working with time and date. I think the ctime standard c library would be a good choice. Most likely the time_t format will be a unix timestamp and it should be safe to increment a "current time" value of this type every second to keep it up to date.

    Regards, Terje

Reply
  • Hi,

    First I must stress that the real time counter (RTC) is a timer. It is not, as the abbreviation "RTC" may suggest, a real time clock. However, it runs off the 32 kHz clock and can be used to keep track of time within the accuracy of that clock signal.

    You will find that the Real Time Counter Example of the latest SDK uses RTC on the nRF52.

    For date and time calculation I suggest that you use an existing time and date library, since there are a lot of pitfalls when working with time and date. I think the ctime standard c library would be a good choice. Most likely the time_t format will be a unix timestamp and it should be safe to increment a "current time" value of this type every second to keep it up to date.

    Regards, Terje

Children
  • thanks Terje for your reply.

    Do you have any idea how can I read the value of real time counter in nrf52?

  • Sorry, I missed the point. See the RTC HAL and driver, in particular RTC HAL and nrf_rtc_counter_get().

  • Hi Terje,

    Actually I am using nrf_rtc_counter_get() function for getting the counter value but it is showing only one value when i print on UART terminal. these are the statements i am using

             nrf_drv_rtc_t const *const    p_instance;
             temp=nrf_drv_rtc_counter_get   (p_instance)    ;
             printf("current value is=%d\r\n",temp);
    

    is it the right way ??

  • You probably haven't started a Low Frequency Clock, without one of those started, the counter doesn't count. You can start the RC one (pretty inaccurate) or the XTAL one (if there's a crystal mounted) which is more accurate. However, you have to start some LF clock.

    Usually this is taken care of by the softdevice initialisation as that needs an LF clock source, however if you're not using a softdevice, ie no bluetooth, then nothing else is running one.

  • Hi,

    i am able to read the value of counter by using

    count = NRF_RTC0->COUNTER;

    Thanks for your support!!