Back to Top

DS1302 RTC MODULE


Introduction


Image not found...
(Fig1) An RTC module using DS1302 chip
For those who might wonder, RTC.... Stands for Real Time Clock.
So... RTC's.... Why do we have to use RTC's to keep date and time? We can use microcontrollers, microprocessors
or even timer IC's to count time as well... But why RTC's ? (That was indeed my very first question...)
One of my favorite microcontrollers, as a matter of fact the pic16lf1939, has built-in 5 hardware timer modules.
4 of them are 8-bit length, and one of them is 16-bit length. So with a little bit of programming we could have
used the internal timers to count time. But instead of them, we use RTC chips to do the same job. Why???
The answer lies on accuracy!!!.
Real Time Clocks have a unit of measurement that indicates how accurate the RTC is, and this called ppm.
(Also known as parts per million) The lower that number is.... the more accurate the RTC is...
Right now i am aware of 5 'technologies' that RTC's use to measure time.

1. Crystal (Quartz Crystal)
2. VCXO (Voltage Controlled Crystal Oscillator)
3. TCXO (Temperature Compensated Crystal Oscillator)
4. OCXO (Oven Controlled Crystal Oscillator)
5. Atomic (Cesium-Hydrogen-Rubidium)

The DS1302 was the first chip i experimented with, and i must admit that it is not so accurate as the following RTC i used which is the DS3231.
(It will be the subject to a different tutorial, since i am very pleased with the performance that this chip provides.)

This chip (the DS1302) uses a simple crystal and to be more specific a 32.768khz quartz crystal which provides the oscillation to count time.
But due to ambient temperature variations, crystal capacitance, pcb capacitance, and parasitic noise, the accuracy changes accordinally.
That is the why the other technologies have been developed to overlap, or minimize non-stable measurements.
I won't get in detail of how these technologies work, (and of course their operational principle), since you can find many great explainatory articles on the web.

Closing i would like to add that this chip except the RTC function, it has a built-in charger which provides trickle charging to the battery, and 31 bytes of static RAM.
The communication to the chip is accomplished via a simple serial interface, and only by using three wires where are the SCK , I/O , CE.
The RTC except time, it provides also date information. It has also a neat feauture at the end of the month, where the date is adjusted
automatically for months which have less than 31 days, including corrections for leap years.
You can also operate the clock in either 24-hour or 12-hour format, with an AM/PM indicator.
And finally the DS1302 is designed to operate on very low power and retain data and clock information on less than 1µW.

PCB Schematic


So once again i did my reverse engineering, and i ended up to the following schematic...
This board (Fig1) above, has two major drawbacks. If you read the datasheet of the chip you will be informed of three critical considerations that must be taken
in account when you desigh a pcb, as a result to minimize the fault measurement of the RTC.
Firstly, the crystal should have 6pf capacitance.
Secondly the legs of the crystal should be soldered as close as it gets to the chip.
And finally around the crystal should exist on the pcb ground plane, so parasitic noises won't interfere on RTC's accuracy.
In addition to that, i have seen in some cases, that the crystal's shell is touched with a wire soldered on the ground plane of the pcb, as a result once again
parasitic noises won't interfere on RTC's accuracy.

Image not found...
(Fig2) The schematic of the RTC module.


This module although it has ground plane on the pcb, the crystal has different capacitance than the recommended, and it is not soldered as close as it gets to chip's pins.
This results to capacitance increnment, and the faulty measurement of the RTC. So beware when you are using the particular module and accuracy is required.
Below you can see the considarations we talked about for the crystal, taken from the datasheet of the chip which are the proper selection, and the placing on the pcb.


Image not found...
(Fig3) The crystal recommendations taken from datasheet.


Materials

Resistors
R1 10KOhm Smd (0805)
R2 10KOhm Smd (0805)
R3 10KOhm Smd (0805)
IC
IC1 DS1302 Trickle-Charge Timekeeping Chip
Crystal
X1 Quartz crystal 32.768Khz
Misc
G1 Battery holder for C2032 battery

Pinout Description


Image not found...
(Fig4) The pinout of DS1302.
(Click to enlarge)

Now lets make a small analysis of the DS1302 pins.

