RTC INTERFACING
RTC
INTERFACING
9.1 INTRODUCTION
The real time clock is
most widely device that provides accurate time and date for many applications.
RTC chip has the capability of providing time components of hour, minute and
second, in addition to date/calendar components of year, month, and day. The
RTC chip uses an internal battery, which keeps the time and date even when
power is off. Although some 8051 microcontrollers come with RTC embedded into the
chip, we have to interface vast majority of them external to the 8051 chip. One
of most widely used RTC chip is DS1307 from Dallas Semiconductor/Maxim Corp. This
RTC chip has specific features of Two-wire Serial Interface. The DS1307 Serial
Real-Time Clock is a low-power; full binary-coded decimal (BCD) clock/calendar
plus 56 bytes of NV SRAM. Address and data are transferred serially via a
2-wire, bi-directional bus. The clock/calendar provides seconds, minutes,
hours, day, date, month, and year information. The end of the month date is
automatically adjusted for months with fewer than 31 days, including
corrections for leap year. The clock operates in either the 24-hour or 12 hour
format with AM/PM indicator. The DS1307 has a built in power sense circuit that
detects power failures and automatically switches to the battery supply. Next
section describes you some of the features of DS1307 RTC chip.
9.2 FEATURES
1. Real-time clock
(RTC) counts seconds, minutes, hours, date of the month, month, day of the
week, and year with leap-year compensation valid up to 2100 years.
2. 56-byte,
battery-backed, nonvolatile (NV) RAM for data storage.
3. Two-wire serial
interface..
4. Programmable square wave
output signal.
5. Automatic power-fail
detects and switch circuitry.
6. Consumes less than
500nA in battery backup mode with oscillator running.
7. Optional industrial
temperature range: -40‹C to +85‹C.
8. Available in 8-pin
DIP or SOIC.
9.3 Pin
Descriptions
This Section describes
you DS1307 pins. See Figure 9.1
VCC, GND:
DC power is provided to
the device on these pins. VCC is the +5V input. When 5V is applied within
normal limits, the device is fully accessible and data can be written and read.
When a 3V battery is connected to the device and VCC is below 1.25 x VBAT,
reads and writes are inhibited. However, the timekeeping function continues
unaffected by the lower input voltage. As VCC falls below VBAT the RAM and
timekeeper are switched over to the external power supply (nominal 3.0V DC) at VBAT.
VBAT:
Battery input for any
standard 3V lithium cell or other energy source. Battery voltage must be held
between 2.0V and 3.5V for proper operation. The nominal write protect trip
point voltage at which access to the RTC and user RAM is denied is set by the
internal circuitry as 1.25 x VBAT nominal. A lithium battery with 48mAhr or
greater will back up the DS1307 for more than 10 years in the absence of power
at 25oC. UL recognized to ensure against reverse charging current when used in
conjunction with a lithium battery.
SCL (Serial Clock
Input): SCL is used to synchronize data movement on serial interface.
SDA (Serial Data Input /Output):
SDA is the input/output pin for the 2-wire serial interface. The SDA pin is
open drain which requires an external pull-up resistor.
SQW/OUT (Square
Wave/Output Driver) :When enabled, the SQWE bit set to 1, the SQW/OUT pin
outputs one of four square wave frequencies (1Hz, 4kHz, 8kHz, 32kHz). The
SQW/OUT pin is open drain and requires an external pull-up resistor. SQW/OUT
will operate with either Vcc or Vbat applied.
X1, X2: Connections for
a standard 32.768 kHz quartz crystal. The internal oscillator circuitry is
designed for operation with a crystal having a specified load capacitance (CL)
of 12.5pF. The DS1307 can also be driven by an external 32.768 kHz Oscillator. In
this configuration, the X1 pin is connected to the external oscillator signal and
the X2 pin is floated.
9.4 Operation
Based on I2C protocol
The DS1307 supports a
bi-directional, 2-wire bus and data transmission protocol that is called I2C
protocol. A device that sends data onto the bus is defined as a transmitter and
a device receiving data as a receiver. The device that controls the message is
called a master. The devices that are controlled by the master are referred to
as slaves. The bus must be controlled by a master device that generates the
serial clock (SCL), controls the bus access, and generates the START and STOP
conditions. The DS1307 operates as a slave on the 2-wire bus. A typical bus
configuration using this 2-wire protocol is show in Figure 4. Figures 5, 6, and
7 detail how data is transferred on the 2-wire bus. Data transfer may be initiated
only when the bus is not busy. During data transfer, the data line must remain
stable whenever the clock line is HIGH. Changes in the data line while the clock
line is high will be interpreted as control signals. Accordingly, the following
bus conditions have been defined:
Bus not busy: Both data
and clock lines remain HIGH.
Start data transfer: A
change in the state of the data line, from HIGH to LOW, while the clock is
HIGH, defines a START condition.
Stop data transfer: A
change in the state of the data line, from LOW to HIGH, while the clock line is
HIGH, defines the STOP condition.
Data valid: The state
of the data line represents valid data when, after a START condition, the data
line is stable for the duration of the HIGH period of the clock signal. The
data on the line must be changed during the LOW period of the clock signal. There
is one clock pulse per bit of data. Each data transfer is initiated with a START
condition and terminated with a STOP condition. The number of data bytes transferred
between START and STOP conditions is not limited, and is determined by the
master device. The information is transferred byte-wise and each receiver acknowledges
with a ninth bit. Within the 2-wire bus specifications a regular mode
(100 kHz clock rate)
and a fast mode (400 kHz clock rate) are defined. The DS1307 operates in the
regular mode (100 kHz) only.
Acknowledge: Each
receiving device, when addressed, is obliged to generate an acknowledge after the
reception of each byte. The master device must generate an extra clock pulse which
is associated with this acknowledge bit. A device that acknowledges must pull
down the SDA line during the acknowledge clock pulse in such a way that the SDA
line is stable LOW during the HIGH period of the acknowledge related clock
pulse. Of course, setup and hold times must be taken into account. A master
must signal an end of data to the slave by not generating an acknowledge bit on
the last byte that has been clocked out of the slave. In this case, the slave
must leave the data line HIGH to enable the master to generate the STOP
condition.
9.5 DATA
TRANSFER ON 2-WIRE SERIAL BUS
Depending upon the
state of the R/W bit, two types of data transfer are possible:
1. Data transfer from a
master transmitter to a slave receiver. The first byte transmitted by the
master is the slave address. Next follows a number of data bytes. The slave
returns an acknowledge bit after each received byte. Data is transferred with
the most significant bit (MSB) first.
2. Data transfer from a
slave transmitter to a master receiver. The first byte (the slave address) is
transmitted by the master. The slave then returns an acknowledge bit. This is
followed by the slave transmitting a number of data bytes. The master returns
an acknowledge bit after all received bytes other than the last byte. At the
end of the last received byte, a not acknowledge is returned. The master device
generates all of the serial clock pulses and the START and STOP conditions. A
transfer is ended with a STOP condition or with a repeated START condition.
Since a repeated START condition is also the beginning of the next serial
transfer, the bus will not be released. Data is transferred with the most
significant bit (MSB) first.
The DS1307 may operate
in the following two modes:
1. Slave receiver mode
(DS1307 write mode): Serial data and clock are received through SDA and SCL. After
each byte is received an acknowledge bit is transmitted. START and STOP
conditions are recognized as the beginning and end of a serial transfer.
Address recognition is performed by hardware after reception of the slave
address and *direction bit (See Figure 6). The address byte is the first byte
received after the start condition is generated by the master. The address byte
contains the 7 bit DS1307 address, which is 1101000, followed by the *direction
bit (R/ W) which, for a write, is a 0. After receiving and decoding the address
byte the device outputs an acknowledge on the SDA Line. After the DS1307
acknowledges the slave address + write bit, the master transmits a register
address to the DS1307 this will set the register pointer on the DS1307. The
master will then begin transmitting each byte of data with the DS1307
acknowledging each byte received. The master will generate a stop condition to
terminate the data write.
2. Slave transmitter
mode (DS1307 read mode): The first byte is received and handled as in the slave
receiver mode. However, in this mode, the *direction bit will indicate that the
transfer direction is reversed. Serial data is transmitted on SDA by the DS1307
while the serial clock is input on SCL. START and STOP conditions are
recognized as the beginning and end of a serial transfer (See Figure 7). The
address byte is the first byte received after the start condition is generated
by the master. The address byte contains the 7-bit DS1307 address, which is
1101000, followed by the*direction bit (R/ W) which, for a read, is a 1. After
receiving and decoding the address byte the device inputs an acknowledge on the
SDA line. The DS1307 then begins to transmit data starting with the register
address pointed to by the register pointer. If the register pointer is not
written to before the initiation of a read mode the first address that is read
is the last one stored in the register pointer. The DS1307 must receive a not
acknowledge to end a read.
9.6 Hardware
Criteria
RTC is based on i2c
protocol which is two wired protocol. So we required only two pins to send
address, to set RTC. This two pins serial data line and serial clock line are
connected to any of two port pins. In our hardware connection we use p3.6 for
SCL and p3.7 for SDA. To keep always on, RTC even in off supply 3v battery is connected
to pin3 of DS-1307. VCC and GROUND is applied to RTC.
Comments