Interfacing of RTC DS1307 Device with Microcontroller

RTC Interfacing with 8051 Microcontroller

RTC Interfacing with 8051 Microcontroller

Have you ever wondered, how your phones and laptops keep track of your time when the device is turned OFF. Well, there is a true clock which is named as an RTC (Real Time Clock). This clock is powered with unbroken battery even that the device is turned OFF. When the device is turned ON & connects to the web, then the device connects to an NTP server and updates the date and time. This article is meant to give a little insight regarding RTC DS1307 (real time clock) interfacing with 8051 microcontroller. It is attainable to outline a computer code I2C library if your controller doesn’t have an I2C bus. This article can walk you through the steps concerned in interfacing RTC DS1307 with a microcontroller.

Interfacing of RTC Device with 8051 Microcontroller

Interfacing RTC DS1307  with 8051 microcontroller is related to all other types of  RTCs interfaced to it. So here is a brief information regarding interfacing of RTC DS1307  device with 8051 microcontroller.

Step-1: Selection of RTC Device

There are different types of RTC (real time clock) devices are available in the embedded system market and these are classified into different types like supply voltage, package type and pin diagram etc. A few kinds of RTC devices are;

  • 2-Wire Serial Interface (or) I2C Bus
  • 3-Wire Serial Interface (or) USB BUS
  • 4-wire Serial interface (or) SPI BUS

First of all, we must select the type of device based on type like SPI bus RTC or I2C bus RTC, or any other device, which is apt for interfacing with a microcontroller. Then, choose the features of the RTC device depending on application like suitable package & clock frequency, battery life. Let us consider RTC DS1307 interfacing with 8051 microcontroller.

Step2: Address and Internal Register of the RTC Device

Based on the crystal frequency, the RTC device offers years, months, weeks, days, hours, minutes and seconds. The RTC device comprises of RAM memory and a battery. Here, RAM memory is used to store the data and battery is for backup in case of power supply failure.

Configuration of RTC DS1307

RTC Internal Blocks and Pin Diagram

RTC Internal Blocks and Pin Diagram

  • A0, A1 and A2 pins are address pins
  • VCC and GND pins are power supply (1.8V to 5.5V range) and ground pins.
  • VBT is a battery power supply (2V to 3.5V)
  • SCL pin is a serial clock and it is used to synchronize data on the serial interface.
  • SDL pin is a serial i/p and o/p pin.
  • CLK Out pin is an optional square wave o/p pin.
  • OSC0 & OSC1 pins are crystal oscillator pins

Addressing of the Device

The I2C bus protocol lets many dependent devices at a time. Every dependent device must comprise of own address to denote on it. The master device interconnects with a particular dependent device by an address. The address of the RTC device is OxA2 where 1010 is given by the manufacturer and, address pins A0, A1 and A2 are used to interconnect with eight RTC devices on the 2C bus protocol.

Addressing of Devices

Addressing of Devices

  • R/W bit is used to perform read or write operations in RTC. If R/W is equal to 0, write operation is performed and R/W is equal to 1, read operation is performed.
  • The address of the read operation of  RTC is  0xA3
  • The address of write operation of  RTC is 0xA

Address and Memory Registers

RTC registers are placed at 00h to 0Fh locations. RAM memory registers are also located at 08h to 3Fh locations. These registers are used to run calendar functionality and time of the day and displays the weekends.

Address and Memory Registers

Address and Memory Registers

Status/Control Registers

RTC DS1307 comprises of two extra registers like control or status register1 and control or status register2. These registers are used to control RTC & Interrupts.

Control or Status Register1
Control Status Register1

Control Status Register1

TEST1=0  is the normal mode
=1 EXT is the clock test mode
STOP=0 RTC starts
=1 RTC stop
TESTC=0 power on reset inactivated
= power on reset activated

Control or 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 equal
=1 Alarm match
TF=0 overflow of Timer does not happen
=1 Timer overflow happens
ALE=0 Alarm interrupts inactivate
=1 Alarm interrupts activate
TIE=0 Timer interrupts inactivate
=1 Timer interrupts activate

Step3: Interfacing RTC DS1307 Device with 8051 Microcontroller

RTC DS1307 can be interfaced with 8051 microcontroller using various serial bus protocols like SPI and I2C that provides a communication link between them. The circuit below shows  RTC DS1307 interfacing with 8051 microcontroller using I2C bus protocol. It is a bi-directional serial protocol and it consists of 2-wires like SDA and SCL. These wires are used to transfer data between devices which are connected to the bus. The 8051 microcontroller doesn’t contain inbuilt RTC device. So, we have connected externally via a serial communication for confirming the comprising data.

RTC Interfacing with 8051 Microcontroller1

RTC Interfacing with 8051 Microcontroller1

I2C  bus protocols have open drain o/ps so, a pull-up resistor is connected to the I2C bus line with a voltage source. If the resistors are not connected to the SDL and SCL lines, the I2C bus does not work.

Step4: Format of RTC Data Framing

I2C bus is used in the interfacing of RTC with a microcontroller. So, the data transfer is in the form of packets or bytes, and these are followed by a credit.

Data Transmitting Frame

In transmitting mode, after selecting a slave device by using address bit, the master device releases the start condition. The address bit includes seven bits, that specify the slave devices as DS1307 address. Serial clock and data are transmitted on SDl and SCL lines. START & STOP conditions are  known as beginning & ending of a serial transfer.  Receive & transmit operations are followed by the R/W bit.

Transmitting Data Frame

Transmitting Data Frame

Start:  Mostly, the data transfer sequence introduced by the master to generate the start condition.

7- Bit Address:  Master sends the slave address into two 8-bit formats     in the place of a single 16-bit address.

Control Status Register Address: This register address is mainly used to let the control status registers.

Control Status Register1: This register used to activate the RTC device

Control Status Register2:  This register is used to enable & disable interrupts.

R/W: When read & write bit is low, then the write operation is performed.

ACK: When  write operation is performed on  the slave device, then the microcontroller  receives the 1-bit ACK from the receiver.

Stop:  When write operation of the slave device is completed, then the microcontroller sends a stop condition to slave device.

Receiving Data Frame
Receiving Data Frame

Receiving Data Frame

Start:  Mostly, the data transfer sequence introduced by the master to generate the start condition.

7- Bit Address:  Master sends the slave address into two 8-bit formats     in the place of a single 16-bit address.

Control Status Register Address: This register address is mainly used to let the control status registers.

Control Status Register1: This register used to activate the RTC device

Control Status Register2:  This register is used to enable & disable interrupts.

R/W: When read & write bit is high, then the read operation is performed.

ACK: When a write operation is performed on  the slave device, then the microcontroller  receives the 1-bit ACK from the receiver.

Stop:  When write operation of the slave device is completed, then the microcontroller sends a stop condition to slave device.

Step5: RTC Programming

Write Operation from Master to Slave:

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

This is all about step by step procedure for RTC DS1307 interfacing with 8051 microcontroller. In addition to this, data frames also discussed which are used to transfer and receive the data with an appropriate program. Furthermore, any doubts regarding this concept, or basic electrical or electronic components for projects please give your feedback by commenting in the comment section below. Please refer the  link: Free E-book on how to design your own microcontroller projects- Edgefx kits