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 through the interfacing the DS1307 real time clock with PIC microcontroller article.  

 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 resisitor.
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. refer to the Interfacing LCD Display with PIC microcontroller article to learn more.
The I2C debugger is shown on the circuit for simulation purpose only.


MPLAB XC8 compiler is used to write the code.
MPLAB XC8 provides built-in libraries for I2C devices which make it easy to read/write date and time information to/from the DS1307 chip. 
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.
The PIC18 Peripheral Library help document found inside the installation folder of CX8 compiler contains the full description of the functions and macros to write or read from the I2C bus.

DS1307 RTC is fully Binary Coded Decimal (BCD) clock/calender. 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.
MPLAB CX8 Library functions for Interfacing LCD With PIC Microcontroller need Character or String Data. So data to be displayed in the LCD Screen should be converted to Character. 
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.

Functions that are used for reading (read_ds1307()) and writing (write_ds1307()) data from DS1307 are explained in great detail in the article Interfacing the DS1307 with PIC Microcontroller please refer it. 

The define MSB() and the define LSB() are used to convert BCD to Character.

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 Code:


If you are using XC8 v1.35, the Peripheral Libraries which include the LCD and other peripherals like ADC, SPI, I2C libraries are no longer included in the installation file as with previous versions. You can either write your own LCD library and use MPLAB code configurator to configure the other peripherals or the easiest is to download the Peripheral Libraries as a separate download and install them,. You can download them at microchip website under the MPLAB XC Compilers downloads tab. They are now called PIC18 Legacy Peripheral Libraries.

Download: PIC18 Legacy Peripheral Libraries

You can download the full project files (MPLAB XC8 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 Digital Clock Header  

Download Digital Clock HEX  

Download Digital Clock Main C  

Download Digital Clock Proteus