Pin#
Name
Function
1
(VCC2)
This is the primary power supply pin.
The DS1302 operates from VCC1 or VCC2. (VCC1 is connected to a backup source like battery.)
When VCC2 is greater than VCC1 + 0.2V, => VCC2 powers the DS1302.
When VCC2 is less than VCC1, => VCC1 powers the DS1302.
2-3
(X1-2)
In these pins you connect a standard 32.768kHz Quartz Crystal.
The DS1302 can be also driven by an external 32.768kHz oscillator.
In this configuration, X1 pin is connected to the external oscillator signal and the X2 pin is floated.
4
(GND)
This is the Ground pin of the chip.
5
(CE)
This pin is the common chip select pin. This pin must be asserted high during a read or a write.
Also an internal 40kΩ pulldown resistor is connected to ground.
Note: Previous datasheet revisions refer to CE as RST.
The functionality of the pin has not changed.
6
(I/O)
This is the I/O pin for the 3-wire serial communication.
This pin is characterized as Input/Push-Pull output, and it has an internal 40kΩ pulldown resistor to ground.
7
(SCLK)
This is the clock pin SCLK where it synchronizes data movement on the serial interface.
This pin as an input it has also an internal 40kΩ pulldown resistor to ground.
8
(VCC1)
VCC1 is the backup source pin. This pin is connected to a backup source (like battery), to maintain
time and date in the absence of primary power.
In systems using the trickle charger, the rechargeable energy source is connected to this pin.
UL recognized, to ensure against reverse charging current when used with a lithium battery


DS1302 Communication


Before we step into the serial transmittion procedure, lets analyze the command byte and some registers that we will talk to...
Below is the command byte that we send to the DS1302 in order to command it either to write, or read from it.


Image not found...
(Fig5) The command byte. (Click to enlarge)


Bit7 Must be always logic 1. Otherwise writing to the DS1302 will be disabled.
Bit6 Specifies the clock/calendar or RAM selection. if it is logic0 we talk to the clock or if it is logic1 we talk to the RAM.
Bits 5 to 1 Specify the designated registers to be input or output. (Addresses)
Bit0 Specifies a write or read operation on the DS1302. A write operation is perfomed if it is logic0 or read operation if it is logic1.
The command byte is always starting with the LSB first (Bit0).




Now lets analyze the Data Transfer procedure.

Image not found...
(Fig6) Data transfer procedure. (Click to enlarge)



As it is obvious before you start transmitting anything, you must pull high the CE pin so you can talk to the chip. (Selection)
After that you send the command byte, to inform the DS1302 of the operation that will follow. (Whether it is read or write, to the clock or RAM.)
And then you send the data to be written in the RAM or the clock if you perform a write operation, or if you perform a read operation you can
read the data from the clock or the RAM.
Now if you read data from the DS1302 data will be available on the first falling edge after the last bit of the command byte is written.
In contrary writing to DS1302 happens on low to high transition right after the command byte transmition.
Four notes to be remembered, is that all transmitting and receiving bytes are LSB priority first,
Additional SCLK pulses that are sent to DS1302 on write operation right after the command byte and the data byte will be ignored.
Additional SCLK pulses that are sent right after the read operation, data will be once again available for reading.
And finally microcontroller's clock should be at 2MHZ max speed when communicating with the DS1302 as the datasheet dictates below (Fig7).
If you are using a different speed then you must calibrate the frequency to meet DS1302 needs and the recalibrate it to the previous frequency.


Image not found...
(Fig7) Maximum clock frequency. (Click to enlarge)




Register Addresses - Setting up


Having said about communication procedure, let's have a look at the registers that DS1302 has.

Image not found...
(Fig8) Register addresses. (Click to enlarge)


From the left, the first two columns, refer to the command byte. Whether you want to read or write to the RTC or RAM.
Columns from (bit7 to bit0) refer to the data byte that it will be transmitted either in RTC or the RAM.

Here is an example... Lets say that we want to write to the RTC the hour.
1). Pull high CE.
2). Send the command byte the 84h value. (Followed by clock pulses SCLK)
3). Send the data byte loaded with the 10h value**. (Followed by clock pulses SCLK)
4). Pull CE low.
This will result to write the hour on the DS1302, which is the 16 O'clock (In 24hour format).

