Activity Forums Tutorials Proper usage of RTC

  • Proper usage of RTC

     Lukas updated 1 year ago 1 Member · 1 Post
  • Lukas

    Member
    September 4, 2020 at 12:06 pm

    Permanent (or very often) polling of the RTC chip causes drift of the RTC internal clock. It may slow down the “real time” for several seconds per day! The reason is that the RTC chip stops its internal clock counter during the SPI communication between the main processor and the RTC chip (to be able to provide consistent information about the time, alarms, flags etc.). To avoid this phenomena it is recommended to use the RTC only as a backup during temporary black-outs and as a source of a precise time. But the synchronization of the Arduino “system time” with the RTC should be done with a reasonable period – e.g. once per day.

    I have used Arduino Time library and prepared an example sketch for you.

    Enjoy!

    Lukas

    #include <TimeLib.h> /* https://playground.arduino.cc/Code/Time/ */

    #include <SPI.h>

    #include <Controllino.h> /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */

    /*

    CONTROLLINO - Demonstration of proper Real Time Clock usage, Version 01P01

    IMPORTANT INFORMATION!

    Please, select proper target board in Tools->Board->Controllino MINI/MAXI/MEGA before Upload to your CONTROLLINO.

    Do not forget to properly setup the mechanical switch at your CONTROLLINO MINI!

    https://controllino.biz/

    (Check https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library for the latest CONTROLLINO related software stuff.)

    */

    void setup() {

    Serial.begin(9600);

    Controllino_RTC_init();

    /* set time and date by separate values values to the RTC chip */

    /* Day, WeekDay, Month, Year, Hour, Minute, Second); */

    // Controllino_SetTimeDate(4,5,9,20,11,13,00);

    /* or use another possibility and define the time and date by strings, e.g. "Nov 15 2018", "11:41:02" */

    /* following example uses predefined C macros __DATE__ and __TIME__ which represent compilation time */

    // Controllino_SetTimeDateStrings(__DATE__, __TIME__); /* set compilation time to the RTC chip */

    syncSystemTimeWithRTC(); /* initial synchronization */

    }

    // the loop function runs over and over again forever

    void loop() {

    syncSystemTimeWithRTC(); /* performs the sync once per day */

    digitalClockDisplay();

    delay(5000);

    }

    int syncSystemTimeWithRTC()

    {

    unsigned char mHours, mMinutes, mSeconds, mDays, mMonths, mYears, mWeekday;

    char mStatus;

    static unsigned char mLastSyncDay = 0;

    if (mLastSyncDay == 0 || mLastSyncDay != day())

    {

    /* re-sync the system clock with the RTC for the first time and then once per day at midnight */

    mStatus = Controllino_ReadTimeDate( &mDays, &mWeekday, &mMonths, &mYears, &mHours, &mMinutes, &mSeconds);

    if(mStatus != 0)

    {

    Serial.println("Error - RTC is not responding!");

    return -1;

    }

    else

    {

    setTime(mHours,mMinutes,mSeconds,mDays,mMonths,mYears); // Set system time

    if(timeStatus()!= timeSet)

    {

    Serial.println("Error - Unable to sync with the RTC!");

    return -1;

    }

    else

    {

    Serial.println("System time was synchronized with the RTC.");

    }

    }

    mLastSyncDay = mDays; /* set the day of the last synchronization with the RTC */

    }

    return 0;

    }

    /* following functions are taken from example TimeRTC (Time library ) */

    void digitalClockDisplay(){

    // digital clock display of the time

    Serial.print(hour());

    printDigits(minute());

    printDigits(second());

    Serial.print(" ");

    Serial.print(day());

    Serial.print(" ");

    Serial.print(month());

    Serial.print(" ");

    Serial.print(year());

    Serial.println();

    }

    void printDigits(int digits){

    // utility function for digital clock display: prints preceding colon and leading 0

    Serial.print(":");

    if(digits < 10)

    Serial.print('0');

    Serial.print(digits);

    }

    /* END OF THE SKETCH */

Viewing 1 of 1 replies

Log in to reply.

Original Post
0 of 0 posts June 2018
Now