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 you are not familiar with it. For details of I2C in AVR, go through AVR Communication Protocols tutorial.

The first thing that the MCU sends to the slave (RTC) is the device ID. The device ID for DS1307, shown below. It also tells weather we want to write to or read from the RTC.

7 6 5 4 3 2 1 0
1 1 0 1 0 0 0 R/W
  • bit-0 is 0 than we Write to RTC
  • bit-0 is 1 we Read from RTC.

This is defined in the code as:

#define C_Ds1307ReadMode_U8   0xD1u  // DS1307 ID
#define C_Ds1307WriteMode_U8  0xD0u  // DS1307 ID

The RTC keeps the date and time arranged in it's memory as shown below:

ADDRESS FUNCTION RANGE
00h Seconds 00–59
01h Minutes 00–59
02h Hours 01-12/00-24
03h Day 01–07
04h Date 01–31
05h Month 01–12
06h Year 00–99
07h Control
08h to 3Fh RAM 00h–FFh

Write to the addresses above we can set the time, and once we set it, we can read it any time we need.


The address 0x07 is a control registered as described below:

7 6 5 4 3 2 1 0
OUT 0 0 SQWE 0 0 RS1 RS0

We write 0x00 to Control register to disable SQW-Out. We do not use any other bits from it, so you need not worry.

Now we can initialize the RTC with the code below

void RTC_Init(void)
{
    I2C_Init();                             // Initialize the I2c module.
    I2C_Start();                            // Start I2C communication
 
    I2C_Write(C_Ds1307WriteMode_U8);        // Connect to DS1307 by sending its ID on I2c Bus
    I2C_Write(C_Ds1307ControlRegAddress_U8);// Select the Ds1307 ControlRegister to configure Ds1307
 
    I2C_Write(0x00);                        // Write 0x00 to Control register to disable SQW-Out
 
    I2C_Stop();                             // Stop I2C communication after initializing DS1307
}

Schematic

[1]

Schematic AVR Interfacing RTC.JPG

Code

The code is very simple. As we are going to display date and time on LCD, we will first initialize LCD and DS1307. Then we need to set time and date only once. Now just read date and time from DS1307 and display it on LCD.

Check LCD Interfacing and DS1307 Working here to know more.

#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(0x04,0x02,0x15);  //  4th Feb 2015
 
  /* 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);
    }		
 
  }