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

Parents
  • 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

Reply
  • 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

Children
  • 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