void showDateTime(struct rtc_t *myRtc)
printf(“Month: %s\nDay: %s\nYear: %d\n\n”,
Take a moment to review that code and think about
the two methods for accessing the data in a structure.
When you are dealing directly with the structure, you use
the dot ‘.’ operator. When you are dealing with a pointer
to the structure, you use the arrow ‘->’ operator. When
you are dealing with structures within structures as shown
in our example, you use the ‘->’ for the outer structure
which was passed as a pointer and the ‘.’ for the inner
structures that got passed along with it. You have to use
this a bit to get comfortable with it.
The PC has a real time clock (RTC) that has its own
battery so that it can keep running — even when the PC
is unplugged. With microcontrollers, we have to add
hardware to make an RTC. In the AVR Butterfly, a 32.768
kHz watch crystal is attached to an internal AVR
timer/counter peripheral so that an interrupt can be
generated every second to keep count of seconds.
Another way to do this is to have an external IC to keep
time for us; we can then ask that device for the time. One
common device used for this is the DS1307 that requires
one of those 32.768 kHz crystals. It can communicate
with a microcontroller via an I2C bus. This month, we will
build an RTC on a breadboard for use with an Arduino
that conveniently has an RTC library already written for us.
We will then add a PCB to this kit (DS1307RTC kit) that
you can purchase from the Nuts & Volts Webstore. In a
later Workshop, we will take a deeper look at computer
time-keeping that will lead us to writing a C based time-keeping library and to learning about the AVR
How the DS1307 Stores
Date and Time Data
The DS1307 has 64 registers in SRAM — the first eight
bytes are used to store the time data; the next 56 are
available for your use (more on that later). Take a look at
the first byte 00h that is used to store seconds. You see
that the four lower bits store seconds, while the next
higher three bits store ‘ 10 seconds.’ You might wonder
what that is all about since you can, in fact, store all the
SMILEY’S WORKSHOP ☺
■ FIGURE 8. Arduino select RTCLib DS1307 sketch.
July 2012 59
■ FIGURE 9. Arduino DS1307 sketch.