Word clock; RTC

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
  • cheap
  • 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.

Advertisements

Word clock; demo video

The following things are not yet done for the Word Clock: 

  1. Case
  2. Real time clock
  3. Human interface

However, I have been able to write the display routines, and a basic loop around the the display. The idea is that the display routine display_time(char hh,char mm) takes two variables, the hour (in hh) and minutes (in mm), and figures out which LEDs to light, and sets an array of globals.  A second routine show_time() pushes the globals out to the shift registers in the right order. show_time() has to be called often, as we are multiplexing the LEDs, but display_time() only needs to be called when the time changes.

As the RTC isn’t yet implemented, looping through the times can then be set to run fast, so that you don’t have to wait 24 hours to test the display logic. 

The video is here: http://youtu.be/iacYOHE3SZk

No audio, because we were watching BSG whilst testing, so I stripped that out.

RTC thoughts

I’ve wired in a DS1305 RTC chip, which has multiple functions. It can operate as a stable 1 second pulse (I’ve wired to a pin on the PIC that allows interrupt on change), it can raise alarms, and it can also, over a serial link, give you a binary string representing the time (including a date if required!). I haven’t decided whether just to use it as a reliable 1 second pulse, and do all the time counting in the PIC code, or to off load all time counting to the DS1305, and just ask it every minute (or so) what the time is, and feed that to the display_time(hh,mm) routine. Current program storage use is only 60% at the moment, so should have room for either. I’ll probably try having the DS do the time keeping and see if it all fits in the code space.