A Real Time Clock IC (DS1307) project using the PIC micro.

 This PIC project uses an I2C Real Time Clock IC (DS1307) and a four digit seven segment display to create a standard desk clock.

RTC DS1307 Real time clock IC circuit diagram
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:
Address Register function
0 Seconds 0-59
1 Minutes 0-59
2 Hours 0-24,1-12
3 Day 1-7
4 Date 1-31
5 Month 1-12
6 Year 0-99
7 Control
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.
To ensure the crystal oscillates correctly you must ensure that :
  • Crystal uses 12.7pf load capacitance (correct crystal type).
  • The crystal is close to the IC.
  • The tracks are short.
  • The chip supply has lots of decoupling (capacitors from +5V to GND). e.g. A 100n and a 10n
  • There are no signal tracks near to the crystal.
  • For a pcb: It has a guard ring and a ground plane and away from digital signals.
If you are doing a board layout there is good advice in the PIC 16F88 datasheet (Timer 1 section) on crystal pcb guard rings. Dallas recommends Application note 58 which I have not read yet.

Real Time Clock IC : Power failure.

The DS1307 detects a power failure if its input voltage (Vcc) falls below (VBat) and automatically switches to the Vbat supply input (you should use a lithium 3V battery here as the backup battery). It also inhibits I2C control signals until Vcc is 1.25 x Vbat so you won't be able to put bad data into the chip as the power is failing!

Input keys

To save microcontroller pins there are four input keys which are all connected to a single analogue input pin. This pin also drives one of the seven segment display LEDs so it has to be switched between input (to read the analogue voltage) and output (to drive the led).
Each key pulls the analogue input to a different voltage level which you can easily read using the ADC (RA0).

Using the Real time Clock IC project

When the system powers up the ram location 0x3f is checked for value 0x20. If this exists then it means that the backup battery has saved contents of the RAM and all the registers have therefore been initialised. So the software skips the initialisation sequence.
If 0x20 is not found then the Real Time Clock IC is initialised to the following values:
Address Initialisation value
0 Seconds 0x59 (bit 7 cleared).
1 Minutes 0x59
2 Hours 0x24 (bit 6 cleared).
3 Day 0x07
4 Date 0x31
5 Month 0x12
6 Year 0x99
These are all Binary Coded Decimal numbers which the Real Time Clock IC works with. Note that the initialisation values have been chosen so you can easily understand what the display is showing.

Mode button

Pressing mode button cycles the display showing different data after each button press. The following display sequence is followed:
  1. Minutes, Secs.
  2. Hours, Minutes.
  3. Date, Month.
  4. '20', Year.

Edit, Up and Down buttons

To change a value the Edit button must be pressed. When it is the left hand two digits are flashed on and off and the Up and Down buttons now let you edit the value. Hitting Edit again flashes the two right hand digits and you can change the value as before. Hitting the Edit button or the Mode button exits the edit mode.
The clock is only stopped when editing the Minutes/Seconds display and restarted at the end of the edit sequence or when the Mode button is hit. This lets you set the seconds accurately but does not disturb the clock in other modes.

Real Time Clock IC hardware

Real Time Clock IC project
Click digital clock schematic to open a pdf document.
RTC Real time clock ic schematic using DS1307
You can use any PIC microcontroller that has an ADC and enough memory to hold the program and enough pins for the display multiplexing.
You can program the PIC in circuit through the ICSP connector.
Real Time Clock IC hardware block diagram
There is nothing realy difficult about the project except for placing the DS1307 away from noise sources and decoupling it as mentioned earlier.
One point is that the multiplexed display does not use any current limit resistors that you would normally expect. You can get away with this if the display is on only for a very short time so that the average current out of the pin is kept small. Turning them on for a long time would draw too much current possibly damaging the microcontroller.

Real Time Clock IC project Software

Project files

Compiler project files
16F88_RTC_DS1307.ppc
C Source files.
16F88_RTC_DS1307.c
I2C.c
Header files.
bit.h - bit manipulation macros
I2C.h
Output files
16F88_RTC_DS1307.hex

Description

16F88_RTC_DS1307.c

This contains the main routine and support functions for controlling the DS1307.
  • init_ds1307()
  • read_ds1307()
  • write_ds1307()
  • edit_ds1307()
  • stop_ds1307()
  • start_ds1307()
These are all self explanatory.
Also in the file are analogue key reader function read_ANA_keys() and display routines:
  • int2seg()
  • clock_display()
int2seg() - Converts a number into the required output value for PORTA to drive the seven segment display.
clock_display() - The complexity in this routine is for selectively blanking left or right pairs of digits so that they can be flashed on and off. Other complexity is preserving the control bits at addresses 0x00 and 0x02. Yet more complexity is in only turning on one seven segement display at each call to the routine.
Other standard routines are included for port initialisation and general initialisation.

I2C.c

This contains the software implementation of I2C (clock stretching has not been tested). To change the PORTs/pins alter the #define statements at the start of I2C.c

Operation

Operation of the software follows the description given earlier in section "Using the Real time Clock IC project"
The only other point to mention is the switching of the TRISA port for reading the ADC at the start of the while loop. This has to set the TRIS state correctly after the ADC read.
And thats it - enjoy.

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Engr.Arif Naseem | Bloggerized by Naseem - | Affiliate Network Reviews