** As we said earlier the DS1302 can be run in either 12-hour or 24-hour mode.
Bit 7 of the data byte defines the 12 or 24-hour mode-select bit. When it is high, the 12-hour mode is selected.
Bit 6 will be always zero.
Bit 5 In the 12-hour mode, is the AM/PM bit with logic being high on PM.
Bit 5 In the 24-hour mode, is the second 10-hour bit (20–23 hours).
The hours data must be re-initialized whenever the 12/24 bit is changed.


Here is another example....But this time we will write a value to the RAM at the 3rd position which is the C4h.
1). Pull high CE.
2). Send the command byte the C4h value. (Followed by clock pulses SCLK)
3). Send the data byte loaded between 0h to 255h. (the value you want to be written) (Followed by clock pulses SCLK)
4). Pull CE low.
And that was it!.So simple!.

Below there are some important things that you must always remember considering the RTC.
When you are reading or writing time or date registers, secondary buffers are used to prevent errors when main registers update.
When reading time or date registers, the secondary buffers are synchronized to main registers on the rising edge of CE.
When writing time or date registers, write transfers occurs on the falling edge of CE.
The countdown chain is reset whenever the seconds register is written.
To avoid rollover issues, once the countdown chain is reset, the remaining time and date registers must be written within 1 second.
Finally the day-of-week register increments at midnight. Values that correspond to the day of week are user-defined but must be sequential.
For example: 1=Sunday, 2=Monday, 3 =Tuesday and so on. Illogical time or date entries will result in undefined operation.

Clock halt flag bit

The seconds register has a bit defined as clock halt (CH). This is the Bit7.
When Bit7 is set to logic 1, the clock oscillator halts, and the DS1302 is placed into a low-power standby mode with a current drain less than 100nA.
When Bit7 has logic 0, the clock will start.
The initial power-on state is not defined.

Write-protect bit

The control register has a bit defined as write-protect bit (WP). This is also the Bit7.
The first seven bits (Bits0-6) are forced to logic0 and when you read them, you will always read logic0.
Before any write operation to the clock or RAM, Bit7 must be logic0.
When this Bit is high, the WP bit prevents a write operation to any registers.
The initial power-on state is not defined.
Therefore, WP bit should be cleared before attempting to write to the device.

Trickle-Charge register

This register controls the trickle-charge options of the DS1302.
We must mention that when DS1302 powers up, the trickle charger is disabled.
The trickle-charge select (TCS bits 4 to 7) control the selection of the trickle charger.
To prevent accidental enabling, only a pattern of 1010 enables the trickle charger.
The diode select (DS bit2 and bit3), select whether one or two diodes are connected between VCC2 and VCC1.
If DS is 01 (one diode is selected.)
If DS is 10 (two diodes are selected.)
If DS is 00 or 11, the trickle charger is disabled independently of TCS.
The (RS bit0 and bit1) select the resistor(s) that connect between VCC2 and VCC1.
The resistor and diodes are selected by the RS and DS bits as shown in the Fig9 below.

Image not found...
(Fig9) Trickle Charger Resistor and Diode Select (Click to enlarge)

Diode and resistor selection is determined by user, according to the maximum current that it is desired for re-chargable battery or super-cap.
The maximum charging current can be calculated as it follows....
Assume that a system power supply of 5V is applied to VCC2 and a super cap is connected to VCC1.
Also assume that the trickle charger has been enabled with one diode and resistor R1 between VCC2 and VCC1.
The maximum current IMAX would therefore be:

IMAX = (5.0V – diode drop) / R1 ≈ (5.0V – 0.7V) / 2kΩ ≈ 2.2mA

As the super-cap charges, the voltage drop between VCC2 and VCC1 decreases and therefore the charge current decreases.
Finally i have attached below the simplified schematic, to be easily understood, of how the trickle charge register enables resistors or diodes accordinally.

Image not found...
(Fig10) Programmable Trickle Charger (Click to enlarge)



Video



COMING SOON.....

Follow me on tweeter and stay tuned with Electronicsgate.com