Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • 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
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Digital clock using RTC DS12C887 and 8051 microcontroller (AT89C51) with time set

By Himanshu Choudhary

Real time clock (RTC) is widely used in many application to provide accurate time. This article explains the making of a simple digital clock using RTC DS12C887 and 8051 microcontroller (AT89C51). The output is displayed on an LCD. This clock also has a provision of setting time at any instant. The clock uses the concept of our earlier articles of interfacing RTC DS12C887 with microcontroller. The clock described here uses the method of polling for running. For further details on interfacing RTC DS12C887 with AT89C51 using polling, refer RTC interfacing.  The free source code for the program is available in C.

 
 

 


 

 

The circuit diagram shows the connection of RTC with the microcontroller. Port P2 is used as data port for LCD; port P0 of the microcontroller is used as data port of RTC DS12C887.  The pins P1^0, P1^1, P1^2, P1^3, P1^4, P1^5, P1^6 of controller AT89C51 are configured as reset, rs, rw, e, dig_hr1, dig_min1, start pins respectively.

 
In this clock the RTC is used in 24 hour mode which gives accurate time and can be displayed on LCD through microcontroller. Firstly oscillator is set, than RTC is set in 24 hour mode by setting 0X20 in register A. For more details, refer RTC interfacing with AT89C51.
 
The microcontroller continuously reads the data from the RTC. The algorithm processes the data and displays the data in correct order on the LCD screen. Interrupt 2 of the microcontroller is used to set time. Whenever P3^3 pin is made low (0), interrupt2 comes and set_time function is called for setting the time. Hours can be set by using the dig_hr1 pin. Minutes can be set with the dig_min1 pin. After setting the time, start pin is made high and the clock starts with the time set by user.  
 
 

 

Project Source Code

###

//Program for Digital clock using RTC DS12C887 and 8051 microcontroller (AT89C51) with time set
/*24 hr clock
set p3^3=0,then start=0 then set time by dig_hr1 & dig_min1, then remove p3^3 & start	*/
#include<reg51.h>
#include<absacc.h>
#define dataport P2
#define port P1
#define lcdport P3
 sbit reset = port^0;
 sbit rs =port^1;
 sbit rw =port^2;
 sbit e = port^3;
 sbit dig_hr1=port^4;
 sbit dig_min1=port^5;
 sbit start=port^6;
 int  min1=0,hr1=0;
 int min0=60,hr0=25;
 unsigned char temp=60,hr,min,sec,num[60]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0X09,0X10,0X11,0X12,0X13,0X14,0X15,0X16,0X17,0X18,0X19,0X20,0X21,0X22,0X23,0X24,0X25,0X26,0X27,0X28,0X29,0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39,0X40,0X41,0X42,0X43,0X44,0X45,0X46,0X47,0X48,0X49,0X50,0X51,0X52,0X53,0X54,0X55,0X56,0X57,0X58,0X59};

 void delay(unsigned int msec )
{
	int i ,j ;
	for(i=0;i<msec;i++)
	for(j=0; j<1275; j++);
}

void lcd_cmd(unsigned char item)
{
	dataport = item;
	rs= 0;
	rw=0;
	e=1;
	delay(1);
	e=0;
	return;
} 

// function to send data
void lcd_data(unsigned char item)
{
	dataport = item;
	rs= 1;
	rw=0;
	e=1;
	delay(1);
	e=0;
	return;
}

void lcd_data_string(unsigned char *str)
{
	int i=0;
	while(str[i]!='')
	{
		lcd_data(str[i]);
		i++;
		delay(1);
	}
	return; 
}

lcd_data_int(int time_val)
{
	int int_amt;
	int_amt=time_val/10;
	lcd_data(int_amt+48);
	int_amt=time_val%10;	 
	lcd_data(int_amt+48);
}


void lcd()
{
	lcd_cmd(0x38);		   
	delay(5);
	lcd_cmd(0x0C);        
	delay(5);
	lcd_cmd(0x80);
	delay(5);
}

