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. /*-----------------------------------------------------------------------------
  2. Program to demonstrate displaying of RTC on LCD
  3. -----------------------------------------------------------------------------
  4. note:
  5. Refer lcd.c(lcd_4_bit.c/lcd_8_bit.c) file for Pin connections
  6. Refer Atmega32 DataSheet for register descriptions.
  7. ------------------------------------------------------------------------------*/
  8.  
  9.  
  10. /* io.h contains the defnition of all ports and SFRs */
  11. #include <avr\io.h>
  12.  
  13. #include "lcd.h" //User defined LCD library which conatins the lcd routines
  14. #include "ds1307.h" //User defined library which conatins the RTC(ds1307) routines
  15.  
  16.  
  17. /* start the main program */
  18. void main()
  19. {
  20. unsigned char sec,min,hour,day,month,year;
  21.  
  22. /* Initilize the lcd before displaying any thing on the lcd */
  23. LCD_Init();
  24.  
  25. /* Initilize the RTC(ds1307) before reading or writing time/date */
  26. DS1307_Init();
  27.  
  28.  
  29. /*Set the time and Date only once */
  30. DS1307_SetTime(0x10,0x40,0x20); // 10:40:20 am
  31. DS1307_SetDate(0x14,0x11,0x12); // 14th Nov 2012
  32.  
  33. /* Display "Time" on first line*/
  34. LCD_DisplayString("Time: ");
  35.  
  36. /* Display "Date" on Second line*/
  37. LCD_GoToLineTwo();
  38. LCD_DisplayString("Date: ");
  39.  
  40. /* Display the Time and Date continuously */
  41. while(1)
  42. {
  43. /* Read the Time from RTC(ds1307) */
  44. DS1307_GetTime(&hour,&min,&sec);
  45.  
  46. /* Display the time on Firstline-7th position*/
  47. LCD_GoToXY(0,6);
  48. LCD_DisplayRtcTime(hour,min,sec);
  49.  
  50.  
  51. /* Read the Date from RTC(ds1307) */
  52. DS1307_GetDate(&day,&month,&year);
  53.  
  54. /* Display the Date on Secondline-7th position*/
  55. LCD_GoToXY(1,6);
  56. LCD_DisplayRtcDate(day,month,year);
  57. }
  58.  
  59. }