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 circuit with a 7-buttons keypad attached
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:
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.
I'm doing this project by instructions on this page,and I came across a problem with LCD display, it shows only 16 black boxes and blinking indicator, was wondering if you could help me to solve this problem
thanks in advance
The idea given by you is really good.. I am very much interested in doing this project. But instead of 16X2 LCD I want to use LED. Can you send the code for the same. It would be a great help to me.
Thanks in anticipation!
The 1000 µF capacitor is just for tinkering. Can't leave it there over the long term. it has 5-10 µA leakage current, so it sucks the the battery dry in 6 months, instead of the '10 years.' btw, 10 years supposes that the battery loses nothing as it ages. but from the geek department, the batteries lose everything in 5-7-10 years just sitting on the shelf, depending on how well made they are. So the lifetime will be about 6 years typical to keep running the oscillators (keep the accurate time) in the chip's registers. With a leakless battery, after the oscillators exhaust the battery in 10 years, there will still be enough to keep the last time stored for an additional 10 years beyond the oscillator circuit going kaput!
@Pete Goss as a matter of fact, you can use any PIC for this application. The RTC uses I2C for the interface, so a PIC with harware I2C will certainly make your life much easier, otherwise you will have to make the interface in software. PIC16F877 has I2C module (MSSP) so it is a good choice.You may need to alter the code though. Read my code first to get an idea how to setup the module.
@Kammenos. Thanks for your reply. Do you think this can be done with the PIC16F877? My PIC2 can't program the 1937. I'm very new to micro-programming, so it may take me a while. Thanks for the info about matrices. I'm going to use a 3 bit to 8 line decoder (74138) for the 7 days and 4 bits for the 4 times to create a 7 X 4 LED matrix.
I would like to know if you think this approach of using the RTC chip and a PIC would be suitable for a project I want to construct. An elderly relative has problems taking the correct dosage of medication, The pharmacist can supply a transparent tray with each dosage put into 28 seperate compartments. 4 for each day. The relative always get it wrong. I want some kind of clock circuit that will light up one of 28 LED's placed beneath each compartment of the tray. This would indicate which compartment she should open up. Also a buzzer would sound at the correct time. I'm think that I need a circuit that I could program to to turn on the correct LED every day at 9:00 - 13:00 - 17:00 and 21:00.
Do you think your approach could do this? Are there enough outputs from the PIC to drive 28 LED's?