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

  1. #include <avr\io.h> // io.h contains the defnition of all ports and SFRs
  2. #include "lcd.h" //User defined LCD library which conatins the lcd routines
  3. #include "ds1307.h" //User defined library which conatins the RTC(ds1307) routines
  4.  
  5. /* start the main program */
  6. void main()
  7. {
  8. unsigned char sec,min,hour,day,month,year;
  9.  
  10. /* Initilize the lcd before displaying any thing on the lcd */
  11. LCD_Init();
  12.  
  13. /* Initilize the RTC(ds1307) before reading or writing time/date */
  14. DS1307_Init();
  15.  
  16. /*Set the time and Date only once */
  17. DS1307_SetTime(0x10,0x40,0x20); // 10:40:20 am
  18. DS1307_SetDate(0x04,0x02,0x15); // 4th Feb 2015
  19.  
  20. /* Display "Time" on first line*/
  21. LCD_DisplayString("Time: ");
  22.  
  23. /* Display "Date" on Second line*/
  24. LCD_GoToLineTwo();
  25. LCD_DisplayString("Date: ");
  26.  
  27. /* Display the Time and Date continuously */
  28. while(1)
  29. {
  30. /* Read the Time from RTC(ds1307) */
  31. DS1307_GetTime(&hour,&min,&sec);
  32.  
  33. /* Display the time on Firstline-7th position*/
  34. LCD_GoToXY(0,6);
  35. LCD_DisplayRtcTime(hour,min,sec);
  36.  
  37.  
  38. /* Read the Date from RTC(ds1307) */
  39. DS1307_GetDate(&day,&month,&year);
  40.  
  41. /* Display the Date on Secondline-7th position*/
  42. LCD_GoToXY(1,6);
  43. LCD_DisplayRtcDate(day,month,year);
  44. }
  45.  
  46. }