Program For Digital Clock Using 8051



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.

Real time digital clock is a very accurate digital clock made by using 8051 microcontroller and ds1307 real time clock chip. Ds1307 chip run the time accurately by using 32.768 KHz Crystal oscillator. 8051 microcontroller sends the command to ds1307 to configure and read the time date day and year. Real time clock is very easy to made and components are easily available in the market. Hello friends, hope you all are fine and having fun with your lives. 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 to use Timer Interrupt in 8051 Microcontroller.We have seen in that post that we can use two timers in 8051 Microcontroller which are Timer0 and Timer1.

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.


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:

8051 Instruction Set Pdf

  • 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
Program For Digital Clock Using 8051

=1 Timer interrupts enable

8051 Microcontroller Applications

Step3: Interfacing RTC ds1307 with 8051

Program For Digital Clock Using 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.

For

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

8051 Programming Board

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

8051 Program Counter

#include<reg51.h>

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

Program For Digital Clock Using 8051

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)
{

Program For Digital Clock Using 8051 Microcontroller

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()
{

Program For Digital Clock Using 8051 Microcontroller

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;
}

Program for digital clock using 8051 switch

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.

8051 Microcontroller Projects Using Assembly

Related Content