Program For Digital Clock Using 8051

The figure shows, real time clock interfacing with 8051 microcontroller using I2C bus protocol. I2C is a bi-directional serial protocol, which consist of two wires such as SCL and SDA to transfer data between devices connected to bus. 8051 microcontroller has no inbuilt RTC device therefore we have connected externally through a serial. This is the circuit diagram of the digital clock using 8051 microcontroller. As we can see the microcontroller is connected to three 7 segment display with distinct ports not multiplexed and the last hour digit is only connected to a pin as it only shows 1. LED and buzzer are self explanatory according to the code. 1 of the LED is for AM and I have connected another LED not shown in the.

RTC is an electronic device which plays an essential role in realtime embedded system design. It provides a precise time and date in various applications such as system clock, student attendance system and alarm etc, that keep track on current time and provides consistent result to the respective task. This article presents RTC interfacing with 8051microcotrollerand basic accessing of internal registers.

RTC Programming and Interfacing

RTC interfacing with 8051 microcontroller is similar to all other kinds of “Real Time Clocks” interfaced to it. So let us look on simple RTC interfacing with 8051 microcontroller and programming procedure involving in it.


Program For Digital Clock Using 8051 Switch

Step1: Select RTC Device

The various kinds of RTC chips are available in the real time embedded world, which are classified based on various criteria such as package type, supply voltage and pin configuration etc. A few types of RTC devices are;

  • Two-Wire Serial Interface (I2C Bus)
  • Three-Wire Serial Interface (USB BUS)
  • Four-wire Serial interface (SPI BUS)

First, we need to select type of RTC device by category based on requirement like I2C Bus RTC or SPI Bus RTC or other, which suitsfor interfacingwith respective microcontroller. Then we can select features of RTC device depending on requirement of application such as battery life, suitable package and clock frequency. Let us consider two-wire interfacing RTC with 8051 microcontroller such as DS1307.

Step2: Internal Register and Address of the RTC Device

RTC stands for real time clock which provides years, months, weeks, days, hours, minutes and seconds based on crystal frequency. RTC consists of inbuilt RAM memory for data storage. A battery backup will be provided in case of failure of main power supply by connecting a battery to RTC device.

RTC DB1307 Configuration:

A0, A1, A2: are address pins of RTC DB1307 chip, which can be used to communicate with the master device. We can control eight devices with RTC interfacing with 8051 microcontroller by A0, A1, A2 bits using I2C protocol.


VCC and GND: VCC and GND are power supply and ground pins respectively. This device operated with 1.8V to 5.5V range.

VBT: VBT is a battery power supply pin. Battery power source must be held between 2V to 3.5V.

SCL: SCL is a serial clock pin and it is used to synchronize data on serial interface.

SDL: It is a serial input and output pin. It is used to transmit and receive the data on serial interface.

Clock Out: It is an optional square wave output pin.

OSC0 and OSC1: These are crystal oscillator pins which are used to provide the clock signals to the RTC device. The standard quartz crystal frequency is 22.768KHzs.

Device Addressing:

I2C bus protocol allows many slave devices at a time. Every slave device must consist of own address to represent on it. The master device communicates with particular slave device by an address. RTC device address is “0xA2” wherein “1010” is given by manufacturer and A0, A1, A2 are user define address, which is used to communicate eight RTC devices on the I2C bus protocol.

R/W bit is used to perform read and write operations in RTC. If R/W=0, write operation is performed and R/W=1 for read operation.

Address of Read operation of RTC= “0xA3”

Address of Write operation of RTC= “0xA2”

Memory Registers and Address:

RTC registers are located in address locations from 00h to 0Fh and RAM memory registers are located in address locations from 08h to3Fh as shown in figure. RTC registers are used to provide calendar functionality and drive time of day and to display the weekends.

Control/Status Registers:

DB1307 consists of two additional registers such as control/status1 and control/status2 which are used to control real time clock and interrupts.

Control/Status Register1:

  • TEST1=0 normal mode

=1 EXT-clock test mode

  • STOP=0 RTC starts

=1 RTC stop

  • TESTC=0 power on reset disabled

