This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

rtc time lag

Hello everyone,
I m interfacing a RTC of ds1307 with microcontroller c8051f020
I follow rtc datasheet
My circuit is working fine.
In the software, I m initializing the interrupt then smbus and after that rtc.
Initialization of rtc includes initializing internal oscillator and generation of 1 sec pulse
1 sec pulse is used as 1 sec interrupt
I read rtc after every one sec in while(1) loop. Flag is set in 1sec interrupt routine and display time on lcd
My problem is, when I switch on the machine I find the lag in seconds
To test it I switched off and on the machine several times and it was noticed that seconds lag is increasing
To check again, I downloaded the program and reset the microcontroller for several times and run it
Still I get lag in seconds as I increase the resets of microcontroller
Is it a mistake of my code or hardware fault?
Lag is of few seconds but if this continues then in a month it will be in minutes
thank u all

  • when I switch on the machine I find the lag in seconds

    Please be more specific. What do you mean when you speak of a "lag in seconds"?

    I m initializing the interrupt then smbus and after that rtc.

    Did you consider that maybe by re-initializing the RTC, you interrupt its ongoing time measurements? That might well cause it to "forget" about the fractional second it was in, so it would fall behind real time by anything between 0 and 1 second.

  • I m initializing the interrupt then smbus and after that rtc.
    I always enable interrupts as the very last thing in the initialization.

    Erik

  • Yes, interrupts should be activated after you have made sure that the device is correctly configured.

    And as already noted, you should not reinitialize the RTC on every reboot. The RTC is expected to be supplied by a super-cap or battery, si it can perform it's work even when the main processor is rebooted or even without power.

    But the RTC will only do its work if you - on main processor boot - first check if it is up and running before you make the decision to write to the RTC. Never activate the start motor until you have checked if the engine is running or not....

  • thanx for reply
    yes u are right
    i was initialising the rtc on every reboot and due to this i was missing some seconds
    so i commented initialisation code and checked several reboots
    now it is working fine
    so i am writing a code which will initialise the rtc only once
    but my doubt is that what if battery drains? i will need initialisation again?
    just now i am using a check which tells me that rtc is initialised and avoid the code of initialisation
    SOFTWARE:
    i have taken a flag whose status i save in eeprom
    when rtc is initialised its status becomes 1
    on next reboot, check is done it status is 1 then no initialisation
    for this i m using #define, #undef and #ifdef
    can i use above preporcessors in c file instead of header file

     if(flag == 0)
      {
       #define RTC_INIT 0
      }
     else
      {
       #undef RTC_INIT
      }
    

    and in rtcinitialisation routine which is next to above code:

    #ifdef RTC_INIT
    code of rtc initialisaton
    #endif
    

    can i write the above code in '.c' file
    because i m not getting any error but it is not executed it properly

    thank u all

  • Time to get a good book on the C language.

    Preprocessor defines can control the generation of C code.

    C statements can not affect preprocessor constants.

    Your test for initialized or not must set a C variable, and your code must test that C variable.

    You should not write into EEPROM if the RTC is initialized or not - your EEPROM will not know if you have lost power to the RTC.

    Read the data sheet to learn what happens with your RTC when it looses power. Verify that behaviour by modifying your circuit so that you can decrease the voltage to the RTC and check yourself what happens with the contents of the RTC.

    Some RTC has a flag that tells if they are up and running or not. Some have a checksum that can be read. Some just restarts the time/date from zero, so you can see if the time is many years older than your source code release. Some RTC may have even other methods, but the data sheet should give you the relevant information.

  • thanx for reply
    i read datasheet of ds1307 but there is no register which will tell me the status of power up or down
    so i m doing this eeprom method
    as my code is not running properly that means preprocessors can not be used in '.c' files
    pls correct me if i m wrong
    i debug the code step wise, but my flow does not go to that preprocessor statements
    it starts executing the code after preprocessor
    can u suggest me any link for preprocessors
    thank u all

  • The preprocessor is for controlling code when you compile, since the preprocessor is run before the compiler.

    You have to decide if the RTC have valid contents when your program is running, i.e. obviously after you have compiled the program.

    So yes, you are wrong if you think that the preprocessor can be used. The preprocessor isn't called a preprocessor for nothing ;)

    If you do not have a specific bit to test in the RTC, then you will have to create a checksum and store in the NV-RAM memory.

    On startup, verify the checksum and verify that the RTC has the clock running and verify that all RTC cells have valid contents and verify that the date is within reasonable range.

    If any test fails, you will have to initialize the RTC and request a date and time from the user.

    Writing a bit in an EEPROM memory can only tell your program that you have initialized the RTC at least once. It will not tell your program if the RTC has drained it's backup battery and is requiring a new initialization.

  • hello;
    thanx a lot, you have solved ny query regarding preprocessors
    now, i m doing the eeprom method for rtc one time initialisation
    for battery drain: i m checking one rtcread flag which i set after every 1 sec. this 1sec pulse i get from rtc.
    in while(1) loop i check this flag and if this is not set after 1sec then i initialise the rtc again
    and my software is working
    any suggestion, please let me know
    thank u all