This tutorial will help you to interface DS1307 RTC with PIC16F877 Microcontroller. The project is compiled in CCS Compiler and simulated with Proteus. The Real Time Clock is interfaced with PIC controller via I2C. I2C is a 2 wire communication protocol. I2C is used for moving data from one device to another simply and quickly. It is a serial, synchronous, Bi-Directional protocol, the data is synchronised Β with clock through SCL pin and it controls when data is changed and when it should be read. All device is controlled by master deviceβs clock, No data is transferred in the absence of clock.
As mentioned above data is bi-directional, master device can both transmit and receive data. The serial data (SDA) and Serial Clock (SCL) pins must be pulled up with resistors, the value of resistors determine the speed of communication.
#include <16F877.h>
#include <lcd_4bit.c>
#use delay(clock=4000000)
#define RTC_SDA Β PIN_C4
#define RTC_SCL Β PIN_C3
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
void rtc_get_time() ;
int convert(int k);
int sec,min,hr,k[20],z,k1[20];
int day1,date,month,year;
void main()
{SETUP_ADC(ADC_OFF);
lcd_init();
while(1)
{rtc_get_time();
lcd_cmd(0x80);
sprintf(k,β%02d:%02d:%02dβ,hr,min,sec);
lcd_array(k);
lcd_cmd(0xc0);
sprintf(k1,β%02d:%02d:%02d β,date,month,year,);
lcd_array(k1);
lcd_cmd(0xc9);
switch(day1)
{
case 1: lcd_str(βSundayβ);
break;
case 2: lcd_str(βMondayβ);
break;
case 3: lcd_str(βTuesdayβ);
break;
case 4: lcd_str(βWednesdayβ);
break;
case 5: lcd_str(βThursdayβ);
break;
case 6: lcd_str(βFridayβ);
break;
case 7: lcd_str(βSaturdayβ);
break;
} }}
void rtc_get_time()
{
Β i2c_start();
Β i2c_write(0xD0);
Β i2c_write(0x00);
Β i2c_start();
Β i2c_write(0xD1);
Β sec = i2c_read();
Β min = i2c_read();
Β hr Β = i2c_read();
Β day1=i2c_read();
Β date=i2c_read();
Β month=i2c_read();
Β year=i2c_read(0);
Β i2c_stop();
Β sec= convert(sec);
Β min= convert(min);
Β hr= convert(hr);
Β day1= convert(day1);
Β date= convert(date);
Β month= convert(month);
Β year= convert(year);
Β z=βaβ;
if(hr>12)
{
hr=hr-12;
z=βpβ;
}
}
int convert(int k)
{
int a0,a1,a;
Β a0=((k&0x0F));
Β a1=k>>4;
Β a=((a1*10)+a0);
Β return a;
}
Above shows Β a simple program to read the Time value from DS-1307 IC. RTC Ds-1307 consumes very low power(500nA), so it can be backup by a battery for power failure issues. Β Β For DS1307, the device address is D0. The data, for example seconds value, located in the zeroth register is taken and converted from BCD to integer, before displaying it on the LCD. We can also write the Time value to Ds1307 registers one by one.