= power on reset enabled

Control/Status Register2:

  • TI/TP= 0 INT active all the time

=1 INT active required time

  • AF=0 Alarm does not match

=1 Alarm match

  • TF=0 Timer overflow does not occur

=1 Timer overflow occurs

  • ALE=0 Alarm interrupts disable

=1 Alarm interrupts enabled

  • TIE=0 Timer interrupts disable

=1 Timer interrupts enable

Step3: Interfacing RTC ds1307 with 8051

RTC can be interfaced to microcontroller by using different serial bus protocols such as I2C and SPI protocols that provide communication link between them. The figure shows, real time clock interfacing with 8051 microcontroller using I2C bus protocol. I2C is a bi-directional serial protocol, which consist of two wires such as SCL and SDA to transfer data between devices connected to bus. 8051 microcontroller has no inbuilt RTC device therefore we have connected externally through a serial communication for ensuring the consisting data.

I2C devices have open drain outputs therefore, a pull-up resistors must be connected to the I2C bus line with a voltage source. If the resistors are not connected to the SCL and SDL lines, the bus will not work.

Step4: RTC Data Framing Format

Since RTC interfacing with8051 microcontroller uses I2C bus therefore the data transfer is in the form of bytes or packets and each byte is followed by an acknowledgement.

Transmitting Data Frame:

In transmitting mode, the master release the start condition after selecting slave device by address bit. The address bit contains 7-bit, which indicate the slave devices as ds1307 address. Serial data and serial clock are transmitted on SCL and SDL lines. START and STOP conditions are recognized as beginning and ending of a serial transfer. Receive and transmit operations are followed by the R/W bit.

Start: Primarily, the data transfer sequence initiated by the master generating the start condition.

7-bit Address: After that the master sends the slave address in two 8-bit formats instead of a single 16-bit address.

Control/Status Register Address: The control/status register address is to allow the control status registers.

Control/Status Register1: The control status register1 used to enable the RTC device

Control/Status Register2: It is used to enable and disable interrupts.

R/W: If read and write bit is low, then the write operation is performed.

ACK: If write operation is performed in the slave device, then the receiver sends 1-bit ACK to microcontroller.

Stop: After completion of write operation in the slave device, microcontroller sends stop condition to the slave device.

Receiving Data Frame:

Start: Primarily, the data transfer sequence initiated by the master generating the start condition.

7-bit Address: After that the master sends slave address in two 8-bit formats instead of a single 16-bit address.

Control/Status Register Address: The control/status register address is to allow control status registers.

Control/Status Register1: The control status register1 used to enable the RTC device

Control/Status Register2: It is used to enable and disable interrupts.

R/W: If read and write bit is high, then the read operation is performed.

ACK: If write operation is performed in the slave device, then the receiver sends 1-bit ACK to microcontroller.

Stop: After completion of write operation in the slave device, microcontroller sends stop condition to the slave device.

Step5: RTC Programming

Write Operation from Master to Slave:

  1. Issue the start condition from master to slave
  2. Transfer the slave address in write mode on SDL line
  3. Send the control register address
  4. Send the control/status register1value
  5. Send the control/status register2 value
  6. Send the date of the like minutes, seconds and hours
  7. Send the stop bit

#include<reg51.h>

sbit SCL=P2^5;
sbit SDA=P2^6;
void start();
void wirte(unsigned char);
delay(unsigned char);

void main()
{

start();
write(0xA2); //slave address//
write(0x00); //control register address//
write(0x00); //control register 1 value//
write(0x00); //control regiter2 vlaue//
write (0x28); //sec value//
write(0x50) ;//minute value//
write(0x02);//hours value//
}

void start()
{

SDA=1; //processing the data//
SCL=1; //clock is high//
delay(100);
SDA=0; //sent the data//
delay(100);
SCL=0; //clock signal is low//
}
void write(unsigned char d)
{

unsigned char k, j=0×80;
for(k=0;k<8;k++)
{
SDA=(d&j);
J=j>>1;
SCL=1;
delay(4);
SCL=0;
}
SDA=1;
SCL=1;
delay(2);
c=SDA;
delay(2);
SCL=0;
}
void delay(int p)
{
unsignedinta,b;
For(a=0;a<255;a++); //delay function//
For(b=0;b<p;b++);
}

