Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • EG Blogs
    • Insight
    • Invention Stories
    • How to
    • What Is
    • News
      • EE Design News
      • DIY Reviews
      • Guest Post
      • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Real Time Clock with Alarm Option using AT89S52 and DS1307 IC

January 1, 2021 By Salman Khan

 

Real time clock is a digital clock which display real time on 16×2 LCD display. Here in this circuit we can also set alarm and time. The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I²C, bidirectional bus. The ck/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

Circuit Diagram of 8051 Microcontroller and DS1307 IC based Real Time Clock

Fig. 1: Circuit Diagram of 8051 Microcontroller and DS1307 IC based Real Time Clock

Fig 1.1: Proteus circuit of Real time digital clock

Description:-

In this circuit Port 2 of microcontroller connected to the data pins of 16×2 LCD display and bit P3^0 and P3^1 of port 3 connected to the command pin of 16×2 LCD display rs and en respectively. And serial data SDA and clock SCL pin of ds1307 connected to P1^0 and P1^1 of microcontroller respectively. 4 switches/ push buttons also used to set time and alarm. Bits P3^2, P3^3,P3^4 and P1^2 are configured as increment digit, increment value, set time and set alarm respectively. And an alarm indicator LED also connected to P1^3 for indicating alarm. When alarm time will match with the real time of clock then led will be activate. In this clock the RTC is used in 24 hour mode which gives accurate time and can be displayed on LCD through microcontroller.The microcontroller continuously reads the data from the RTC. Program continuously read the data from ds1307 and show on display. Bit P3^4 is made active low when and low signal comes to program calls time set function and bits P3^2 and P3^3 is also made active low to set time/date/day as well as alarm time after pressing bit P1^2.

Overview of 8051 Microcontroller and DS1307 IC based Real Time Clock

Fig. 2: Overview of 8051 Microcontroller and DS1307 IC based Real Time Clock

Fig 1.3: Circuit Diagram of RTC 

Component Used

Component Used:-

89S52 microcontroller

A microcontroller is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals. Program memory in the form of NOR flash or OTP ROM is also often included on chip, as well as a typically small amount of RAM. Microcontrollers are designed for embedded applications, in contrast to the microprocessors used in personal computers or other general purpose applications.

Typical Image of AT89S52 8051 Microcontroller

Fig. 3: Typical Image of AT89S52 8051 Microcontroller

16×2 LCD

LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16×2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are preferred over seven segments and other multi segment LEDs. The reasons being: LCDs are economical; easily programmable; have no limitation of displaying special & even custom characters (unlike in seven segments), animations and so on.

A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5×7 pixel matrix. This LCD has two registers, namely, Command and Data.

The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling display etc.

The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD. Click to learn more about internal structure of a LCD.

Pin Diagram of 16X2 Character LCD

Fig. 4: Pin Diagram of 16X2 Character LCD

DS1307

The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I²C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

Typical Image of DS1307 RTC IC

Fig. 5: Typical Image of DS1307 RTC IC

 

Project Source Code

###

