digit. The TinyRTC takes advantage of any bits not needed
for other purposes, or sometimes just locks them to zero.
For example, bit 7 of Register 0 is used to turn the clock
on or off. If high, then the clock is halted, but if cleared it
takes off running.
Register 2 can be used in two different ways as shown
in the figure. If its bit 6 is low, then time is represented in
24 hour mode (so-called military time). On the other
hand, if bit 6 is set, then time is stored in a 12 hour
format. In the latter case, bit 5 tells you if the hour is
before or after noon; it’s cleared for the former, or set for
The other time and date registers should be pretty
obvious, so let’s turn to Register 7 which controls the
output pin labeled SQW, standing for “square wave.” This
pin is brought out on the TinyRTC and can be used in a
couple interesting ways. As the figure indicates, if bit 4 is
set, then a square wave is generated on the SQW pin. The
bits labeled RS0 and RS1 let you set the frequency at 1
Hz, 4.096 kHz, 8.192 kHz, or 32.768 kHz.
The 1 Hz option is particularly attractive since it could
be employed to generate microcontroller interrupts once a
second; say, to take a new temperature reading, or just to
flash the colon between the hours and minutes of a
display. If bit 4 is cleared, then the output pin simply
follows the state of bit 7 which gives you software control
of SQW. Take a moment to look over the figure one last
time. While it might appear mysterious at first blush, you’ll
find yourself at home with it quite soon.
Bring On the Memory
As mentioned earlier, the TinyRTC gives you access to
56 bytes of general-purpose RAM within the DS1307
chip; you can use this any way you want. The addresses
follow immediately after the registers described previously.
So, RAM lives at locations 8 through 63.
We normally think of RAM as temporary memory.
Thanks to the backup battery, however, it now becomes
nonvolatile — at least as long as you keep the cell in place.
The AT24C32 IC provides 4096 bytes of genuine
nonvolatile EEPROM storage. This might prove useful for
such things as storing messages to be displayed on an
LCD at times directed by the clock (birthdays,
appointments, warnings, etc.) or perhaps for logging data
from outboard sensors (light, temperature, humidity, and
so forth). In any event, the RAM and EEPROM are there
for the taking, so feel free to drink deeply of them.
Both the DS1307 and AT24C32 chips on the TinyRTC
module are I2C devices. If you’ve played with this
synchronous communication scheme before, then you’ll
know that the two lines called SCL and SDA (clock and
data, respectively) must have pull-up resistors on them.
The TinyRTC includes these. Even though both chips are
hanging on the SCL and SDA lines, only one set of pull-ups is required.
I hope I haven’t frightened you with the mention of
I2C — a scheme that seems daunting at first. For, in fact,
the software library put together especially for this article
takes all of the wailing and gnashing of teeth out of it! A
handful of higher level commands is all you need to start
doing something useful at once. Let’s see what they look
like. The software to drive the TinyRTC has been written in
Great Cow Basic. This is an amazingly full-featured,
powerful, yet easy-to-use compiler. Best of all, it’s open
source and absolutely free of charge. You can download it
from gcbasic.sourceforge.net. You can fetch this article’s
source code from the article link.
Apart from the four demonstration programs that we’ll
get to in just a moment, there are two other files of note.
These are general-purpose include files which add I2C and
TinyRTC commands to the Great Cow Basic compiler.
They’re titled “I2C-Alt.h” and “TinyRTC.h,” respectively.
(Be sure to view the “ReadMeFirst.txt” file for any
updates). Actually, the I2C stuff will be invisible to you, and
is required only by the TinyRTC file. Of course, you’re
certainly welcome to dip into whatever’s there should you
wish to modify the TinyRTC commands or create new
ones which occur to you. That’s the beauty of open
So, what are these high level commands? Figure 4
lists them. These should all make a great deal of sense.
There are commands to set the time, read the date, set
the SQW output frequency, read and write to the
EEPROM, and so forth. To cut down on confusion in the
figure, the parameters aren’t shown. Instead, if you head
RTC_Enable() Turn clock on or off
RTC_ResetClock() Reset clock to manufacturer's default
RTC_SetClock() Set date, day of the week, time, and hour mode
RTC_Set Time() Set only the time
RTC_SetDate() Set only the date and day of the week
RTC_ReadClock() Read date, time, and am/pm flag
RTC_Read Time() Read only the time and am/pm flag
RTC_ReadDate() Read only the date and day of the week
RTC_SetHourMode() Choose 12 or 24 hour clock mode
RTC_SetSQW() Set SQW output frequency
RTC_Write() Write to clock register or RAM
RTC_Read() Read from clock register or RAM
eepByte W() Write a byte to the EEPROM
eepByteR() Read a byte from the EEPROM
eepArr W() Write a string or array to the EEPROM
eepArrR() Read a string or array from the EEPROM
50 April 2015