Program For Digital Clock Using 8051

In this tutorial, we will see how to interface DS1307(RTC) with 8051.
First, we will see the internals of DS1307 and later how to read and write the date and time.

Program For Digital Clock Using 8051 Microcontroller

  • 1DS1307 Basics

The Real-time clock DS1307 IC basically is stand-alone time clock with following features.

  • Real-time clock (RTC) counts seconds, minutes, hours, date of the month, month, the day of the week, and year with leap-year compensation valid up to 2100.
  • The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.
  • 56-byte, battery-backed, non-volatile (NV) RAM for data storage
  • Two-wire(I2C) serial interface
  • Programmable squarewave output signal
  • Automatic power-fail detect and switch circuitry
  • Consumes less than 500nA in battery backup mode with oscillator running
  • Optional industrial temperature range: -40°C to +85°C

Project report on the Digital clock using RTC and microcontroller 8051 1. 1 A Project report on Digital Clock with time and Alarm functions Prepared By: Maulik Sanchela 2. 2 Abstract Digital clock is displays the time using RTC. This circuit is used many applications like cars, railway stations, houses, offices, etc. The Real-time clock DS1307 IC basically is stand-alone time clock with following features. Real-time clock (RTC) counts seconds, minutes, hours, date of the month, month, the day of the week, and year with leap-year compensation valid up to 2100. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.

  • Digital clock using DS12C887 and 8051 microcontroller (AT89C51) in 12 hour mode. This article is an improved version of LCD based clock using RTC DS12C887 and 8051 microcontroller (AT89C51) using update interrupt. Has two modes of operation i.e., 12 hour and 24 hour mode. In our earlier articles we explained how to use 24 hour mode.
  • For this clock, we can set the time at any instant. Here, the clock can work in either 24 hour mode or 12 hour mode and the RTC chip is configured by programming 8051 controller. I will demonstrate two circuits of Digital Clocks using 8051 Microcontroller: one uses the RTC DS12C887 and the other uses the RTC DS1307.
  • 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.

DS1307 Pins

Below image shows the pin diagram and the recommended connections for DS1307. VCC, GND: These pins are used to provide the power to the chip. When 5V is applied within normal limits, the device is fully accessible and data can be written and read. When a 3V battery is connected to the device and VCC is below 1.25 x VBAT, reads and writes are inhibited. However, the timekeeping function continues unaffected by the lower input voltage. As VCC falls below VBAT the RAM and timekeeper are switched over to the external power supply (nominal 3.0V DC) at VBAT.

X1-X2:Pins to connect the external 32.768kHz oscillator that provides the clock source to the chip.

Vbat: A 3.3v Lithium battery can be connected to this pin in order to provide the power source when the external supply voltage is not available. Battery voltage must be held between 2.0V and 3.5V for proper operation.

For

SCL: This pin must be connected to SCL pin of the I2C Bus/Master.

SDA: This pin must be connected to SDA pin of the I2C Bus/Master.

SQW/OUT: When enabled, the SQWE bit set to 1, the SQW/OUT pinoutputs one of four square wave frequencies (1Hz, 4kHz, 8kHz, 32kHz).

  • Note: The SCL,SDA, and SQW are open drain and must be pulled up with appropriate pull-up resistors as shown in the image.

DS1307 Memory

The RTC keeps the date and time arranged in it's memory as shown below:

Control Register
76543210
OUT 0 0 SQWE0 0RS1RS0

OUT: This bit controls the output level of the SQW/OUT pin when the square-wave output is disabled. If SQWE = 0, the logic level on the SQW/OUT pin is 1 if OUT = 1 and is 0 if OUT = 0. By default this pin will be 0.

SQWE:This bit, when set to logic 1, enables the oscillator output. The frequency of the square-wave output depends on the value of the RS0 and RS1 bits.

RS1-RS0:These bits control the frequency of the square-wave output when the squarewave output has been enabled.

8051 Program Counter

Ds1307 ID
76543210
1101000R/W
  • R/W is 0 then data is Written to RTC
  • R/W is 1 then data is Read from RTC

This is defined in the code as:

Program For Digital Clock Using 8051
  1. Start the I2C communication.
  2. Send the DS1307 address and select write operation
  3. Send the Address of Control Register for sending the command.
  4. Send the Command to Disable the SQW Out.
  5. Stop the Communication.


  1. Start the I2C communication.
  2. Send the DS1307 address and select write operation
  3. Send the Address of SECOND Register for writing the second value.
  4. Write the Sec,min,hour,weekDay,date,month,year one by one.
  5. Stop the Communication.


  1. Start the I2C communication.
  2. Send the DS1307 address and select write operation
  3. Send the Address of SECOND Register for reading the second value.
  4. Stop the Communication.
  5. Send the DS1307 address and select Read operation
  6. Read the Sec,min,hour,weekDay,date and month one by one and send positive acknowledgement.
  7. Read the Year and send the Neg/No Acknowledgement.
  8. Stop the Communication.

Both the above functions use a simple structure shown below for easy access

Now, let's put together all that we have discussed in a simple example to read and show the time on character LCD.

Note: The date and time read from Ds1307 will be of BCD format, like:

  • 0x12,0x39,0x26 for 12hr,39min and 26sec.
  • 0x15,0x08,0x47 for 15th day,8th month and 47th year


Download the sample code and design files from this link.

Have an opinion, suggestion , question or feedback about the article let it out here!

Please enable JavaScript to view the comments powered by Disqus.

You might be seeing a wall clock running with a 1.5V battery or a digital watch running with lithium or silver oxide. But, an interesting thing is, if you want to tick a clock in real time applications, the only solution is to use RTC (Real Time Clock) to get Date and Time.

In the previous tutorial, I have explained how to use RTC DS3231 with microcontroller and display date and time on LCD.

In this article, I will show how to display date and time on PC using 8051 serial communication.

RTC Schematic of DS3231

RTC code for DS3231

This embedded C program evaluates the working of DS3231 RTC with 8051 Microcontroller and displays time and date on PC.

The below code snippet shows the macros used for RTC registers. The addresses correspond to the slave address of DS3231, second, minute, hour, second, date, month, and year, hour format.

The below code shows the macros used for i2c communication with 8051.

Program For Digital Clock Using 8051 Instruction

The below code explains the serial communication with 8051 and RTC.

The header file has been created for accessing the global variables.

The delay function has been used to set some lag for sending byte by byte to PC.

Proteus Simulation

Conclusion

Assembly Language Code For Digital Clock Using 8051

I hope you understand how to use Real Time Clock (RTC) to display the date and time on the serial terminal.

There are multiple ways to monitor the data. It might be an LCD, PC or a serial debugger.

8051 Programming Board

This DS3231 RTC code is tested on 8051 microcontroller. The date and time will be shown on the serial terminal at 9600 baud rate. The software can be putty, real term, dock light, and hyper terminal, etc.