My previous version of the Word Clock didn’t keep time well. That’s because I used the PIC’s internal clock to generate an interrupt every n cycles, and counted those. Now, as a short term timer (say, for an electric toothbrush motor controller), that’s fine. But over 24 hours, small errors due to different temperatures, or, even worse, just guessing the value of n, will become significant.
To solve this, I decided use a Real Time Clock. After a little searching, I found the DS1305 from Maxim that had the following desirable properties
- 3 wire interface (and so is easy to interface to a PIC)
- 5V supply
- DIP case
- raises an “alarm” once a second
- in stock
The idea is then that on startup the PIC communicates with the DS, setting it up to raise an alarm once a second. The alarm is just a single line going low (and it needs pulling high as its an open drain output). The PIC then waits, and when it notices the line going low, it increments the second counter by one. Hidden in the documentation is the line that the alarm line will remain low until the PIC (in this case) writes to the DS again.
So why is the RTC better? Well, basically it removes the requirement for getting the PIC internal oscillator right. When I first read the datasheet, I noticed that the RTC also keeps a time and date stored, as yyyy/mm/dd:hh:MM:ss. A possible code flow for the PIC would be to set the RTC to the correct date and time, and then, roughly once per minute, ask the RTC what the time is, and then use that. I decided not to do that though.
The RTC also allows a small, low voltage battery (like a little lithium coin cell) can be used as a secondary supply for the RTC which will keep the oscillator running. If the Word Clock was going to be battery operated, this would be useful. But I’d already decided that swapping batteries might be a pain and that a wallwart PSU was cheap enough. And more advanced models of RTC have temperature compensation, so if that was deemed desirable, it could be used.
So the RTC is being underused. No matter.