HOME ELECTRICAL › ELECTRONICS › COMMUNICATION › ROBOTICS PRO
Projects › Project Ideas IC › Embedded Sensors Components Tools › I
Interfacing RTC (DS1307) with
Microcontroller and its Programming
RTC is an electronic device which plays an essential role in realtime embedded system
provides a precise time and date in various applications such as system clock, student
system and alarm etc, that keep track on current time and provides consistent res
respective task. This article presents RTC interfacing with 8051microcotrollerand basic ac
internal registers.
RTC Interfacing with 8051 Microcontroller
RTC Programming and Interfacing
RTC interfacing with 8051 microcontroller is similar to all other kinds of “Real Tim
interfaced to it. So let us look on simple RTC interfacing with 8051 microcontroller and pro
procedure involving in it.
Step1: Select RTC Device
The various kinds of RTC chips are available in the real time embedded world, which are
based on various criteria such as package type, supply voltage and pin configuration etc. A
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
Bus RTC or other, which suitsfor interfacingwith respective microcontroller. Then we
features of RTC device depending on requirement of application such as battery lif
package and clock frequency. Let us consider two-wire interfacing RTC with 8051 micr
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, m
seconds based on crystal frequency. RTC consists of inbuilt RAM memory for data storage
backup will be provided in case of failure of main power supply by connecting a batt
device.
RTC DB1307 Configuration:
RTC Internal Blocks and Pin Diagram
A0, A1, A2: are address pins of RTC DB1307 chip, which can be used to communicat
master device. We can control eight devices with RTC interfacing with 8051 microcontro
A1, A2 bits using I2C protocol.
VCC and GND: VCC and GND are power supply and ground pins respectively. This devic
with 1.8V to 5.5V range.
VBT: VBT is a battery power supply pin. Battery power source must be held between 2V to
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 i
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 sig
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 cons
address to represent on it. The master device communicates with particular slave de
address. RTC device address is “0xA2” wherein “1010” is given by manufacturer and A0,
user define address, which is used to communicate eight RTC devices on the I2C bus proto
Device Addresing
R/W bit is used to perform read and write operations in RTC. If R/W=0, write operation is
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 re
located in address locations from 08h to3Fh as shown in figure. RTC registers are used
calendar functionality and drive time of day and to display the weekends.
Memory Registers and Address
Control/Status Registers:
DB1307 consists of two additional registers such as control/status1 and control/status2
used to control real time clock and interrupts.
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
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 I2
protocols that provide communication link between them. The figure shows, real
interfacing with 8051 microcontroller using I2C bus protocol. I2C is a bi-directional seri
which consist of two wires such as SCL and SDA to transfer data between devices connec
8051 microcontroller has no inbuilt RTC device therefore we have connected externally
serial communication for ensuring the consisting data.
RTC Interfacing with 8051 Microcontroller
I2C devices have open drain outputs therefore, a pull-up resistors must be connected to th
line with a voltage source. If the resistors are not connected to the SCL and SDL lines, the b
work.
Step4: RTC Data Framing Format
Since RTC interfacing with8051 microcontroller uses I2C bus therefore the data transf
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
bit. The address bit contains 7-bit, which indicate the slave devices as ds1307 address.
and serial clock are transmitted on SCL and SDL lines. START and STOP conditions are
as beginning and ending of a serial transfer. Receive and transmit operations are follow
R/W bit.
Transmitting Data Frame
Start: Primarily, the data transfer sequence initiated by the master generating the start con
7-bit Address: After that the master sends the slave address in two 8-bit formats instead
16-bit address.
Control/Status Register Address: The control/status register address is to allow the contr
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
microcontroller.
Stop: After completion of write operation in the slave device, microcontroller sends stop c
the slave device.
Receiving Data Frame:
Receiving Data Frame
Start: Primarily, the data transfer sequence initiated by the master generating the start con
7-bit Address: After that the master sends slave address in two 8-bit formats instead of a
bit address.
Control/Status Register Address: The control/status register address is to allow con
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-
microcontroller.
Stop: After completion of write operation in the slave device, microcontroller sends stop c
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 addition
steps, data frames used for transferring and receiving the data is also discussed in this
user understanding with appropriate programming. For further any help regarding this co
can leave a comment below.
SHARE THIS POST:
Facebook Twitter Google+ LinkedIn Pinterest
‹ PREVIOUS
GPS Based Voice Navigat
Electronic Testing Equipment and Their Types
Visually Im
RELATED CONTENT
What is Non-Inverting Op-Amp : What is an Absolute Encoder : What is a Rotameter : Working & What is Ro
Working & Its Applications Working & Its Applications Its Applications Working &
3 Comments
Afnas Says:
at
i need complete program to interface 8051 with RTC. can you please help me
RANJITH P Says:
at
Hello sir,I am getting warnings if i put the codes in keil software.can you please help out
Sravanthi Says:
at
I need the complete code for interfacing the rtc ds1307 with 8051 and display it on the lcd
Add Comment
Comment:
Name *
Email *
Website
Post Comment
CATEGORIES RECENT COMMENTS
Communication Tarun Agarwal on What is a Full Wave
Rectifier : Circuit with Working Theory
Electrical
Haswell Frank on What is a Full Wave
Electronics
Rectifier : Circuit with Working Theory
Project Ideas
Jakkula gayathri on Seminar Topics for
Robotics Electronics and Communication Engineering
Students
Technology
Emmanuel Anthony on Understanding of
Cathode Ray Tube – CRT
Advertise With Us Disclaimer Report Violation Image Usage
Copyright 2013 - 2021 © Elprocus