While it is possible to implement rudimentary timekeeping functions in code with PAUSE, problems
arise when BASIC Stamp needs to handle other activities. This is especially true when an application
needs to handle time, day and date. The cleanest solution is an external real-time clock. In this
experiment, well use the Dallas Semiconductor DS1302. Like the DS1620, the DS1302 requires only
three lines to communicate with the BASIC Stamp. Since these two devices are compatible with each
other, the clock and data lines to can be shared giving the BASIC Stamp real-time clock and
temperature measurement using only four I/O lines.

Once programmed the DS1302 runs by itself and accurately keeps track of seconds, minutes, hours
(with an AM/PM indicator, if running in 12-hour mode), date of month, month, day of week and year
with leap year compensation valid up to the year 2100. As a bonus, the DS1302 contains 31 bytes of
RAM that we can use as we please. And for projects that use mains power, the DS1302 also contains
a trickle-charging circuit that can charge a back-up battery.

The DS1302 is a register-based device, that is, each element of the time and date is stored in its own
register (memory address). For convenience, two modes of reading and writing are available: register
and burst. With register access, individual elements can be written or read. With burst access, all of
the registers can be set at once and any number (starting with seconds) can be read back.

In order to keep our interface with the DS1302 simple, this experiment uses it in the 24-hour mode.
In this mode, we dont have to fuss with the DS1302 AM/PM indicator bit. For a 12-hour display, well
deduce AM/PM mathematically. In the code, time is handled as a single, word-sized variable
(rawTime) that represents the number of minutes past midnight. This will make calculating durations
and comparing alarm times with the current time very straightforward.

Another compelling reason to use a raw time format is that the DS1302 stores its registers in BCD
(binary coded decimal). BCD is a method of storing a value between zero and 99 in a byte-sized
variable. The ones digit occupies the lower nibble, the tens digit the upper. Neither nibble of a BCD
byte is allowed to have a value greater than nine. Thankfully, the BASIC Stamp allows nibble-sized
variables and, more importantly, it allows variables to be aliased.

This experiment demonstrates the DS1302 basics by setting the clock, then polling it for updates.
Conversion to and from the DS1320 BCD register format is handled by the subroutines that set and
retrieve information in burst mode.

Four pushbuttons are used to set the day, hours and minutes of the clock. Normally, the buttons
cause each element to increment. By holding the fourth button, each element will roll back. When no
button is pressed, the program passes to a routine called Do_Some_Task. This is where you would
put additional code (reading a DS1620, for example).

Program output is sent to a DEBUG window. The Show_Time subroutine handles printing the day and
time in the format specified by ClockMode.