void set_rtc_time()  
{
	XBYTE[10]=0x20;
	XBYTE[11]=0x82;
	XBYTE[0]=0x00;
	XBYTE[2]=min;
	XBYTE[4]=hr;
	XBYTE[7]=0x01;
	XBYTE[8]=0x01;
	XBYTE[9]=0x10;
	XBYTE[1]=0xFF;
	XBYTE[3]=0xFF;
	XBYTE[5]=0xFF;
	XBYTE[11]=0x22;
}

void set_hr1()
{
	hr1++;
	if(hr1>23)
	hr1=0;
	lcd_cmd(0xc3);
	lcd_data_int(hr1);
	lcd_data(':');
	hr0=hr1;
}

void set_min1()
{
	min1++;
	if(min1>59)
	min1=0;
	lcd_cmd(0xc6);
	lcd_data_int(min1);
	min0=min1;
}

void set_time()	interrupt 2
{
	lcd_cmd(0x01);
	if(start==0)
	{
		lcd_data_string("SET TIMING");
		lcd_cmd(0xc3);
		lcd_data_int(hr1);
		lcd_data(':');
		lcd_data_int(min1);
		while(start==0)
		{
			delay(10);
			if(dig_hr1==0)
			set_hr1();
			if(dig_min1==0)
			set_min1();	 
		}
	}
	lcd_cmd(0x01);
	hr=num[hr1];
	min=num[min1];
	set_rtc_time();
	lcd_cmd(0x80);
	lcd_data_string("TIME:");
	hr0=25;
	min0=60;
}

bcdconv(unsigned char mybyte)
{
	unsigned char x,y;
	x=	mybyte & 0x0F;
	x=x | 0x30;
	y= mybyte & 0xF0;
	y=y>>4;
	y=y | 0x30;
	lcd_data(y);
	lcd_data(x);
}

void read_rtc_display() 
{ 
	reset=0;
	reset=1;
	XBYTE[11]=0x02;
	hr=XBYTE[4];
	lcd_cmd(0x87);
	min=XBYTE[2];
	sec=XBYTE[0];
	if(hr!=hr0)
	{
		lcd_cmd(0X87);
		bcdconv(hr);
		lcd_data(':');
		hr0=hr;
	}
	if(min!=min0)
	{
		lcd_cmd(0X8A);
		bcdconv(min);
		lcd_data(':');
		min0=min;
	}
	if(sec!=temp)
	{
		lcd_cmd(0x8D);
		bcdconv(sec);
		temp=sec;
	}
}

void main()
{
	reset=1;
	lcd();
	XBYTE[10]=0x20;
	XBYTE[1]=0xFF;
	XBYTE[3]=0xFF;
	XBYTE[5]=0xFF;
	XBYTE[11]=0x02;
	lcd_cmd(0x01);
	IE=0x84;
	lcd_cmd(0x80);
	lcd_data_string("TIME:");
	while(1)
	{
		read_rtc_display();
	}
}

###

 


Circuit Diagrams

Circuit-Diagram-Digital-Clock-Using-RTC-DS12C887-8051-Microcontroller-AT89C51-Time-Set

Project Components

  • AT89C51 Microcontroller
  • LCD
  • RTC DS12C887

Project Video


Filed Under: 8051 Microcontroller
Tagged With: 8051, clock, ds12c887, microcontroller
 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • MicroPython – I2C protocol in ESP8266 and ESP3
  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • What's the deal with all these "MPPT" IC's with no current sense?
  • Photovoltaic MOSFET Drivers - Voltage Rating
  • Impedance requirement for SDRAM signals
  • A circuit that can adjust a resistance and probing a voltage node
  • A analogue circuit that spit out the resistance desired

RSS Electro-Tech-Online.com Discussions

  • IRS2453 the H circuit
  • Ampro 16mm Stylist projector woes.
  • How to quickly estimate lead acid battery capacity ?
  • Finally switched to Linux.
  • Multistage BJT amplifier
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 © 2022 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
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • 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
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering