RTC Revisited

It’s been quite some time since I last looked at the RTC module for the Topsy Turvy Clock. I’ve found a suitable rechargeable battery but until now I’ve been focusing on the motors and mechanisms.

When I tried to get the code for the RTC working with the time.h library I found that it was behaving quite strangely, the time was jumping all over the place and setting the time and date was not working.

I swapped over to my earlier test code and that was working correctly. The problem came when I was working with the tmElements_t structure. I dived into the library and discovered that this was a series of unsigned 8 bit integers i.e. values 0 to 255. This is obviously fine for the hours, minutes and day of the month but the year is currently 2014 which won’t fit into the space. When I investigated further I discovered that the tmElements_t structure is expecting the year to be the number of years since 1970. I also looked at the data sheet for the DS1307 and that was expecting the year to be a value from 0 to 99 representing the number of years since 2000.

Once I adjusted the code to compensate for this (using the macros in time.h) the setting and reading behaved correctly.

This is the disadvantage of using a structure rather than a class in that a class could check the data and avoid an overflow, although this is difficult to check at compile time. The 2 digit year also means that my clock will have a Y2K1 issue when the clock rolls over to 2100.

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available