#include
sbit rs=P3^0;
sbit en=P3^1; 
sbit SDA=P1^0;              //data pin for ds1307 bi-directional bit
sbit SCL=P1^1;             //clock pin for ds1307 output bit
sbit alarm_set=P1^2;       //set alarm input bit
sbit alarm_beep=P1^3;      //alarm indicator output bit
sbit next=P3^2;            //increament digit
sbit inc=P3^3;            //increament value input bit
sbit set=P3^4;           //set time input bit
void set_time();        //time/date/day set function
void display();          //time/date/day display function 
void alarm();            //alarm set function
char ack;
unsigned char day1=1;
unsigned char k,x;
unsigned int date=1, mon=1, year=0x14, hour=0, min=0, sec=0; 
unsigned int alarm_min, alarm_hour; 
void I2CStart()
{
SDA=1;SCL=1,SDA=0,SCL=0;
}                             //"start" function for communicate with ds1307 RTC
void I2CStop()
{
SDA=0,SCL=1,SDA=1;
}                                //"stop" funci=tion for communicate wit ds1307 RTC
unsigned char I2CSend(unsigned char Data)      //send data to ds1307 
{
char i;
static bit ack_bit;
for(i=0;i<8;i++)
{
if(Data & 0x80) SDA=1;
else SDA=0;
SCL=1;
Data<<=1;
SCL=0;
}
SDA=1,SCL=1;
ack_bit=SDA;
SCL=0;
return ack_bit;
}
unsigned char I2CRead(char ack)           //receive data from ds1307
{
unsigned char i, Data=0;
SDA=1;
for(i=0;i<8;i++)
{
Data<<=1;
do{SCL=1;}
while(SCL==0);
if(SDA) Data|=1;
SCL=0;
}
if(ack)SDA=0;
else SDA=1;
SCL=1;
SCL=0;
SDA=1;
return Data;
}
void delay(unsigned int time)                // delay function
{
unsigned int i,j;
for(i=0;i               // send command to LCD
{
P2=cmd;
rs=0,en=1;
delay(2),en=0;
}
void lcddata(unsigned char item)                //send data to LCD
{
P2=item;
rs=1,en=1;
delay(2),en=0;
} 
void lcd_init()                         //LCD initialize
{
  lcdcmd(0x38);
  lcdcmd(0x06);
  lcdcmd(0x0c);
} 
void lcdnum(unsigned char convert)          //convert data in to ascii
{
 lcddata(convert/16+48);
 lcddata(convert%16+48);
}
void print(char *str)                     // print data/values on LCD
{
 while(*str!='')
 {
  lcddata(*str);
  str++;
 }
}
void day(char d)                         // Function for display day on LCD
{
switch(d)
{
  case 0:
  print("DAY");
  break;
  case 1:
  print("SUN");
  break;
  case 2:
  print("MON");
  break;
  case 3:
  print("TUE");
  break;
  case 4:
  print("WED");
  break;
  case 5:
  print("THU");
  break;
  case 6:
  print("FRI");
  break;
  case 7:
  print("SAT");
  break;
 }
 }
void main() // main function
{
 lcd_init();
 lcdcmd(0x80);
 print("RTC clock by ");
 lcdcmd(0xc0);
 print("Saddam Khan  ");
 delay(300);
 lcdcmd(0x01);
 while(1)
 {
 if(set==0)  // check time set button press
 {
 I2CStart();
 I2CSend(0xD0);
 I2CSend(0x00);
 I2CSend(0x00);
 I2CSend(0x00);
 I2CSend(0x00);
 I2CSend(0x01);
 I2CSend(0x01);
 I2CSend(0x01);
 I2CSend(0x00); 
 I2CSend(0x80); 
 I2CStop();
 k=1;
 set_time();   // call time set function
 I2CStart();
 I2CSend(0xD0);
 I2CSend(0x00);
 I2CSend(0x00);
 I2CSend(min);
 I2CSend(hour);
 I2CSend(day1);
 I2CSend(date);
 I2CSend(mon);
 I2CSend(year); 
 I2CSend(0x80); 
 I2CStop(); 
 }
 if(alarm_set==0)    //check alarm set button press
 {    
  alarm();    //call alarm set function
  while(alarm_set==0);
  k=5;
 }
  I2CStart();
  I2CSend(0xD0);
  I2CSend(0x00);
  I2CStart();
  I2CSend(0xD1);
  sec=I2CRead(1);
  min=I2CRead(1);
  hour=I2CRead(1);
  day1=I2CRead(1);
  date=I2CRead(1);
  mon=I2CRead(1);
  year=I2CRead(1);
  I2CStop();
  lcdcmd(0x0c);
  if(hour==alarm_hour && min==alarm_min)    // check alarm time with real time
  { 
   k=7;
   if(sec>88){k=0;}    //k=0; remove alarm
   alarm_beep=1;    //alarm output set
  } 
 else
  {
   alarm_beep=0;    //alarm output reset
  }  
   display();    //display time/date/day 
   delay(1);
 }
 }
void set_time() //time set function
{
 lcdcmd(0x0e);
 while(k<7)
 {
  while(k==3) //set date
  {
   x=year%4;
   if(inc==0)
   {
    date++;while(inc==0);
    if(date==10){date=date+6;} //covert decimal value into BCD
    if(date==26){date=date+6;}
    if(date==42){date=date+6;}
    if(x==1 && mon==2 && date==41){date=1;} //check for 28 day febuary
    if(x==0 && mon==2 && date==48){date=1;} //check for 29 day febuary
    if((date==49) && (mon==4) || (mon==6) || (mon==9) || (mon==17)){date=1;
    } // check for 30 day month
   if(date==50){date=1;} // check for 31 day month
   display();}
   if(next==0){k=4;while(next==0);} //check for next digit
   lcdcmd(0x81);
  } 
  while(k==2) //set month
  {
   if(inc==0)
   {
    mon++;while(inc==0);
    if(mon==10){mon=mon+6;}
    if(mon==19){mon=1;}   //check for end of year
    display();
    }
    if(next==0){k=3;while(next==0);}
    lcdcmd(0x84);
   }
   while(k==1)  //set year
  {
   if(inc==0)
   {
   year++;while(inc==0);
   if(year==10){year=year+6;}
   if(year==26){year=year+6;}
   if(year==41){year=0;} 
   display(); 
   }
   if(next==0){k=2;while(next==0);}
   lcdcmd(0x89);
  }
  while(k==4)    //set day
  {
   if(inc==0)
   {
   day1++;while(inc==0);
   if(day1==8){day1=1;}
   display(); 
   }
   if(next==0){
   k=5;while(next==0);
   }
   lcdcmd(0x8f);
  }
  while(k==5)   //set hour
  {
   if(inc==0)
   {
   hour++;while(inc==0);
   if(hour==10){
   hour=hour+6;
   }
   if(hour==26){
   hour=hour+6;
   }
   if(hour==36)
   {
   hour=0;
   }
   display();
   }
   if(next==0){
   k=6;while(next==0);
   }
   lcdcmd(0xc1);
  }
  while(k==6)    //set min
  {
   if(inc==0)
   {
   min++;while(inc==0);
   if(min==10)
   {
   min=min+6;
   }
   if(min==26)
  {
 min=min+6;
 }
   if(min==42){min=min+6;}
   if(min==58){min=min+6;}
   if(min==74){min=min+6;}
   if(min==90){min=min+6;}
   if(min==96){min=0;}
   display();}
   if(next==0){k=10;while(next==0);}
   lcdcmd(0xc4);
  }
 }
} 
void display() //function to display time/date/day on LCD
{
  lcdcmd(0x80);
  lcdnum(date);
  lcdcmd(0x82);
  print("-");
  lcdcmd(0x83);
  lcdnum(mon);
  lcdcmd(0x85);
  print("-20");
  lcdcmd(0x88);
  lcdnum(year);
  lcdcmd(0x8a);
  print("  ");
  lcdcmd(0x8c);
  day(day1);
  lcdcmd(0xc0);
  lcdnum(hour);
  lcdcmd(0xc2);
  print(":");
  lcdcmd(0xc3);
  lcdnum(min);
  lcdcmd(0xc5);
  print(":");
  lcdcmd(0xc6);
  lcdnum(sec);
  if(k==7)
  { 
   if(sec%2==0)
    {
    lcdcmd(0xca);
    print("ALARM");
    }
else
   {
print("       ");
   }
  }
  else if(k==5)
  {
    lcdcmd(0xca);
    print("ALARM");
  }
  else
  {
  lcdcmd(0xc8);
  print("         "); 
  }
  }
 void alarm() // set alarm
 {
  unsigned int k=1;
  alarm_hour=0, alarm_min=0;
  hour=alarm_hour;
  min=alarm_min;
  lcdcmd(0x0e);
  lcdcmd(0x01);
  print("Set alarm      ");
  delay(200);
  display();
  while(k<3) //set hour for alarm
  {
   while(k==1)
   {
    if(inc==0)
    {alarm_hour++;while(inc==0);
    if(alarm_hour==10){alarm_hour=alarm_hour+6;}
    if(alarm_hour==26){alarm_hour=alarm_hour+6;}
    if(alarm_hour==36){alarm_hour=0;}
hour=alarm_hour;
    display();}
    if(next==0){k=2;while(next==0);}
    lcdcmd(0xc1);
   }
     //set min for alarm
   while(k==2)
   {
    if(inc==0)
    {alarm_min++;while(inc==0);
    if(alarm_min==10){alarm_min=alarm_min+6;}
    if(alarm_min==26){alarm_min=alarm_min+6;}
    if(alarm_min==42){alarm_min=alarm_min+6;}
    if(alarm_min==58){alarm_min=alarm_min+6;}
    if(alarm_min==74){alarm_min=alarm_min+6;}
    if(alarm_min==90){alarm_min=alarm_min+6;}
    if(alarm_min==96){alarm_min=0;}
    min=alarm_min;
    display();}
    if(next==0){k=3;while(next==0);}
    lcdcmd(0xc4);
  }
 }
 }






//without time and alarm set

#include
sbit rs=P3^0;
sbit en=P3^1; 
sbit SDA=P1^0;
sbit SCL=P1^1;
char ack;
char array[7];
void I2CStart(){SDA=1;SCL=1,SDA=0,SCL=0;}
void I2CStop(){SDA=0,SCL=1,SDA=1;}
unsigned char I2CSend(unsigned char Data)
{
char i;
static bit ack_bit;
for(i=0;i<8;i++)
{
if(Data & 0x80) SDA=1;
else SDA=0;
SCL=1;
Data<<=1;
SCL=0;
}
SDA=1,SCL=1;
ack_bit=SDA;
SCL=0;
return ack_bit;
}
unsigned char I2CRead(char ack)
{
unsigned char i, Data=0;
SDA=1;
for(i=0;i<8;i++)
{
 Data<<=1;
 do{SCL=1;}
 while(SCL==0);
 if(SDA) Data|=1;
 SCL=0;
}
if(ack)SDA=0;
else SDA=1;
SCL=1;
SCL=0;
SDA=1;
return Data;
}
void delay(int time)
{unsigned int i,j;
for(i=0;i 
 I2CSend(0x80);
 I2CStop(); 
 while(1)
 {
  I2CStart();
  I2CSend(0xD0);
  I2CSend(0x00);
  I2CStart();
  I2CSend(0xD1);
  for(s=0;s<7;s++)
  {array[s]=I2CRead(1);}
  I2CStop();
  LCD_command(0x80);
  LCD_sendnum(array[4]);
  print("-");
  LCD_sendnum(array[5]);
  print("-20");
  LCD_sendnum(array[6]);
  LCD_command(0x8c);
  day(array[3]);
  LCD_command(0xc0);
  LCD_sendnum(array[2]);
  print(":");
  LCD_sendnum(array[1]);
  print(":");
  LCD_sendnum(array[0]);
  LCD_command(0xc9);  
 }
}
###

 


Circuit Diagrams

Circuit-Diagram-8051-Microcontroller-DS1307-IC-Based-Real-Time-Clock
Circuit-Diagram-8051-Microcontroller-DS1307-IC-Based-Real-Time-Clock

Project Video

Related Articles Read More >

Wireless distance measurement using ultrasonic sensor
Computerized wireless Pick and Place Robot
Designing an Arduino-based ECG monitor using an AD8232 ECG sensor
Controlling Appliances Wirelessly using RF Technology

Featured Tutorials

  • Screenshot of Raspbian OS on Raspberry Pi RPi Python Programming 03: Raspberry Pi as Linux System
  • Raspberry Pi Models RPI Python Programming 02: Raspberry Pi Models
  • Raspberry Pi 4 RPi Python Programming 01: Introduction to Raspberry Pi 4
  • RPi Python Programming 05: Introduction to Python
  • RPi Python programming 04 RPi Python programming 04: Setting up Raspberry Pi Linux computer
  • Python Basics RPi Python Programming 06: Python basics

Stay Up To Date

Newsletter Signup

EE Training Center Classrooms

“ee

“ee

“ee

“ee

Recent Articles

  • Arduino’s L293D motor driver shield guide
  • NXP launches its first Wi-Fi 6E Tri-Band system-on-chip
  • Nexperia launches industry’s first 80 V RETs for high-voltage bus circuits
  • TDK releases low-profile medical sensors
  • Getting started with Raspberry Pi
...

RSS EDABOARD.com Discussions

  • Power controll on 230V with zero switching and PWM?
  • hysteresis variation mc sim
  • Creepage distance from primary to secondary of offline SMPS
  • HSPICE Simulation refuses to match the Spectre Simulation
  • Same geometry but slightly different results

RSS Electro-Tech-Online.com Discussions

  • How do I find the value of an exploded ceramic capacitor?
  • Any advice on identifying the HV wires on an old flyback.
  • How are you managing with the Covid-19 pandemic?
  • zener diode problem
  • Where has the fun gone?
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2021 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • EG Blogs
    • Insight
    • Invention Stories
    • How to
    • What Is
    • News
      • EE Design News
      • DIY Reviews
      • Guest Post
      • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering