DS1307 Realtime Clock Unit

This is a new library unit for the DS1307 realtime clock/calendar chip. There are many commands for setting it, reading it, as well as accessing the RAM within the part. It automatically calls upon the software I2C library unit.

Overview


Here is a quick command reference for your convenience, but be sure to see the details below. DOW is an acronym for day-of-the-week, and SQW for square wave.

DS1307_Enable(boolean)
DS1307_ResetClock
DS1307_SetClock(hour, minute, second, DOW, date, month, year)
DS1307_SetTime(hour, minute, second)
DS1307_SetDate(DOW, date, month, year)
DS1307_ReadClock(hour, minute, second, a.m.|p.m., DOW, date, month, year)
DS1307_ReadTime(hour, minute, second, a.m.|p.m.)
DS1307_ReadDate(DOW, date, month, year)
DS1307_SetMode(12|24, hour)
DS1307_ReadMode(12|24)
DS1307_SetSQW(rate)
DS1307_Write(address, value)
DS1307_Read(address), returns the value

Compilation Constants


There are no compilation constants required of this library unit proper. However, seeing as it calls upon the software I2C library unit, you'll need to define the following required of that one. For example,

{$DEFINE SDA_PORT 'PortA'}  // I2C SDA port
{$DEFINE SDA_PIN 2}         // and its pin number
{$DEFINE SCL_PORT 'PortA'}  // I2C SCL port
{$DEFINE SCL_PIN 3}         // and its pin number


Global Constant


There is only one global constant set by the unit, the I2C base address of the DS1307. This is determined by the manufacturer, and has the following value:

DS1307_ADDR = 0xD0;         // DS1307 I2C address

Commands


And here are fuller descriptions of the commands.

procedure DS1307_Enable(value : boolean);
Enables clock if value = TRUE, disables if value = FALSE.

procedure DS1307_ResetClock;
Reset clock to 00:00:00 01 01/01/00.
Also sets 24-hour mode and enables the clock.

procedure DS1307_SetClock(hour, minute, second, DOW,
                                                        date, month, year : byte);
Set entire clock: hours, minutes, seconds, day of week, date, month, year
Also sets 24-hour mode and enables the clock.

procedure DS1307_SetTime(hour, minute, second : byte);
Set time only: hours, minutes, seconds.
Also sets 24-hour mode and enables the clock.

procedure DS1307_SetDate(DOW, date, month, year : byte);
Set date only: day of week, date, month, year.
There is no error detection for out-of-range dates.

procedure DS1307_ReadClock(var hour, minute, second : byte;
                                                             var am_pm : boolean;
                                                             var DOW, date, month, year : byte);
Read entire clock: hours, minutes, seconds, a.m. or p.m., day of week,  date, month, year. am_pm = FALSE means a.m.; am_pm = TRUE means p.m.

procedure DS1307_ReadTime(var hour, minute, second : byte;
                                                           var am_pm : boolean);
Read time only: hours, minutes, seconds, a.m. or p.m.

procedure DS1307_ReadDate(var DOW, date, month, year : byte);
Get date only: day of week, date, month, year.

procedure DS1307_SetMode(newMode : byte; var hour : byte);
Set hour mode: 12 = 12-hour, 24 = 24-hour. Current hour may be changed.

procedure DS1307_ReadMode(var mode : byte);
Return the current hour mode: 12 = 12-hour, 24 = 24-hour.

procedure DS1307_SetSQW(value : byte);
Set square wave output pin mode.
value = 0:    disable
              4:     4.096 kHz
              8:     8.192 kHz
              32:   32.768 kHz
              else: 1 Hz }

procedure DS1307_Write(address, value : byte);
Write to the internal RAM. Use addresses 0x08 to 0x3F only, else there will be wraparound to the register space of 0x00 to 0x07. This is especially important when doing multibyte writes. But note that this procedure could also be used to write to clock registers 0 though 7 if that's really what you want, for low-level access.

function DS1307_Read(address : byte) : byte;
Read from the internal RAM. See the note, above.

Add It to Your Library Collection


You can fetch the DS1307 Realtime Clock library unit here:


Be sure to read over the source code for additional details on how to use it.

Try It Out


There are two exercises on this blog which can demonstrate this library unit. The first puts the clock/calendar through its paces, while the second reads from and writes to the DS1307 onboard RAM. You can go to both of these if you click here.

No comments:

Post a Comment