DS1307 RTC with GMT/BST auto-adjustment

hippy

Technical Support
Staff member
This code shows how to take the time data from a DS1307 RTC ( I2C Real-Time Clock ) and automatically adjust the date and time to local time, GMT (UTC) or BST for the UK. The DS1307 always keeps track of date and time as GMT, and time setting must be done using GMT.

BST starts on the last Sunday of March at 01:00:00 GMT, and finishes on the last Sunday of October at 01:00:00 GMT / 02:00:00 BST. The algorithm basically checks if it's between those times and if so adds an extra hour to what the DS1307 GMT date and time returned was, handling the roll-over during 23:00:00-23:59:59 GMT.

The code relies on the DS1307 automatically handling day roll-over adjusting the entire date including day of week ( held as 1-7, Sun-Sat, within DS1307 but converted to 0-6, Sun-Sat within the code ). The code only works for years 2000-2099, but so too does the DS1307. When we reach year 2100, the code can be adjusted to add and subtract days to compensate for the wrong leap year handling the DS1307 will present. Anyone trying is on their own; it's unlikely I'll be around then to help :)

All date and time information is kept and handled as BCD but code is written so it should be usable with any PICAXE, not just the X2 which have BCD conversion unary operators. Only tested with 28X2.

Output to AXE033 and/or Programming Editor Terminal display.
 

Attachments

1968neil

Senior Member
setting the time

Hi Hippy,

Just tried the code posted with an 18M2 and the FRM010 LCD Firmware chip.

All is working well, however , How do you set the actual time ?
I can't see in the code a setting for this.

Or am i being a plonker and missed the obvious ? :confused:

Regards
Neil

Great piece of very useful code by the way
 

hippy

Technical Support
Staff member
I didn't include any time setting code as that is usually project specific ( buttons, serial, GPS ). You can use the Programming Editor Datalogger Wizard to set the initial time.
 

1968neil

Senior Member
makes sense now,
Just as a note for others using this setup :

Set the time using the wizard then disconnect the ds1307 data lines before you program the 18M or the time will be incorrect, i found this after much head scratching ! I would imagine on the data logger there is a link that has to be removed ?
Thanks again Hippy

Regards

Neil
 

hippy

Technical Support
Staff member
There's some time setting and test code in my program to demonstrate that it works and for testing.

Those are the 'SetupInitialDateAndTime' and 'Test' routines. Simply edit or remove those if that's what's causing the problem.
 

kollinsb

Member
I was going to use thos for a new project I am doing. Is it best to just subtract 6 hours (or 5 depending on the situation)? Right now I'm on Central Daylight Time and it's 5:00 pm on 7/23/2011. Just wanting to save time where I can but want to figure it out myself.

Thank you.
 

hippy

Technical Support
Staff member
@ kollinsb : If I were in your situation I'd set the DS1307 to local non-daylight saving time and then let it jump forward an hour during DST. That way the code should work virtually as is, except you probably have to change the date/time when DST starts and ends.

You don't have to use GMT, that just happens to be 'local time' for me. It's really local / local+1.
 

1968neil

Senior Member
There's some time setting and test code in my program to demonstrate that it works and for testing.

Those are the 'SetupInitialDateAndTime' and 'Test' routines. Simply edit or remove those if that's what's causing the problem.
Spot on Hippy, many thanks fop that, i will post a finished project at a later date, im building a rain water harvesting system at home and the time and date is very handy for automated sprinklers etc . Thanks again !
Regards
Neil
 

Paix

Senior Member
Not exactly related perhaps. Good for things in everyday real time control, but time can be a nightmare in situations where you need to know if the summer time offset is currently in use or not, the data record was written when the offset was in use or not and when comparing date/time for elapsed times, are you comparing times in the two periods etc.

I'm sure that forumeers won't run into these problems often with Picaxe systems, but standing in high relief but twice a year, it can take a couple of years for even the big boys to get their acts totally together - ie Sun / Ingres Corp (circa 1990).

It's only time - simple, until you has to deal with some of the more mundane detail - don't think global, that makes it worse. think "All times zulu" KISS :)

On our local breadboards we are so protected - enjoy.

Thanks for the technical detail Hippy and to 1968neil for pointing out a pitfall likely to remove the final follicles for the unwary.
 
Top