Read Operation from Slave to Master:

#include<reg51.h>
sbit SCL=P2^5;
sbit SDA=P2^6;
void start();
void write(usigned char );
void read();
void ack();
void delay(unsigned char);
void main()
{
start();
write(0xA3);// slave address in read mode//
read();
ack();
sec=value;
}
void start()
{

SDA=1; //processing the data//
SCL=1; //clock is high//
delay(100);
SDA=0; //sent the data//
delay(100);
SCL=0; //clock signal is low//
}
void write(unsigned char d)
{

unsigned char k, j=0×80;
for(k=0;k<8;k++)
{
SDA=(d&j);
J=j>>1;
SCL=1;
delay(4);
SCL=0;
}
SDA=1;
SCL=1;
delay(2);
c=SDA;
delay(2);
SCL=0;
}
void delay(int p)
{
unsignedinta,b;
For(a=0;a<255;a++); //delay function//
For(b=0;b<p;b++);
}
Void read ()
{
Unsigned char j, z=0×00, q=0×80;
SDA=1;
for(j=0;j<8;j++)
{
SCL=1;
delay(100);
flag=SDA;
if(flag1)
{
z=(z|q);
q=q>>1;
delay (100);
SCL=0;
}
void ack()
{
SDA=0; //SDA line goes to low//
SCL=1; //clock is high to low//
delay(100);
SCL=0;
}

These are the necessary steps for RTC interfacing with 8051 microcontroller. In additions to these steps, data frames used for transferring and receiving the data is also discussed in this article for user understanding with appropriate programming. For further any help regarding this concept you can leave a comment below.

Related Content

In this simple tutorial, we will interface the 7 segment display with 8051 microcontrollers. we will program using Assembly language or Machine language which is very essential and the mother of all recent programming languages such as C or Embedded C.

About 7 segment Display: A seven-segment is one of the most basic displays that we can display number from 0 to 9( In Binary) or 0-F (in Hexadecimal). The seven-segment display pins (a,b,c,d,e,f,g) in addition to the decimal point of both a common anode and common cathode displays are connect to port pins of LPC1768 via current limiting resistors (220 Omega).

Program For Digital Clock Using 8051

Working principles: The Working principle of this project is very easy. The Counter will count Numbers from 0 to 9 and Loop it again and again. As the name suggests 7 segment displays with 8051 MCU, The code is written in Assembly language.

We have done some other useful tutorials using 7 segment displays such as counter from 0000 to 999 and also Full Digital clock.

Pins Configuration

Seven segments LED is a device having seven light-emitting diodes with either anode terminals (common anode or cathode) terminals. So, they form the shape of number 8.

To use this seven-segment display, once should know the pin configurations very well. As you came to know that, the display has got 9 pins in total,(Ag+DP+Common pins) but essentially, incase of Common Cathode, the display has 2 GNDs that are internally connected, so, the Sum is 9+1 = 10 pins.

A 7-Segment display has 7-segments/pins named as a, b, c, b, e, f, g to form the shape of “8” and another pin called ‘h’ for a decimal point along with two extra pins for GND when it comes to Common Cathode.

Common Cathode: All the positive terminals (Anodes) of all the 8 LEDs are connected together, named as COM Cathode. So, the negative terminals are left alone with a-g

Common Anode: All the negative terminals (Cathodes) of all the 8 LEDs are connected together, named as COM A. Then, all the positive terminals are left alone with a-g so as to connect to the port.

Microcontroller

Components Required

  • AT89C51 Microcontroller
  • 7 segment display
  • Crystal oscillator
  • 22pf Capacitor
  • Power Source 5v-9v

Circuit Diagram

Project Code

You should save the code as .asm since it’s written in Assembly language.

Program For Digital Clock Using 8051 Microcontroller

Project Simulation

That is the end of our simple tutorial. Hope you liked it, don’t forget to subscribe my channel at