Program For Digital Clock Using 8051



Digital Clock using 8051 Microcontroller In this project, we are going to demonstrate making a RTC Clock using 8051 microcontroller. If you would like to do this project with Arduino, check this digital clock using Arduino. The major component of this project is DS1307 which is a real time digital clock IC.

Aug 17, 2018 - In today's post, I am going to share Interrupt based Digital clock with 8051 Microcontroller. In the previous post, I have explained in detail How. Even larger displays are also available; some digital clocks sport digits that are two to five inches tall. Interfacing with 8051 The circuit diagram shown above is of an AT89S51 microcontroller based 0 to 9 counter which has a 7 segment LED display interfaced to it in order to display the count.

Here is a simple project on how to make a digital clock with 8051(89c51,89c52) microcontroller. The clock is efficient and their is no difference in time even in milli seconds. You can verify it with the digital clock you have. In the project i utilized the 8051 microcontroller internal clock source to produce a delay exactly equal to 1 second. This delay is then used to build a efficient clock with 89c51 microcontroller. Usually digital RTC(real time clocks) are interfaced with microcontrollers to make a digital clock. But i utilized the internal timers of 89c51 microcontroller to generate a precise time for making a clock with 8051 microcontroller. 16×2 lcd is interfaced with 8051 microcontroller to display time on lcd. Time in minutes, hours and seconds will be displayed on 16×2 lcd. 4×4 numeric keypad is also part of the 8051 digital clock project. It is used to set time, user manually enters the minutes and hours by pressing the buttons of 4×4 numeric keypad. Buttons represents numbers on keypad.

8051 microcontroller clock – Project requirements

  • 16×2 lcd (To display time)
  • 8051 (89c51 or any of 8051 series microcontroller)
  • Potentiometer/variable resistor (For setting LCD contrast)
  • Crystal(11.0592 MHz) (For clock generation)
  • 4×4 keypad (For setting time)
  • 5 volts power supply for driving lcd and microcontroller.

89c51 microcontroller digital clock – Project circuit diagram

Connect 4×4 keypad with Port-1 of 8051(89c51) microcontroller. Connect 16×2 lcd with Port-2 of microcontroller. The rs (register set) pin of 16×2 lcd is connected to Port-3 pin#5 and en(enable) pin is connected to Port-3 Pin#6 of microcontroller. Ground rw(Read-Write) Pin of 16×2 lcd. Attach crystal of frequency 11.0592 MHz to Pin# 18 and 19 of 8051 microcontroller. I used Crystal of frequency 11.0592 MHz. For reset connect a push button to Pin#9 of microcontroller. To set contrast attach a potentiometer/variable resistor to contrast pin of lcd. ​​
digital clock with 8051(89c51,89c52) microcontroller
The code of the project is very complex. If you are newbie it will be very hard to grab all the information in the code. I can not explain each and every statement of code here. I made separate tutorials on how to interface 16×2 lcd and 4×4 numeric keypad with 89c51 microcontroller? If you don’t know about keypad and lcd interfacing with microcontrollers i recommend to take all the tutorials. You have to take each tutorial to fully understand the code. Because each component programming is different with other. Lcd is interfaced in 8-bit mode with 8051 microcontroller. To understand the working of 16×2 lcd take the tutorials whose links are below. Key-pad coding is very easy take the tutorial.

Note: These tutorials are very important because the coding in the project heavily depends on these tutorials.

​Time delay generation is also very difficult using 8051(89c51,89c52) microcontroller. You have to be familiar with the internal timer registers of the 8051. For delay generation take the following tutorials.

  1. How to give one minute delay using 8051.
Program For Digital Clock Using 8051
int scan_key(); Used to set time initially at the beginning when power is supplied.
int scan_key1(); Used to set time if 1 is pressed.
The difference between the above two functions is one is used to set time initially when you power on the hardware. The second one is used to set time when clock is operational. For example clock is running and you want to set time again so pressing ‘1‘ on keypad will lead you into scan_key1() function and you can set time according to your desire.

void display(); Used to check increments in seconds, hours and minutes and display time.
void start(); Used to check increments in seconds , hours and minutes and display time.

void tochar(unsigned int value); Used to display time by converting integer in character.
To display a numeric character on 16×2 lcd we have to convert it in character form. The above function is used to achieve this task.

void onemintdelay(); Used to start the internal timer till one second.
One minute delay is generated with the above function. For knowing how to generate one second delay with 89c51 microcontroller? You have to take the tutorial mentioned above.

8051 Microcontroller Projects Using Assembly

digital clock 8051 microcontroller minutes settings
When you are done with all the above circuit building and code compilation. Then burn the code in your 8051 microcontroller with a good burner and now its time to test the code. At the start of the circuit triggering/powering you will see a message “SET 1 CLOCK” displayed on the first line of the 16×2 lcd and on second line you will see time in hours, minutes and seconds. Hour increments after every 60 minutes, minute is incremented after 60 seconds. When hour, minute and seconds reaches 12, 60, 60 they are again initialized with 0 for next reading.

Note: You can also set the time at any instance just press 1 and you can see settings , set the time, and your clock starts running from updated time.

Important note:The seconds starts from 3 and goes to 60. This is not an error in coding simply just because after each second i have to check some readings like hour minutes etc in which three seconds are required from each minute. Time displayed on the 16×2 lcd is perfect their is no error in it. Valhallaroom mac valhalla dsp valhallaroom for mac.

