Line 1: Line 1:
 +
[[Category:8051_tutorials]]
 
=Basics=
 
=Basics=
 
{{Box|type=l_green_light|text=
 
{{Box|type=l_green_light|text=

Revision as of 13:47, 27 March 2014

Basics

The Real time clock DS1307 IC basically is stand alone time clock. Well, basically we can use a micrcontroller to keep time, but the value would go off as soon as it is powered off.

The RTC DS1307 is a handy solution to keep time all the way, when it is powered by a coin cell.

It is uses I²C (Inter-Integrated Circuit) protocol, referred to as I-squared-C, I-two-C, or IIC) for communication with the micrcontroller.Check the basics of I2C here, if are not familiar with it.

Schematic

The schematic is very simple, LCD is connected in 4 bit mode. The Serial Clock (SCL) from DS1307 RTC is connected to P3.6 of 8051 and Serial Data(SDA) is connected to P3.7 of 8051. The lines are pulled up for stable voltages. 8051 RTC Interface.PNG

Code

The main program

#include <reg51.h> 
 
#include "lcd.h"   // xplore labz lcd library	 
#include "ds1307.h"  // Xplore Labz RTC library
 
/* start the main program */
void main() 
{
   unsigned char sec,min,hour,day,month,year;
 
  /* Initilize the lcd before displaying any thing on the lcd */
    LCD_Init();
 
  /* Initilize the RTC(ds1307) before reading or writing time/date */
    DS1307_Init();
 
 
  /*Set the time and Date only once */
   DS1307_SetTime(0x10,0x40,0x20);  //  10:40:20 am
   DS1307_SetDate(0x26,0x01,0x14);  //  26th January 2014
 
  /* Display "Time" on first line*/
  LCD_DisplayString("Time: ");
 
   /* Display "Date" on Second line*/
  LCD_GoToLineTwo();
  LCD_DisplayString("Date: ");
 
   /* Display the Time and Date continously */ 
   while(1)
    {
	   /* Read the Time from RTC(ds1307) */ 
        DS1307_GetTime(&hour,&min,&sec);        
 
		/* Display the time on Firstline-7th position*/
 
         LCD_GoToXY(0,6); 		 
         LCD_DisplayRtcTime(hour,min,sec);
 
 
	    /* Read the Date from RTC(ds1307) */ 
        DS1307_GetDate(&day,&month,&year);        
 
		/* Display the Date on Secondline-7th position*/
         LCD_GoToXY(1,6); 		 
         LCD_DisplayRtcDate(day,month,year);
     }
}

The libraries

The library files and functions used in the example can be found in 8051_Family_C_Library You may also download the entire project code at the bottom of the page.


The Results

The setup was tested on PRO_X51 8051 development board. RTC Interfacing with 8051.jpeg

8051_DS1307_RTC_Interfacing

{{#widget:Facebook_Like_Box|profile=http://www.facebook.com/XploreLabz}} {{#seo: |title=RTC Interfacing with 8051 |titlemode=append |keywords=8051,AT89s51,at89c51,p89v51rd2,XploreLabz,DS1307, Real time Clock |description= This tutorial describes interfacing Real time clock DS1307 with 8051 micrcontroller using I2C }} }

Have a opinion, suggestion or feedback about this article, let it out here!