Professional Documents
Culture Documents
(RTC)
Project report submitted in partial fulfillment of the requirements
For the award of the degree of
BACHELOR OF TECHNOLOGY
IN
ELECTRICAL AND ELECTRONICS ENGINEERING
BY
A.AMALA
(07241A0201)
NITHYA RAGHUNATH
(07241A0209)
CERTIFICATE
(07241A0201)
NITHYA RAGHUNATH
(07241A0209)
GRIET, Hyderabad
GRIET, Hyderabad
ASST. PROF
(Internal Guide)
INDEX
S.NO
TOPIC
PAGE
NO.
INTRODUCTION
DESCRIPTION
OPERATION
17
REAL-TIME CLOCK
4
19
AT89C51
5
APPLICATIONS OF RTC
35
CONCLUSION
38
1. INTRODUCTION
1.1 THE REAL-TIME CLOCK
A real-time clock (RTC) is a computer clock (most often in the form of an integrated
circuit) that keeps track of the current time. Although the term often refers to the devices in
personal computers, servers and embedded systems, RTCs are present in almost any
electronic device which needs to keep accurate time.
A real time clock (RTC) is a timepiece module having an independent battery for
operation and has a backup RAM always supplied with electric power from the battery. Many
data processing circuits utilize real-time clocks to provide a real-time clock value indicating,
for example, the current day, date and time.
Typically, when the data processing circuit is first activated, the correct day, date and time
may need to be set. When the data processing circuit is shut down, power is maintained to the
real-time clock by a battery, so that the real-time clock may continue to operate. Personal
computers and digital electronics typically include a real time clock that tracks the time of
day, day, month, and year. The use of wireless communication systems continues to expand.
In some digital cellular systems, the in-phone real-time clock is synchronized to the
precise local time given by the base station to which the phone is communicating with
currently. This feature is known as automatic local time adjustment. To implement automated
and programmable functions, a real time clock within the wireless device is generally
required. The real time clock provides a precise representation of time which can be
compared to stored values in registers to provide automated functions. During power
interruptions, the real time clock is typically powered from either a dedicated clock battery,
such as a small button cell, or from a storage capacitor.
On a computer mother board, a RTC is provided to maintain the data stored in a
CMOS on the mother board. Generally, electrical power is provided to a RTC by a battery
arranged on the mother board. The RTC is used to update the current time and date within the
computer system without any intervention from the system processor. Usually the RTC and
other peripheral devices are coupled to the peripheral bus through an interrupt controller.
Such a clock has various uses such as time stamping files and inserting dates into documents.
5
The RTC is used to update a program clock, typically a date/time clock maintained by the
operating system (OS) and referred to herein as the OS clock. An operating system may
include a system clock to provide a system time for measuring small increments of time. The
system clock may update the system clock in response to a periodic interrupt generated by a
system timer, or a real time clock event timer.
A Real-Time-Clock (RTC) is, as the name suggests, a clock which keeps track of time
in a "real mode." While there are a number of 8051-compatible microcontrollers that have
built-in, accurate real-time clocks (especially from Dallas Semiconductor), some simple
applications may benefit from a software RTC solution that uses the built-in capabilities of an
8051 microcontroller.
1.2 BENEFITS
The purpose of using real-time clocks has the following benefits:
1.4 TIMING
Most RTCs use a crystal oscillator, but some use the power line frequency. In many
cases the oscillator's frequency is 32.768 kHz. This is the same frequency used in quartz
clocks and watches, and for the same reasons, namely that the frequency is exactly 215 cycles
per second, which is a convenient rate to use with simple binary counter circuits.
1.5 USAGE
A real-time clock (RTC) is a battery-powered clock that is included in a microchip in
a computer motherboard. This microchip is usually separate from the microprocessor and
other chips and is often referred to simply as "the CMOS" (complementary metal-oxide
semiconductor). A small memory on this microchip stores system description or setup values
- including current time values stored by the real-time clock. The time values are for the year,
month, date, hours, minutes, and seconds. When the computer is turned on, the Basic InputOutput Operating System (BIOS) that is stored in the computer's read-only memory (ROM)
microchip reads the current time from the memory in the chip with the real-time clock.
Many integrated circuit manufacturers make RTCs, including Intersil, Maxim,
Philips, Texas Instruments and STMicroelectronics. The RTC was introduced to PC
compatibles by the IBM PC/AT in 1984, which used a MC146818 RTC. Later Dallas made
compatible RTCs, which was often used in older personal computers, and are easily found on
motherboards because of their distinctive black battery cap and silkscreened logo. In newer
systems the RTC is integrated into the southbridge chip.
Popular serial clock circuit is numerous now, such as DS1302, DS1307,
PCF8485,etc.. The interface of the circuit is simple, cheap, easy to use, widely adopted. The
real-time clock circuit DS1302 is that one kind of DALLAS Company has circuit that
detailed electric current of tiny stream charges ability, the leading particulars adopts the serial
data transmission, can offer the programming charging function for losing power of electric
protection, and can close the charging function.
Structure
and
operating
principle
of
DS1302
DS1302 is a kind of high performance which U.S.A. DALLAS Company introduces, low
power consumption, real-time clock circuit with RAM, it can for day, month and year,
Sunday, when, go on minute or second time, have leap year to compensate the function,
working voltage is 2.5V- 5.5V. Adopt three line interfaces and CPU to carry on synchronous
communication, a clock signal or RAM datum of conveying a plurality of byte that and can
adopt burst way. There are RAM registers used for preserving the data in temporariness of
31* 8 for one within DS1302. DS1302 is the upgrading products of DS1202, compatible with
DS1202, but increased the power duplicate supply pin of the main power source / back,
7
offered and carried on the charged ability of detailed electric current of tiny stream to the
power of back at the same time.
This is one kind of RTC that is usually used.
Some microcontrollers have a real-time clock built in, generally only the ones with many
other features and peripherals.
The complete operation of the DS1307 RTC and its use in controlling home appliances is
described in the other chapters.
2. DESCRIPTION
2.1 GENERAL DESCRIPTION
The DS1307 serial real-time clock (RTC) is a low power, full binary-coded decimal
(BCD) clock/calendar plus 56 bytes of NV SRAM.
Address and data are transferred serially through an I2C, bidirectional 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 backup supply. Timekeeping operation
continues while the part operates from the backup supply.
2.2 FEATURES
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
The typical operating circuit and pin configurations of DS1307 is shown in the figure above
10
TIMING DIAGRAM
12
FUNCTION
X1
X2
VBAT
GND
Ground
SDA
Serial Data Input/Output. SDA is the data input/output for the I2C serial
interface. The SDA pin is open drain and requires an external pullup
resistor. The pullup voltage can be up to 5.5V regardless of the voltage
on VCC.
SCL
Serial Clock Input. SCL is the clock input for the I2C interface and is
used to synchronize data movement on the serial interface. The pullup
voltage can be up to 5.5V regardless of the voltage on VCC
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 pullup resistor. SQW/OUT operates with either VCC or VBAT
13
VCC
15
16
continues to run. This eliminates the need to re-read the registers in case the internal registers
update during a read. The divider chain is reset whenever the seconds register is written.
Write transfers occur on the I2C acknowledge from the DS1307. Once the divider chain is
reset, to avoid rollover issues, the remaining time and date registers must be written within
one second.
Bit 7: Output Control (OUT). This bit controls the output level of the SQW/OUT pin when
the square-wave output is disabled. If SQWE = 0, the logic level on the SQW/OUT pin is 1 if
OUT = 1 and is 0 if OUT = 0. On initial application of power to the device, this bit is
typically set to a 0.
17
Bit 4: Square-Wave Enable (SQWE). This bit, when set to logic 1, enables the oscillator
output. The frequency of the square-wave output depends upon the value of the RS0 and RS1
bits. With the square-wave output set to 1Hz, the clock registers update on the falling edge of
the square wave. On initial application of power to the device, this bit is typically set to a 0.
Bits 1 and 0: Rate Select (RS[1:0]). These bits control the frequency of the square-wave
output when the squarewave output has been enabled. The following table lists the squarewave frequencies that can be selected with the RS bits. On initial application of power to the
device, these bits are typically set to a 1.
Data transfer can 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.
18
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 bytewise and each receiver acknowledges with a ninth bit. Within the I2C bus specifications a
standard mode (100kHz clock rate) and a fast mode (400kHz clock rate) are defined.
The DS1307 operates in the standard mode (100kHz) only.
19
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 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.
20
3. OPERATION
The DS1307 can operate in the following two modes:
1. Slave Receiver Mode (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. Hardware performs
address recognition after reception of the slave address and direction bit (see Figure 4). The
slave address byte is the first byte received after the master generates the START condition.
The slave address byte contains the 7-bit DS1307 address, which is 1101000, followed by the
direction bit (R/W), which for a write is 0. After receiving and decoding the slave address
byte, the DS1307 outputs an acknowledge on SDA. After the DS1307 acknowledges the
slave address + write bit, the master transmits a word address to the DS1307. This sets the
register pointer on the DS1307, with the DS1307 acknowledging the transfer. The master can
then transmit zero or more bytes of data with the DS1307 acknowledging each byte received.
The register pointer automatically increments after each data byte are written. The master will
generate a STOP condition to terminate the data write.
2. Slave Transmitter Mode (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. The DS1307 transmits serial data on SDA 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 5). The slave address byte is the first byte received after the START
condition is generated by the master. The slave address byte contains the 7-bit DS1307
address, which is 1101000, followed by the direction bit (R/W), which is 1 for a read. After
receiving and decoding the slave address the DS1307 outputs an acknowledge on SDA. 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 register pointer
automatically increments after each byte are read. The DS1307 must receive a Not
Acknowledge to end a read.
21
22
23
/********************************************************************/
/* DEMO1307.c
*/
/********************************************************************/
#include <at89x52.h>
#include <stdio.h>
/***************************** Defines *****************************/
#define
ACK
#define
NACK
#define
ADDRTC
#define
DS1307
1
0xd0
void
I2C_start();
void
I2C_stop();
void
uchar
I2C_read(uchar);
void
readbyte();
void
writebyte();
void
initialize();
24
void
disp_clk_regs(uchar);
void
burstramwrite(uchar);
void
burstramread();
void
alrm_int();
void
alrm_read();
void
tc_setup();
/* global variables */
uchar
void I2C_start()
/* ----------------------------------------------*/
{
sda = 1; scl = 1;
sda = 0;
}
void I2C_stop()
/* ---------------------------------------*/
{
sda = 0; sda = 0; sda = 0; sda = 0;
/* ----------------------------- */
{
uchar i;
scl = 0;
for (i = 1; i <= 8; i++)
{
sda = (d >> 7);
scl = 1;
d = d << 1;
scl = 0;
}
sda = 1;
scl = 0;
scl = 1;
if(sda) printf("Ack bit missing %02X",(unsigned int)d);
scl = 0;
}
uchar I2C_read(uchar b)
/* ----------------------------------- */
{
uchar d, i;
25
sda = 1;
scl = 0;
for (i = 1; i <= 8; i++)
{
scl = 1;
d = d << 1;
d = d | (unsigned char)sda;
scl = 0;
}
sda = b;
scl = 0;
scl = 1;
if(b == NACK)
scl = 0;
sda = 1;
return d;
}
void readbyte() /* -- read one byte of data from the specific address -- */
{
uchar Add;
printf("ADDRESS: ");
/* Get Address */
scanf("%bx", &Add);
I2C_start();
I2C_write(ADDRTC);
I2C_write(Add);
I2C_start();
I2C_write(ADDRTC | 1);
printf("%2bx", I2C_read(NACK) );
I2C_stop();
}
void writebyte()/* -- write one byte of data to the specified address -- */
{
uchar Add;
uchar Data;
printf("Address: ");
/* Get Address */
scanf("%bx", &Add);
printf("DATA: ");
scanf("%bx", &Data);
/* and data */
26
I2C_start();
I2C_write(ADDRTC);
I2C_write(Add);
I2C_write(Data);
I2C_stop();
}
void initialize() /*initialize the time and date using entries from stdin*/
/* Note: NO error checking is done on the user entries! */
{
uchar
I2C_start();
I2C_write(ADDRTC);
I2C_write(0x00);
I2C_start();
I2C_write(ADDRTC);
I2C_write(0x00);
I2C_write(sec);
I2C_write(min);
I2C_write(hr);
I2C_write(dy);
I2C_write(dt);
27
I2C_write(mn);
I2C_write(yr);
#if defined DS1307 || defined DS1338
{
I2C_write(0x10);
}
#elif defined DS1337 || defined DS1339
{
I2C_start();
I2C_write(ADDRTC);
I2C_write(0x0e);
I2C_write(0x20);
I2C_write(0);
}
#elif defined DS1340
I2C_write(0x10);
I2C_start();
I2C_write(ADDRTC);
I2C_write(0x09);
I2C_write(0);
}
#endif
I2C_stop();
}
void
disp_clk_regs(uchar prv_sec)
/* ----------------------------------------- */
{
uchar Sec, Min, Hrs, Dte, Mon, Day, Yr, mil, pm;
printf("Yr Mn Dt Dy Hr:Mn:Sc");
while(!RI)
{
I2C_start();
I2C_write(ADDRTC);
I2C_write(0x00);
I2C_start();
I2C_write(ADDRTC | 1);
28
Sec = I2C_read(ACK);
Min = I2C_read(ACK);
Hrs = I2C_read(ACK);
Day = I2C_read(ACK);
Dte = I2C_read(ACK);
Mon = I2C_read(ACK);
Yr = I2C_read(NACK);
I2C_stop();
if(Hrs & 0x40)
mil = 0;
else
mil = 1;
if(Sec != prv_sec)
{
if(mil)
{
printf("%02bX/%02bX/%02bX %2bX", Yr, Mon, Dte, Day);
printf(" %02bX:%02bX:%02bX", Hrs, Min, Sec);
}
else
{
if(Hrs & 0x20)
pm = 'A';
else
pm = 'P';
Hrs &= 0x1f;
return;
prv_sec = Sec;
}
RI = 0; /* Swallow keypress before exiting */
}
void burstramwrite(uchar Data)
{
uchar j;
I2C_start();
29
I2C_write(ADDRTC);
I2C_write(0x08);
{
I2C_write(Data);
}
I2C_stop();
}
void burstramread()
/* ----------------------------------------- */
{
uchar j;
I2C_start();
I2C_write(ADDRTC);
I2C_write(8);
I2C_start();
I2C_write(ADDRTC | 1);
alrm_int()
{
uchar
M = _getkey();
switch(M)
{
case '1':
M = 0xf;
break;
case '2':
M = 0xe;
break;
30
case '3':
M = 0xc;
break;
case '4':
M = 8;
break;
case '5':
M = 0;
break;
case '6':
M = 0x40;
break;
}
if(M & 0x40)
{
printf("Enter the day (1-7): ");
scanf("%bx", &DyDt);
}
else
{
printf("Enter the date (1-31): ");
scanf("%bx", &DyDt);
}
printf("Enter the hour (1-23): ");
scanf("%bx", &Hr);
printf("Enter the minute (0-59): ");
scanf("%bx", &Min);
printf("Enter the second (0-59): ");
scanf("%bx", &Sec);
if( (M & 1) )
Sec |= 0x80;
Min |= 0x80;
Hr |= 0x80;
DyDt |= 0x80;
DyDt |= 0x40;
I2C_start();
I2C_write(ADDRTC);
I2C_write(7);
I2C_write(Sec);
I2C_write(Min);
I2C_write(Hr);
I2C_write(DyDt);
I2C_start();
I2C_write(ADDRTC);
I2C_write(0x0e);
I2C_write(5);
I2C_stop();
31
}
void
alrm_read()
{
uchar
I2C_start();
I2C_write(ADDRTC);
I2C_write(7);
I2C_start();
I2C_write(ADDRTC | 1);
Sec = I2C_read(ACK);
Min = I2C_read(ACK);
Hr = I2C_read(ACK);
DyDt = I2C_read(NACK);
tc_setup()
{
uchar
M, val;
TC 0x10
TC 0x08
#else
#define
#endif
M = _getkey();
switch(M)
{
case '1':
val = 1;
break;
case '2':
val = 2;
break;
32
case '3':
val = 3;
break;
}
printf("1-no diode2-1 diode");
M = _getkey();
switch(M)
{
case '1':
val += 4;
break;
case '2':
val += 8;
break;
}
I2C_start();
I2C_write(ADDRTC);
I2C_write(TC);
I2C_write(val | 0xa0);
I2C_stop();
}
else
{
I2C_start();
I2C_write(ADDRTC);
I2C_write(TC);
I2C_write(0);
I2C_stop();
}
I2C_start();
I2C_write(ADDRTC);
I2C_write(TC);
I2C_start();
I2C_write(ADDRTC | 1);
(void)
/* ----------------------------------------------------- */
{
uchar
M, M1;
sqw = 1;
while (1)
{
33
RW RAM Write");
#endif
M = _getkey();
switch(M)
{
case 'A':
case 'a':
printf("Init or Read: ");
M1 = _getkey();
switch(M1)
{
case 'I':
case 'i':
alrm_int();
break;
34
case 'R':
case 'r':
alrm_read();
break;
}
break;
case 'B':
case 'b':
printf("Read or Write: ");
M1 = _getkey();
switch(M1)
{
case 'R':
case 'r':
readbyte();
break;
case 'W':
case 'w':
writebyte();
break;
}
break;
case 'C':
case 'c':
printf("\rEnter Clock Routine to run:C");
M1 = _getkey();
switch(M1)
{
case 'I':
case 'i':
initialize();
break;
case 'R':
case 'r':
disp_clk_regs(0x99);
break;
}
break;
case 'R':
case 'r':
printf("\rEnter Ram Routine to run:R");
M1 = _getkey();
35
switch(M1)
{
case 'R':
case 'r':
burstramread();
break;
case 'W':
case 'w':
break;
}
break;
case 'T':
case 't':
tc_setup();
break;
}
}
//-----------------------------------
36
RTC_ARR[1] = 0x50;
// minute = 59
RTC_ARR[2] = 0x12;
RTC_ARR[3] = 0x03;
// Day = 1 or sunday
RTC_ARR[4] = 0x10;
// Date = 30
RTC_ARR[5] = 0x08;
// month = August
RTC_ARR[6] = 0x10;
// year = 05 or 2005
WriteRTC(&RTC_ARR[0]);
// Set RTC
//----------------------------------while(1)
{
if(RTC_ARR[1]==0x51)
P1=0xaa;
if(RTC_ARR[1]==0x52)
P1=0x55;
ReadRTC(&RTC_ARR[0]);
putchar(0x0C); // clear Hyper terminal
printf("calender \n");
printf("Day : %s\r\n",Int2Day(RTC_ARR[3]));
printf("Time : %02bX:%02bX:%02bX\r\n",RTC_ARR[2],RTC_ARR[1],RTC_ARR[0]);
printf("Date
20%02bX",RTC_ARR[4],Int2Month(RTC_ARR[5]),RTC_ARR[6]);
//
delay(10000);
37
%02bX-%s-
}
}
void delay(int time)
{
int i,j;
for(i=0;i<time;i++);
for(j=0;j<5000;j++);
}
38
4.3 CIRCUIT
39
5. APPLICATIONS OF RTC
A real time clock (RTC) circuit may be used to maintain rough GPS time
while the rest of the GPS circuitry is off. The global positioning system (GPS) is a
collection of twenty-four earth-orbiting satellites. Each of the GPS satellites travels in
a precise orbit about 11,000 miles above the earth's surface. A GPS receiver locks
onto at least three of the satellites to determine its precise location.
Real time clock is also used in managed network elements for time stamping
event reports sourced from the respective element. Such reports may, for example, be
the start or finish times of a traffic connection
Clock circuits are an essential component of modern computer systems
amount of I/O bus interface circuitry and other circuitry onto only a few chips. These
chipsets may implement the I/O bus interface circuitry, timer, real-time clock (RTC),
direct memory access (DMA) controller, and other additional functionality such as
integrated power and thermal management with quick resume capabilities and random
seed number generation for security applications such as cryptography, digital
signatures, and protected communication protocols. Clock circuits generate a regular
series of pulses based on a piezoelectric crystal, which governs the frequency of the
pulses. The clock signal that is generated is used to synchronize the operation of the
other components and circuits in the system. The boot up operation of a computer
system is reliant upon the establishment of a reliable system clock. Establishing this
reliable signal adds additional time to the boot up process for a system.
An electronic apparatus generally has a RTC (real time clock) installed in the
circuit board thereof for recording system time. A real time clock is one of the
requisite hardware devices of an electronic device. A standard RTC includes an
oscillating circuit coupled to a digital counter.
42
43
6.CONCLUSION
Real time clocks can be much more accurate than other time-keeping alternatives,
they allow the main system to perform important tasks, and they do not consume much
power. Electronic devices can even increase their functionality by using real-time clocks.
Certain electronic devices can rely on real-time clocks when comparing the times of previous
functions. If the functions have taken place within a designated period of time, device
functions can be reduced drastically.
Hence real time clocks using microcontrollers are used extensively in controlling
home appliances
44