Download the project files, code(C++,Hex) compiled in keil Uvision 4 compiler. Just download the hex Code in your microcontroller. MaKe the circuit and enjoy your digital clock. And one thing more don’t forget to write comments about the post and project below. Project video is also given below.
Digital alarm with 8051(89c51,89c52) microcontroller is a comprehensive project. Comprehensive in the form that its code is very lengthy. Programming is not an easy stuff for every one , and when it comes to program a microcontroller it becomes more complex to write a code for desired output if you are not familiar with the basics of programming. Digital alarm with 8051(89c51,89c52) microcontroller is an extension of digital clock with 8051(89c51,89c52) microcontroller project. The alarm works in the same way as your digital watches and mobile phone alarms works. Well all alarms works same 😀

8051 microcontroller digital alarm – Project requirements

  • 8051(89c51 or 89c52) microcontroller
  • alarm(I used buzzer)
  • 16×2 lcd
  • Potentiometer/variable resistor (For setting lcd contrast)
  • 4×3 keypad(You can also use 4×4. 4×4 contains 4×3 in it :D)
  • Crystal(11.0592 MHz)
  • Bread board(Designing circuit. You can also design circuit on PCB(Printed circuit board))

Tutorials regarding 8051 digital alarm clock project

Its not possible to explain each and every statement of code. For example 16×2 lcd interfacing with 8051 microcontroller or 4×4 numeric keypad interface with 8051 microcontroller or how to generate proper time delay using internal timers of 89c51 microcontroller can not be explained in a single tutorial. So i made multiple tutorials on each component which is part of this project.
  • ​How to generate one second delay with 8051 microcontroller.
  • Digital clock with 8051 microcontroller.


All the above tutorials play a vital role in 8051 digital alarm clock project. I will say the code of all the above tutorials is part of this project. I just arranged the code in sequence with some minor modifications and made a stream line code flow to achieve digital alarm clock functionality.

when you first run the program you have to set the clock time same like the digital clock project(Link given above). Once you set the clock time it will start running for ever unless you reset the microcontroller. Now when ever you want to set an alarm press the button located in first coulomb and at fourth row of 4×3 keypad. Pressing the button will lead to an event which displays messages on 16×2 lcd to set hours and minutes. Now set alarm time.

Note: No external RTC(real time clock) is interfaced with 8051 microcontroller for time calculation. Rather internal timers of 89c51 mirocontroller are utilized for producing clock time(Visit above digital clock with 89c51 tutorial).

89c51 microcontroller digital alarm – Circuit diagram

The circuit for the project is very simple. Connect 16×2 lcd with Port 1 of 8051 (89c51 or 89c52) microcontroller. Rd(read) Wr(write) and En(enable) signals are provided to 16×2 lcd using port 3 pins 5 , 6 & 7 of 8051 (89c51 or 89c52) microcontroller. Port 0 pin 0 is used to operate buzzer when ever alarm time reached to its matched clock time. Port 2 is dedicated for our 4×3 keypad. Rows are connected to pin 0, 1 ,2 & 3 of port 2. Coulombs are connected to pins 5 , 6 & 7 of port 2. Rest of the connections are simple apply 5v to pin 40 and pin 31. Connect crystal 11.0592 MHz to pins 18 and 19. Ground pin 20. Connect reset button to pin 9.
Program For Digital Clock Using 8051

89c51 microcontroller digital alarm clock – Project code

CodeComing to the code. First i included the necessary header file

8051 Program Counter

reg52.h. This header file should be included when ever you are writing and compiling your 8051 microcontroller program in keil ide. It includes all the necessary linker and debuggers for 8051 microcontroller configuration.

Then keypad() function is declared. This function scans the keys pressed on the 4×3 keypad. Next comes tochar() function. It is used to deal with integer type numbers to be displayed on the 16×2 lcd. Integer numbers must be converted to 8-bit character format for displaying on 16×2 lcd.

Then single bits of ports are declared for connecting our buzzer and 4×3 keypad. Next character arrays are declared they are used as messages to be displayed on 16×2 lcd on suitable conditions. delay() function is used to generate some delay for suitable purposes. lcdcmd() function is sending commands to lcd. lcddata() function is sending and printing data on lcd screen. checkconditions() function is checking conditions like if seconds reached to 60 increment the minute, if minutes reached to 60 increment hour, check if clock reached the alarm time ring the buzzer.

Program for digital clock using 8051 programming

Assembly Language Code For Digital Clock Using 8051

clockdelay() function is producing seconds, minutes and hours for us. lcdinit() initializing the lcd. settime() function is setting time of clock on every reset of the program. User have to input the time using 4×3 keypad. start() function is printing the time on 16×2 lcd at start of the program execution.

The above tutorials are very important if you want to understand the functions used in the code. If you don’t care about code than just burn the hex code make the circuit and enjoy your creation 😀

The alarm is 12 hours alarm which means you can put-on alarm for maximum next 12 hours period. Do not enter hours greater than 12 and do not enter minutes greater than 60. If you want to make the alarm more intelligent just insert more functions in it like set alarm for am or pm time. Do some other work when ever clock time reaches the alarm time like switch on a bulb, start your water motor to fill the tank or switch off the oven. If you are really willing to do some extra stuff than go a head and give me your feedback.
Download the project code and hex file compiled in keil Uvision-2. If you have any questions regarding code or any thing just leave a comment below.Your comments are highly apreciated.