Digital Clock using PIC Microcontroller and the DS1307 Real Time Clock Circuit Diagram

A Digital Clock can be made easily by using PIC Microcontroller and the DS1307 real time clock with an LCD or seven segment display.
The DS1307 is a low power serial real time clock/calender from Maxim Integrated with full binary coded decimal (BCD) clock/calendar plus 56 bytes of Non Volatile Static Random Access Memory. 
Data and Address are transferred serially through a bidirectional I2C bus. The RTC provides year, month, date, hour, minute and second information. The end date of months is automatically adjusted for months fewer than 31 days including leap year compensation up to year 2100. It can operate either in 24-hour format or 12-hour format with AM/PM indicator. 
DS1307 comes with built-in power sensing circuit which senses power failures and automatically switches to back up supply. Timekeeping operation continues while the part operates from the backup supply. 
The DS1307 RTC uses an external 32.768kHz Crystal Oscillator and it does not requires any external resistors or capacitors to operate.

The PIC 18F2620 has a built-in I2C (Inter-Integrated Circuit) bus, any suitable PIC Microcontroller with I2C bus can be used as well for this project.
Although a digital clock can be built without an external real time clock chip, by using only internal PIC timer, The DS1307 RTC and simillar chips makes the software easier as it takes care of all calendar functions, adjustment of for months with fewer than 31 days, accounting for leap years and other functions which could have make the code more complex. The other advantage of using the DS1307, it comes with built-in power sensing circuit which senses power failures and automatically switches to back up supply so it can keep the real time information when the main circuit loses power. 
Before continuing, be sure to read this article:

Interfacing the DS1307 real time clock with PIC microcontroller.

 Project Circuit diagram

As shown on the circuit diagram above, the DS1307 is connected to PORT C of the PIC which has built in I2C bus (pin 14 and 15 of the 18F2620). Connect two pull up resistors (R1 and R2) for the bus to work. 
Connect a 3V backup battery (B1) to pin VBAT (pin 3) of the DS1307 to backup the device data but if this is not required, this pin can be grounded.
On our circuit, we are using internal oscillator of the PIC and the MCLR is disabled. If an external oscillator is needed, it can be connected to pins 9 and 10 and if the MCLR is needed to reset the PIC, it can be connected to positive supply via a 10K resistor.
Three push buttons connected to PORT C as well are used to set the date and time, once the SET button is pressed, the device enters the setup mode. Pressing the UP button will increment the the Hour and pressing the DOWN button will decrement.
Pressing the SET button again will move the cursor to Minutes, pressing again to seconds than to day and so on.
A 16 x 2 lines LCD display is connected to PORT B.

To learn more how to use an LCD, refer to the article: 

Interfacing LCD Display with PIC microcontroller.

The I2C debugger is shown on the circuit for simulation purpose only. The MCLR pin is disabled in software.

Project Code

MikroC Pro for PIC  compiler is used to write the code.
MikroC Pro for PIC provides built-in libraries for I²C devices. DS1307 works as a slave device on the I²C bus. 
Registers containing the date and time information can be obtained by implementing a START and followed by device identification address. Then each registers can be accessed sequentially by using its address until a STOP condition is executed. 
Device Address of the DS1307 is 0x68 = 1101000 (page 12 of datasheet). 
More details can be found from the DS1307 Datasheet.
DS1307 RTC is fully Binary Coded Decimal (BCD) clock/calendar. So the data read from DS1307 should be converted to required format according to our needs and data to be written to DS1307 should be in BCD format as well.
MikroC Library functions to display characters to LCD need Character or String Data. So data to be displayed in the LCD Screen should be or converted to String. 
Addition and Subtraction cannot be directly applied on BCD when updating the time and date. The functions to update data should convert it first from BCD to Binary and vice versa when needed.
The BCD2Binary() function convert BCD to Binary so we can do some basic calculations and the Binary2BCD() function convert Binary to BCD so we can write to the DS1307 Register in BCD.

Hour, Minute, Second, Date, Month and Year data are stored in DS1307 in separate 8-bit registers in BCD format as shown in the DS1307 Datasheet on page 8. We read the data of these registers to access time/date. The define MSB() converts most significant 4 bits to corresponding character and the define LSB()  converts least significant 4 bits to corresponding character.  When writing to the hour register, Bit 6 is used to select the 24-hour or 12-hour mode selection bit. When this bit is made high (1), 12-hour mode is selected and Bit 5 will represent AM/PM (Logic High represents PM).

Full Project Source Code:

You can download the full project files (MikroC source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files). 

Download MikroC Source Code

Download  Digital Clock Proteus Schematic