A Real Time Clock using PIC16F88 Microcontroller
This PIC project uses an I2C Real Time Clock IC (DS1307) and a four digit seven segment display to create a standard desk clock.
Note: If you typed DS1703 Real Time Clock to find this page you probably mis-spelled the chip type. Anyway you can find a DS1307 (RTC) Real Time Clock IC project and information here.
Real Time Clock IC : DS1307
Although the PIC16F88 has a built in oscillator for a 32kHz watch crystal a DS1307 is easier to use on a bread board. This is because you can control the layout of the circuit more easily.
The RTC also makes the software easier as it takes care of all calendar functions; accounting for leap years etc.
The DS1307 (RTC) Real Time Clock IC (an I2C real time clock) is an 8 pin device using an I2C interface (although the data sheet does not mention I2C to avoid royalty payments!). It has 8 read/write registers that store the following information:
Note: Addresses 0x08 to 0xf3 are user RAM and if you use a backup battery these are then non volatile ram i.e. they will save their contents after the power is off – so you have an extra 56 bytes of ram to play with!
Note: Address 3f is used in this project as a check to see if the clock needs initialising and to store the upper year digit (for easier coding).
The last address 0x08 is the CONTROL address and it determines what is generated at the SQW/OUT pin. You can control the level directly via I2C or set it to 1Hz, 4096Hz, 8192Hz, or 32768kHz.
In the same way as the I2C pins you need to add a pull-up to V+ at the SQW/OUT pin to see any output signal as it is an open drain output!
Real Time Clock IC : Embedded control bits
There are two specific ‘gotcha’ type controls embedded in the addresses which make using the chip slightly more complicated.
Real Time Clock IC DS1307 : Clock halt
The most important is the Clock Halt Bit (CH) which is bit 7 of address 0. This is the regeister that controls ‘seconds’ and the CH bit has to be preserved otherwise the chip stops the clock. Writing zero to this bit resets the CH bit so that the clock runs.
You have to reset the CH bit to zero to let the chip operate!
Real Time Clock IC DS1307 : 24/12 Hour control
The second is the 24/12 hour control which is bit 6 of address 2. It is set high for 12 hour mode and low for 24 hour mode.
The problem with these two bits is that you have to preserve them when accessing the registers to write data and ignore them when reading out values for display. Its not a big problem and you can see how it’s done when you look at the code.
Real Time Clock IC : 32kHz oscillator
Surprisingly making an accurate 32kHz oscillator is a difficult task (much more than a high speed oscillator e.g. a Mhz crystal oscillator). This is because low speed oscillator drivers are designed for low power operation. That means high impedance and therefore low current which makes the driver extremely sensitive to noise (or any nearby signals which can capacitively couple to the crystal wire).
Using the DS1307 lets you put the crystal in the least noisy part of the board. In addition it sets the crystal load capacitance which is critical in making the crystal oscillate at exactly 32kHz – controlling its initial error i.e. for the specified ppm error value the load capacitance must be exact.
Note: A common way of calibrating a crystal (not in this project) is crystal pulling or changing the capacitance at one crystal pin relative to the other – so load capacitance is crucial.
The DS1307 loads the crystal with 12.7pF so you need to buy a crystal that is defined to use this load capacitance. Circuit layout also affects the capacitance at the crystal pins so you must keep the crystal as close as possible to the chip and the tracks from crystal to chip must be short.
For more detail: A Real Time Clock using PIC16F88 Microcontroller
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
JLCPCB Prototype: Only $2 for 10pcs 10×10cm, 24 hours Quick Turn, DHL Delivery in 3 days
Current Project / Post can also be found using:
- pic 16f88 clock i2c