Summary of Interfacing DS1307 Real time clock with PIC16f877
The article explains interfacing a DS1307 real-time clock (RTC) with a PIC16F877 microcontroller via I2C. It describes DS1307 features (BCD clock/calendar, 56 bytes NV SRAM, automatic month/leap-year adjustment, I2C slave) and outlines reading BCD-formatted time from the DS1307, converting BCD nibbles to ASCII digits, and displaying the result on an LCD. Sample conversions for higher and lower nibbles are given.
Parts used in the Interfacing DS1307 with PIC16F877 Project:
- DS1307 Real Time Clock IC
- PIC16F877 microcontroller
- 16x2 LCD display (or compatible LCD)
- I2C pull-up resistors (typically for SDA and SCL)
- Crystal/resonator for DS1307 (32.768 kHz)
- Backup battery for DS1307
- Power supply (VCC and GND connections)
- Connecting wires and PCB or breadboard
In any advance project we need real time clock synchronize with our work and for this purpose the best option is DS1307 (RTC Ic). Interfacing DS1307 Real time clock with PIC16f877 is done by I2C communication. To know I2C protocol you may refer my previous post “Interfacing external EEPROM with PIC Microcontroller”. To know about Interfacing DS1307 Real time clock with PIC16f877, we should know something about DS1307 IC.
Real time clock ( 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 I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The DS1307 operates as a slave device on the I2C bus. Access is obtained by implementing a START condition and providing a device identification code followed by a register address. Subsequent registers can be accessed sequentially until a STOP condition is executed.
Project Description:
In this project we will see how we interfaced DS1307 with pic16f877 via I2C protocol. After interfacing DS1307 we recive the data from DS1307 and display it on LCD. Weall ready know before that DS1307 is send data in full BCD format. So we have to convert this data in digit and send to display on LCD.
Now question is how we convert BCD number to 4 bit number?
For getting higher nibble we use command
return ((bcd >> 4) + ‘0’) : it means the number will be Binary Right Shift 4 times with loading zero on left side.
For getting Lower nibble we use command
return ((bcd & 0x0F) + ‘0’) : it means we did and operation 0F with BCD number.
For more detail: Interfacing DS1307 Real time clock with PIC16f877
- What communication protocol is used to interface DS1307 with PIC16F877?
The DS1307 is interfaced using the I2C protocol as described in the article. - What data format does DS1307 send for time and date?
DS1307 sends clock and calendar data in full BCD (binary-coded decimal) format. - How do you obtain the higher nibble from a BCD byte?
Get the higher nibble by right shifting the BCD byte by 4 and adding the ASCII zero: (bcd >> 4) + '0'. - How do you obtain the lower nibble from a BCD byte?
Get the lower nibble by masking with 0x0F and adding the ASCII zero: (bcd & 0x0F) + '0'. - Does the DS1307 adjust for month lengths and leap years?
Yes, the DS1307 automatically adjusts the date for months with fewer than 31 days and includes leap year corrections. - How is access to DS1307 registers obtained on the I2C bus?
Access uses a START condition followed by the device identification code and a register address; registers can then be read sequentially until a STOP is issued. - Can the DS1307 act as an I2C master?
No, the DS1307 operates as a slave device on the I2C bus. - What additional memory does the DS1307 provide?
The DS1307 provides 56 bytes of nonvolatile SRAM.
