Using a real time clock can be invaluable to a senior project. There are many applications where this device is important. If accurate time keeping is needed and power consumption is a concern, this is the only option. The Dallas 1302 real time clock is a good device because of its small size and extremely low power consumption.




Figure 1 shows a schematic of the DS1302 connected with a M68HC912B32 microcontroller. This clock features:

·        Real Time Clock (RTC) counts seconds, minutes, hours, date of month, day of week, and year with leap year correction to 2099

·        Three wire SPI interface

·        2.0V to 5.5V full operation

·        31 bytes battery backed RAM

·        Uses less than 300nA at 2.0V


Power Supply

Primary power for the DS1302 is supplied through Vcc2. Battery backup is connected to Vcc1. The DS1302 has an onboard comparator that detects which of Vcc1 or Vcc2 is greater, and runs off of the greater supply. When main power is removed the oscillator runs from the battery backup. When the DS1302 is supplied from Vcc1 the clock shuts down all circuitry except for the oscillator to conserve power. This means that the SPI will not work.



Figure 1: DS1302/SPI Connection Schematic

SPI Interface

To communicate with a microcontroller Dallas has used a Motorola SPI protocol. This three wire bus consists of: /Reset, IO, and SCK.




This pin acts as a chip select. It is an active low line. To write or read to the DS1302 this line must be asserted high and held high until the end of the read/write. When this line is low all reads and writes to the DS1302 are disabled.



This is a bidirectional data line. When /Reset is set high this pin and its associated registers are turned on. Data is serially shifted in and out in 8 bit words. A write or read cycle consists of sending a command byte, followed by a read or write byte. The command byte specifies the address to read or write.



This is the system synchronizing clock. The DS1302 data sheet specification is for a maximum clock rate of 2 MHz with a 5.0 V supply.  I have been unable to achieve these results. The maximum clock rate that I have been able to make work has been 500 kHz.



A crystal with a load capacitance of 6pF that oscillates at 32.768 kHz must be used with this clock. If a crystal with improper loading capacitance is used the clock will not keep accurate time. The crystal must be mounted as close to the X1 and X2 pins as possible, with no additional components connected to these pins. For additional information on crystals see Dallas App Note 58.


Motorola 68HC12

To get the data that the DS1302 keeps track of, a microcontroller is required. Figure 1, shows the connection between the clock and 68HC12 microcontroller. The external pullup resistors MUST be used. The internal pullup resistors on PORTS do not work with a bidirectional data line.


SPI Configuration

To successfully communicate with the DS1302 PORTS, the SPI port must be configured properly. The port configuration c-code is shown below.



DDRS |= (SS|SCK|MOMI); /* sets bit 7,6,5 of DDRS as outputs */

SP0BR = 0x03;            /* set bit rate to 500 kHz */

SP0CR1 = 0x71;          /* Enable,Mstr,WireOR,CPOL:CPHA=00,LSB First*/

SP0CR2 = 0x01;          /* Bidirectional SPI (M0MI) */


1.      The SS line is set low to ensure that there is no accidental read or write to the clock.

2.      The ports data direction register sets bit 7, 6 and 5 as outputs.

3.      SP0BR is the SCK baud rate generation register, this is set at 500 kHz.

4.      SP0CR1 sets the 68HC12 as the master device, data transmission in wired or mode (bidirectional), the clock phase and polarity bits are 0, and data is transmitted least significant byte first.

5.      SP0CR2 sets PS5 as MOMI for bidirectional data transmission.



Software for using the DS1302 with the 68HC12 has been written by Todd Morton. This code can be found on the Y:\ drive of the Ridge computer network. It is located at Y:\CODE401\WWU\emodules\SPI\DS1302RTC. This directory has all source code and needed to use the DS1302. Included in this directory are the following modules:

·        DS1302Spi.c

·        LCD.c

·        SpiWatch.c


The DS1302Spi.c module has functions to write and read to the DS1302. It also has a SPI initialization function with the code shown on the previous page.


The LCD module contains all functions necessary to display information on a 2x16 LCD.


SpiWatch.c is a sample program that displays all the register contents of the DS1302 onto a 2x16 LCD.



Common Problems

CH bit

The DS1302 has a clock halt bit at bit 7 of the seconds register. When this bit is set, it turns all oscillator circuitry off to conserve power. Since this bit is in RAM its value is undetermined until written to. This bit must be set to 0. This must be done any time that all power has been removed. As long is there is power to either Vcc1 or Vcc2 all registers will be maintained so this bit should be cleared once and have a battery backup connected to Vcc1 at all times.


/Reset Line

The reset line must be held at logic 0 until Vcc is greater than 2.0V. Out of reset the SS port of the 68HC12 is an output at logic 1. When first powering up the clock and 68HC12, connect the /Reset pin to ground until everything has powered up. After power up, the /Reset line can be connected to the microcontroller. As long as there is a voltage greater than 2.0V on eitherVcc1 or Vcc2, the clock will operate normally. If for any reason all power is removed, this procedure must be repeated.


External Pullup Resistors

External pullup resistors must be used on the three wire bus, (see Figure 1). This is because the internal pullup resistors on the 68HC12 will not work with a bidirectional data line. Any time a pin becomes an output the internal pullups are disabled. This means the pullup on SCK, and SS will always be disabled, and MOMI will be disabled when writing to the clock. Using at least 10kΩ pullup resistors is absolutely necessary.


Additional Resources

Below is a list of all additional information on the 68HC12 SPI port and the DS1302.

All the App notes are published on the Dallas web site. The 68HC12 Data sheet is published on Motorola’s web site. Embedded Microcontrollers is a textbook that deals specifically with the 68HC12.

·        DS1302 Data Sheet

·        App Note 504: Design Considerations for Dallas Real-Time Clocks

·        App Note 58: Crystal Considerations with Dallas Real-Time Clocks (RTCs)

·        App Note 82: Using the Dallas Tricklecharge Timekeeper

·        App Note 505: Lithium Coin-Cell Batteries: Predicting an Application Lifetime

·        App Note 701: Using the DS32kHz with Dallas Real-Time Clocks

·        M68HC12 Data Sheet, rev 3,  pp 303 – 316

·        Embedded Microcontrollers, Todd D. Morton, pp 356 – 364