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.
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
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.
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:
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
RS – RB0
RW – RB1
EN – RB2
Data Lines – Port D
SDA – RC4
SCL – RC3
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.
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);
unsigned char sec,min,hour,day,date,month,year;
void rtc_send(unsigned char a);
void rtc_send_byte(unsigned char addr,unsigned char data);
unsigned char rtc_read();
unsigned char rtc_read_byte(unsigned char addr);
unsigned char convup(unsigned char bcd);
unsigned char convd(unsigned char bcd);
while(!SSPIF); // SSPIF is zero while TXion is progress
void rtc_send(unsigned char a)
void rtc_send_byte(unsigned char addr,unsigned char data)
unsigned char rtc_read()
unsigned char rtc_read_byte(unsigned char addr)
unsigned char rec;
unsigned char convup(unsigned char bcd)
unsigned char convd(unsigned char bcd)
#define rs RB0
#define rw RB1
#define en RB2
#define delay for(i=0;i<1000;i++)
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void cmd(unsigned char a)
void dat(unsigned char b)
void show(unsigned char *s)