The Time according to AVR and Dallas Semiconductor
updated 8-04-99

    The Atmel AVR series of RISC processors and the Dallas Semiconductor DS1305 are close to the same age.   It's only
fitting that the two devices should be joined.  It's ironic that the AT90S2313 and the DS1305 are just about the same price,
around $3ea.  Illustrated here is the simplicity on how to program the DS1305 RTC by an AVR processor.  The DS1305
is rich in features such as a programmable battery charger (current limited), two independent 'time match' alarms, primary
power failure output, an oscillator circuit that apparently requires no caps, two separate methods for serial communications,
leap year correction and even some SRAM was thrown in.  The only thing missing is a 32bit counter for elapsed time.
Dallas Semi has left some 'reserved' space in the chip, and if everyone rubs their magic lamp, this feature could be added.  One of the alarms can be programmed to send an INT every second, and the AVR can increment a software counter.

    My needs are for autonomous data acquisition devices that rely on Solar panels to charge the batteries.  With the clever
alarm circuitry in the DS1305, an INT signal can be sent once every second or once every several days.  It can even be
configured to 'alarm' on a specific day/time of the week.  Each of the independent alarm outputs can be tied to one of the
AVR INT pins.
For my data logger, I'm using one of the Alarm outputs to control a transistor switch that supplies power to the
AVR & Max202 VCC supply.  Just putting the AVR to sleep is not enough power savings for this type of remote equipment
operation.  There will not be any 'elapsed' time loss, as the AVR software will know to add the sleep time to the counter.
This project will be using the 3-wire mode.  Pin 9 on the DS1305 is grounded to select this protocol.  Place Pin 9 to VCC
for the 4-wire SPI mode.  The AT90S2313 pins are to precious to throw away.  Besides, this uP does not have any hardware
SPI logic.  Programming the RTC data can be performed 1 byte at a time, or several bytes by 'burst' mode operation.  What
helps here is that only an 8bit starting address is required to read the entire DS1305 register contents.  The Read and Write
data modes are distinguished only by bit 7 in the start address.  After the AVR code sends the address, the DS1305 has auto
increment logic that internally advances the address pointer after every 8bits are clocked.  This is really going to help make the
AVR code size small and fast.  Here is some ASM 'burst mode' starter code. DS1305A.ASM and the JAVRBasic test
program DS1305.bas that sends the time to the host PC via RS232. The Basic test uses the AT90S8515 running at
about 7mhz. If you are using JAVRBasic, re-load the newer runtime, as there are some additions.   Single Register
Read/Write code. 1305RD_WR.asm   and the RTC DS1305.pdf data sheets.

DS1305 data sheets used with permission of Dallas Semiconductor Corp.  (Thanks Pat and Jim)
(special thanks to Yvon Hache)
Figure 2 displays the entire RTC address mapping, including the 96 byte SRAM locations.

ds1305r1.gif (4305 bytes)


Figure 3 below shows the DS1305 register map.  Notice that the Read/Write modes are distinguished only by BIT7,
and the lower 7 address bits are the same.
DS1305REG.GIF (10974 bytes)

Table 1 gives an illustration of the Alarm set function.
DS1305TAB1.GIF (21680 bytes)

Set the RTC Alarm match time in the same BCD format as the Time of Day registers.  Here is an AVR code sample for
setting the Alarm Match Register to generate an IRQ every day @ 09:05:30.

SetRTCAlarm:
    ldi    r30,0x89        ; remember to use BCD format (Hex digits work)
    sts   ahours,r30    ; Put alarm (09) hours in SRAM + BIT7=1
(see table 1 register mask)
    ldi    r30,0x05     
    sts   amins,r30     ; Put alarm (05) minutes in SRAM
    ldi    r30,0x30
    sts   asecs,r30     ; and the alarm (30) seconds in SRAM
    clr    r30
    sts   day,r30        ; day of week (1-7), we won't use it here
    ldi    r30,7             ; 0x07 for Alarm 0,   or 0x0b for Alarm 1
    ldi    r24,4             ; 4 bytes to 'burst write'
    rjmp BurstWriteAlarm  ; in the DS1305a.asm.
   
Dallas Semi gives a good explanation of the RTC Control register below. 
The DS1305.bas module _wrclk
performs 'non-burst-mode', single RTC Register Write functions.   Be sure that BIT7 in the Control Register is set.  If clear,
the RTC stops!  Also note that BIT6 MUST be Cleared before any Time-of-Day and SRAM Memory can be
written to (Write Protect Bit).
DS1305T2.GIF (23477 bytes)


Here are the JAVRBasic and Asm source samples for the complete DS1305 RTC w/Alarms.  This example prompts
the user for the current time & date. The alarm is set to send an IRQ each minute and is displayed on the TTY screen.
Updated 8-04-99 for proper parameter passing.
DS1305.bas
DS1305.asm

For those who need an accurate Elapsed timer with built in crystal and sealed battery, the DS1603 is an excellent choice.
Two separate 32bit counters only enhance the viability of this unique RTC.   The primary counter advances 1 count
every second regardless of the external power (internal battery). There is a secondary counter that only gets incremented
every second while the external 5v is applied.  Even with external power loss, this 32 bit counter's contents are preserved.
This is perfect for a runtime meter project.  This is a small piece of code to get started on. Will document it more later. This
RTC is shipped with the internal battery dis-connected, and the code to 'FUSE' the link is provided.  This procedure
must be performed one time, prior to RTC usage.
DS1603.asm

jackt@igalaxy.net