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

problem with RTC counting

Hi guys, I'm facing some trouble to put my rtc working properly. I made a code based on the rtc example but it's not incrementing the time the right way.

Bellow follows my code, it's just to send the time through the uart each second.

I'm using the nRF51-DK (nRF51422, 32.768 kHz crystal) no softdevice, SDK10

static void lfclk_config(void)
{
    ret_code_t err_code = nrf_drv_clock_init(NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request();
}

static void rtc_config(void)
{
    uint32_t err_code;

    //Initialize RTC instance
    err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_handler);
    APP_ERROR_CHECK(err_code);

    //Enable tick event & interrupt
    nrf_drv_rtc_tick_enable(&rtc,true);

    //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
//    err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME*RTC0_CONFIG_FREQUENCY,true);
//    APP_ERROR_CHECK(err_code);

    //Power on RTC instance
    nrf_drv_rtc_enable(&rtc);
}


int main(void)
{
    leds_config();
    lfclk_config();
    rtc_config();
    uart_config();
    printf("\n\rExample1234567890\r\n");
    printf("\n\rRTC0_CONFIG_FREQUENCY=%d",RTC0_CONFIG_FREQUENCY);
    struct tm *timeinfo;
    time_t date_hour_seconds;
    while(1)
    {
      date_hour_seconds=nrf_drv_rtc_counter_get(&rtc);
      timeinfo = localtime(&date_hour_seconds);
      printf("\n\rCurrent date and hour: %s", asctime(timeinfo));
      nrf_delay_ms(1000);
    }
}

I've tried two configurations of RTC0_CONFIG_FREQUENCY on my nrf_drv_rtc.h file one using the value 8 which was on rtc example and other with the value 32768 which was on RTC1_CONFIG_FREQUENCY.

Example1234567890

RTC0_CONFIG_FREQUENCY=8

Current date and hour: Thu Jan  1 00:00:00 1970

Current date and hour: Thu Jan  1 00:00:08 1970

Current date and hour: Thu Jan  1 00:00:16 1970

Current date and hour: Thu Jan  1 00:00:24 1970

Current date and hour: Thu Jan  1 00:00:32 1970

Current date and hour: Thu Jan  1 00:00:40 1970

Current date and hour: Thu Jan  1 00:00:48 1970

Current date and hour: Thu Jan  1 00:00:56 1970

Current date and hour: Thu Jan  1 00:01:04 1970


--------------------------------------------------------

Example1234567890

RTC0_CONFIG_FREQUENCY=32768

Current date and hour: Thu Jan  1 00:00:27 1970

Current date and hour: Thu Jan  1 13:51:32 1970

Current date and hour: Fri Jan  2 03:36:03 1970

Current date and hour: Fri Jan  2 17:20:34 1970

Current date and hour: Sat Jan  3 07:05:05 1970

Current date and hour: Sat Jan  3 20:49:36 1970

Current date and hour: Sun Jan  4 10:34:08 1970

Current date and hour: Mon Jan  5 00:18:39 1970

Current date and hour: Mon Jan  5 14:03:10 1970
  • Hi,

    Despite what its name (real time counter) may suggest, this peripheral is not intended for keeping track of the current time. Rather, it is a low frequency timer which is set to tick at a set frequency. It can generate timer events for each tick, or for a certain number of ticks, or when the counter overflows.

    Using the RTC for keeping track of time is not necessarily a good idea. RTC runs off the LFCLK and will inherit the accuracy of that clock signal. If done correctly this means 250 ppm or better, but it may still be a better idea to use a dedicated clock IC for keeping track of time with second precision over long periods of time. (250 ppm is about 1 second per hour.)

    If you want to use the RTC for keeping track of time, you will have to set it up to generate an event at a certain period (say 1 second) and use that event to increment a variable holding the current time as a unix timestamp value (number of seconds since January 1st 1970). If initially set to the current time, this timestamp value will contain the current time (with a clock drift within the accuracy of the LFCLK source.)

    RTC0_CONFIG_FREQUENCY is the frequency for the RTC in ticks per second. What happens in your code is that RTC0 is initially starting at 0 (January 1st 1970 at 00:00:00) then incrementing at the RTC0_CONFIG_FREQUENCY frequency. As you read the RTC0 value every second, when you use a frequency of 8 Hz you will get timestamp values with increments of 8 seconds. Similarly you get larger increments when using a higher frequency. (It is not increments of exactly 32768 seconds because of inaccuracies both for the RTC and for the sleep function.)

    Regards, Terje

  • Hi

    i am working on implementation of real time clock using RTC in nrf52. from your comment it seems that you are getting the correct time and date values .Can you please help me in implementing the same.How to read the current value of counter in nrf 52 RTC example?

  • Hi Ankush, in fact the solution I tried didn't work as an Real Time Clock. You can check why on the answer below. I'll put an external RTC on my circuit, it'll work better.

  • Dear Terje, you write "RTC runs off the LFCLK and will inherit the 2 % accuracy of that clock signal." If I only need to program one device, can I measure the real frequency of the LFCLK (say, with an oscilloscope) and use the measured difference to make a more accurate "real time clock" using the RTC? Or will its frequency vary with each restart of the chip? Thanks, Tamas

Related