Interfacing RTC (DS1307) with Microcontroller and its Programming

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 Interfacing with 8051 Microcontroller
RTC Interfacing with 8051 Microcontroller

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:

RTC Internal Blocks and Pin Diagram
RTC Internal Blocks and Pin Diagram

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.

Device Addresing
Device Addresing

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.

Memory Registers and Address
Memory Registers and Address

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:

Control Status Register1
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:

Control Status Register2
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.

RTC Interfacing with 8051 Microcontroller
RTC Interfacing with 8051 Microcontroller

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.

Transmitting Data Frame
Transmitting Data Frame

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:

Receiving Data Frame
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(flag==1)
{
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.

6 Comments

  1. i need complete program to interface 8051 with RTC. can you please help me

    1. Tarun Agarwal says:

      Hi
      Sorry to inform you that we don’t have that project, and we cannot provide you like that
      And once again please visit our domestic website https://www.edgefxkits.com/
      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

  2. RANJITH P says:

    Hello sir,I am getting warnings if i put the codes in keil software.can you please help out

  3. Sravanthi says:

    I need the complete code for interfacing the rtc ds1307 with 8051 and display it on the lcd

    1. Tarun Agarwal says:

      Hi Sravanthi
      I sincerely appreciate your kind response regarding my article. once you purchase project from our website then we provide all the documents, source code, circuit diagram, tool kit and components.

      And once again please visit our domestic website https://www.edgefxkits.com/

      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

Add Comment