9 June 2011
Author: Giorgos LazaridisLCD PIC Clock with the Maxim DS1307 RTC
A friend of mine asked me to design a home automation for him. He wants me to build something that will communicate with his air condition through an IR receiver that it has, and it will act as a programmable timer, to turn it on and off automatically. Then i thought that i should spice it up a little bit, and make a programmable IR universal timer, or something. No matter what, i will certainly need to interface an RTC (stands for Real Time Clock) chip. This chip is responsible for keeping the time.
This circuit will help you understand how to use the built-in I2C communication module of PIC microcontrollers, and also how to use the Maxim DS1307 Real Time Clock chip.
The Maxim DS1307 is one very good solution
Being a PIC circuit designer has one great advantage over using arduino or other development platforms: It gives you the ability to learn how to choose the right chip for your job. The greater the experience i get, the more the parameters i include in my search criteria, parameters like price, package size, functionality, extra features and more. This time, i decided to use the DS1307 RTC chip from Maxim.
The chip uses I2C protocol to communicate with the microcontroller. I will use the PIC 16F1937, because it has enough pins for my future project and it has a built in I2C module, the MSSP. This is a remarkable feature for this microcontroller. The module can be configured to operate with 7 or 10 bit addressable clients. All the hard job is done by hardware, which makes the assembly sheet much more smaller and simpler to follow.
The DS1307 has an extra power pin to connect a 3V coin battery. This provides power to the chip whenever the main power supply is off. The consumption in that case is as low as 500 nA. According to the manufacturer, a lithium battery with 48mAh will back up the chip for more than 10 years in the absence of power!
One thing that has to be taken into account is the crystal. The chip uses an external 32.768kHz crystal for the oscillator. When choosing one to buy, you must choose one with Load Capacitance 12.5 pF. Other value than that may result in faster or slower pulses. Choosing for example a crystal with load capacitance 6pF causes the RTC to be about 3 to 4 minutes per month faster.
Another nice feature with this chip is that it provides 56x8 extra RAM space for data saving. Each time/date set has 7 bytes of data, so you can store 8 full time/date sets (8x7=56). You can store for example alarm notifications.
The circuit is very simple. Click the following schematic to enlarge it:
The LCD occupies pins 21 through 28, except pin 23. This pin (RC4/SDI/SDA/T1G/SEG11) along with pin 18 (SEG6/SCK/SCL/RC3) are used to interface the DS1307 chip. The PIC 16F1937 has a built in I2C hardware module. This module will do all the dirty work to communicate with other chips using I2C protocol. I do not need to use any external library, as the connection is done by hardware.
I added a big 1000uF capacitor in parallel with the battery back-up for only one reason. This capacitor will act as a back-up-back-up power if the main power is turned off and the battery is removed. The capacitor holds enough current to keep the RTC running for some time, even without the coin cell batter.
As mentioned before, the crystal must have 12.5 pF load capacitance to match the internal capacitance of the chip. Otherwise there will be an unwanted timing offset.
There are 7 push buttons to set the time and date. The first 3, are to set the date (day month year), while the other 3 are to set the time (hour min sec). Each time a button is pressed, the value is increased. If the seventh button is pressed simultaneously, then each time a button is pressed the value is decreased.
Before you get a headache - Your clock may run fast or slow!
There is one rule when you start playing with a new chip: Read the datasheet (RTFM)! STUDY the datasheet i might say. A detail may cause you a headache. First of all, something regarding the backup battery. The battery MUST be included in the circuit, otherwise it will not operate normally. If you do not wish to use a backup battery, then you have to ground the VBAT pin.
Another problem that will occur when you prototype this circuit on a breadboard, is that it will probably run fast. Every 5 to 10 minutes, your clock will be 1 to 2 seconds faster than the real time. That is absolutely normal. The reason? Parasitic pulses on the crystal. If you wish to make this circuit and you also wish to be accurate enough, then you HAVE to follow the guidelines of this document:
Application note 58 - Crystal Considerations with Maxim Real-Time Clocks (RTCs)
There is one nice test to see if your circuit is ok, even if it is built on a breadboard. First synchronize it with a clock that you know it runs accurate (NOT WITH YOUR PC CLOCK!!!). Then, remove the external power supply completely, and let the circuit run on the backup batter for a couple of hours. Re-connect the external power supply and see if there is any difference between the 2 clocks. With this trick, you eliminate the parasitic pulses from the cables on the breadboard.
The PIC software
Here are all the files for this project:
Bill Of Materials