Line 16: Line 16:
 
<syntaxhighlight>
 
<syntaxhighlight>
  
  #include <avr\io.h> // io.h contains the defnition of all ports and SFRs
+
  #include <avr\io.h>   // io.h contains the defnition of all ports and SFRs
#include "lcd.h" //User defined LCD library which conatins the lcd routines
+
#include "lcd.h"   //User defined LCD library which conatins the lcd routines
#include "ds1307.h" //User defined library which conatins the RTC(ds1307) routines
+
#include "ds1307.h"   //User defined library which conatins the RTC(ds1307) routines
  
 
/* start the main program */
 
/* start the main program */
Line 36: Line 36:
  
 
   /* Display "Time" on first line*/
 
   /* Display "Time" on first line*/
  LCD_DisplayString("Time: ");
+
  LCD_DisplayString("Time: ");
  
 
   /* Display "Date" on Second line*/
 
   /* Display "Date" on Second line*/
  LCD_GoToLineTwo();
+
  LCD_GoToLineTwo();
  LCD_DisplayString("Date: ");
+
  LCD_DisplayString("Date: ");
  
 
   /* Display the Time and Date continuously */  
 
   /* Display the Time and Date continuously */  
 
   while(1)
 
   while(1)
 
     {
 
     {
  /* Read the Time from RTC(ds1307) */  
+
/* Read the Time from RTC(ds1307) */  
 
         DS1307_GetTime(&hour,&min,&sec);         
 
         DS1307_GetTime(&hour,&min,&sec);         
  
 
/* Display the time on Firstline-7th position*/
 
/* Display the time on Firstline-7th position*/
        LCD_GoToXY(0,6);  
+
        LCD_GoToXY(0,6);  
        LCD_DisplayRtcTime(hour,min,sec);
+
        LCD_DisplayRtcTime(hour,min,sec);
  
 
 
    /* Read the Date from RTC(ds1307) */  
+
/* Read the Date from RTC(ds1307) */  
        DS1307_GetDate(&day,&month,&year);         
+
        DS1307_GetDate(&day,&month,&year);         
  
/* Display the Date on Secondline-7th position*/
+
/* Display the Date on Secondline-7th position*/
 
         LCD_GoToXY(1,6);  
 
         LCD_GoToXY(1,6);  
 
         LCD_DisplayRtcDate(day,month,year);
 
         LCD_DisplayRtcDate(day,month,year);

Revision as of 12:36, 4 February 2015

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

[1]

Schematic AVR Interfacing RTC.JPG

Code

 #include <avr\io.h>    // io.h contains the defnition of all ports and SFRs
#include "lcd.h"	   //User defined LCD library which conatins the lcd routines
#include "ds1307.h"    //User defined library which conatins the RTC(ds1307) routines
 
/* 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(0x14,0x11,0x12);  //  14th Nov 2012
 
  /* Display "Time" on first line*/
   LCD_DisplayString("Time: ");
 
   /* Display "Date" on Second line*/
   LCD_GoToLineTwo();
   LCD_DisplayString("Date: ");
 
   /* Display the Time and Date continuously */ 
   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);
    }		
 
  }