1


RTC ds-1307

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.  


Circuit Diagram To Interface DS-1307 With PIC Microcontroller


Ds1307 with Pic16f877



Embedded C Program for DS-1307 I2C 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. 

Simple Digital Clock using Ds-1307 and PIC Microcontroller

Digital clock using DS-1307


Post a Comment

 
Top