PIC16F877A – RTC (DS1307) Interfacing

In this tutorial we are going to discuss RTC Interfacing with PIC16F877A. Before that we should know about the RTC (DS1307) operations. Let’s start.


RTC (DS1307)


The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I²C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.


  • Completely Manages All Timekeeping Functions
    • Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year with Leap-Year Compensation Valid Up to 2100
    • 56-Byte, Battery-Backed, General-Purpose RAM with Unlimited Writes
    • Programmable Square-Wave Output Signal
  • Simple Serial Port Interfaces to Most Microcontrollers
    • I2C Serial Interface
  • Low Power Operation Extends Battery Backup Run Time
    • Consumes Less than 500nA in Battery-Backup Mode with Oscillator Running
    • Automatic Power-Fail Detect and Switch Circuitry
  • 8-Pin DIP and 8-Pin SO Minimizes Required Space
  • Optional Industrial Temperature Range: -40°C to +85°C Supports Operation in a Wide Range of Applications
  • Underwriters Laboratories® (UL) Recognized

VCC, GND: These pins are used to provide the power to the chip. When 5V is applied within normal limits, the device is fully accessible and data can be written and read. When a 3V battery is connected to the device and VCC is below 1.25 x VBAT, reads and writes are inhibited. However, the timekeeping function continues unaffected by the lower input voltage. As VCC falls below VBAT the RAM and timekeeper are switched over to the external power supply (nominal 3.0V DC) at VBAT.

X1-X2:Pins to connect the external 32.768kHz oscillator that provides the clock source to the chip.

Vbat: A 3.3v Lithium battery can be connected to this pin in order to provide the power source when the external supply voltage is not available. Battery voltage must be held between 2.0V and 3.5V for proper operation.

SCL: This pin must be connected to SCL pin of the I2C Bus/Master.

SDA: This pin must be connected to SDA pin of the I2C Bus/Master.

SQW/OUT: When enabled, the SQWE bit set to 1, the SQW/OUT pin outputs one of four square wave frequencies (1Hz, 4kHz, 8kHz, 32kHz).

  • Note: The SCL,SDA and SQW are open drain and must be pulled up with appropriate pull up resistors

Connection circuit

DS1307 requires very less number of components to operate. One of the mandatory component is the crystal oscillator of 32.768kHz. The battery backup is optional as you can connect power source to Vbatt pin. But It is good to have battery connected. When running on battery it only consumes 500 nA of current just for the operation of clock and to maintain content of NV RAM. Your can use a coin cell type battery as backup source for DS1307.

Following image shows typical connection diagram for DS1307 RTC.

DS1307 Memory

All the registers of DS1307 stores value as BCD, i.e. if clock is at 49 seconds then register 0x00 will show 0x49 as binary coded decimal value of 49. This makes it easy for programmer to read and display number on LCD or serial terminal. So if you want to use register values of RTC then first convert the value from BCD to decimal and then use it.

The RTC keeps the date and time arranged in it’s memory as shown below:

Register 0x07 is control register for square wave output pin. RS0 and RS1 bit selects the output frequency as per table below:

You can also use SQW/Out pin as GPO pin, when SQW function of DS1307 is not used. Bit 7 controls the output level of the pin. If OUT bit is 1 then OUT pin is high and when 0 OUT pin will be low.

RTC Interfacing with PIC16F877A

 DS1307 uses I2C Protocol and act as a slave device and I2C Master can read/write register of RTC. To communicate with the slave device, master need the slave address of device connected on bus. DS1307 has fixed slave address which makes it impossible to connect two RTC devices on same bus, don’t worry occurrence of such a scenario is close to zero.
Slave address for I2C Write: 0b11010000 = 0xD0
Slave address for I2C Read: 0b11010001 = 0xD1

Circuit Diagram


RS –  RB0

RW – RB1

EN – RB2

Data Lines – Port D





In this code, I’m printing the Time, Date and the day of the week. If you want to download the full project, please click Here.




RTC Interfacing with PIC16F877A[ Please find the output image Here ]

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
September 15, 2017 6:12 AM

which compiler you are using?

April 2, 2018 11:12 PM

void rtc_send_byte(unsigned char addr,unsigned char data)
why use this function here..?

what is value of data..?
where u r call this function?

GS Ramesh
GS Ramesh
October 12, 2018 5:38 AM

hi admin.. i have 1 doubt why do you use convup and convud functions i didnot clear.. if i try to set time using button when it reach 10 it print as 1; similarly 1: instead of 11 what is prblm https://uploads.disquscdn.com/images/836717ea80e6b3c7c970f38a4059820bc946cc8733a1934958a9f985e8c4d49d.png

EmbeTronicx India
EmbeTronicx India
Reply to  GS Ramesh
October 14, 2018 5:48 AM

Hi Ramesh, Answer for Question 1: All the registers of DS1307 stores value as BCD, i.e. if clock is at 49 seconds then register 0x00 which stores the seconds will show 0x49 as binary coded decimal. So If we want to print than we need to print 4 first and 9 second. How we can get the values? See below explanation. Convup (convert the upper nibble of the byte to ASCII): 1. (0x49 >> 4) = 0x04 (We got value 4 using this step) 2. (0x04 + 48) = 52 (This is the value of 4 in ASCII. Now we… Read more »

GS Ramesh
GS Ramesh
Reply to  EmbeTronicx India
October 15, 2018 1:04 AM

pls adjust my english because i no only little bit. am not change anything in the source code i just added time set code with my knowledge and than my i2c is gud. below added my code for set the hour… if anything is worng pls teach me…thanks for your wonderful explanation.. if(set_button==0) // this s fr change the mode from time display to time edit. { cmd(0x01); while(one==0) // its just for continuse loop and it will terminate end of the time set. { cmd(0x82); show(“hrs”); cmd(0x86); show(“min”); cmd(0x8a); show(“sec”); cmd(0xc2); data(convup(hour)); // ur dat is modified as a… Read more »

EmbeTronicx India
EmbeTronicx India
Reply to  GS Ramesh
October 15, 2018 10:37 AM

Hi Ramesh,

Pls send your complete project code. Just compress and mail us to [email protected]. We will correct and send you. Because we need to test that first then only we can correct you.

Thank you.

August 9, 2020 6:37 PM

What if there is no ack from slave after address will the program stop executing further instructions if it so how can I overcome it

Would love your thoughts, please comment.x
%d bloggers like this: