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