This is only a preview of the February 2004 issue of Silicon Chip. You can view 35 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Simple Supply Rail Monitor For PCs":
Items relevant to "Studio 350 Power Amplifier Module; Pt.2":
Items relevant to "Using The Valve Preamp In A Hifi System":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
PICAXE-18X
4-channel
datalogger
Pt.2: adding a real-time clock
Last month, we examined the datalogger circuit, described how to
build the basic module and detailed a basic datalogging mission
using the light and temperature sensors. This month, we add a
real-time clock to the datalogger and show you how to use it.
By CLIVE SEAGER
U
SING THE BASIC datalogger hardware, logging
can be carried out at regularly spaced intervals
(up to several hours) using simple programmed time
delays. For example, the datalogger program presented
last month used the “Pause” instruction to generate a
60,000ms (1 minute) sampling interval.
However, time delays generated in this way are not
entirely accurate, due mainly to manufacturing tolerances within the PICAXE micro’s internal resonator. This
becomes particularly evident when longer sampling
periods are called for. To avoid this problem, you can
add a real-time clock (RTC) IC to the basic datalogger
module.
DS1307 Real-Time Clock chip
The Maxim/Dallas Semiconductor DS1307 is an accurate real-time clock in an 8-pin DIP package. It automatically maintains the current time and date, including
corrections for leap years and months with less than
31 days. A standard low-cost 32.768kHz watch crystal
connects between pins 1 & 2 to provide an accurate time
base.
An optional 3V lithium coin cell can also
be connected to pin 3, ensuring that the device keeps functioning even when main circuit power is removed. The IC automatically
detects removal of the main power source and
switches to the lithium cell when required.
Less than 1µA of current is consumed by the
chip in this mode, meaning a cell life of 10
years or more.
The DS1307 has two additional features
of interest. Pin 7 is an open collector output
that can be programmed to flash at a 1Hz
rate. This allows an LED to be attached as a
seconds indicator in clock applications. The
IC also contains 56 bytes of general-purpose
RAM, which can be used as extra memory by
the PICAXE micro.
Fig.1: this diagram shows how the DS1307 real-time clock chip
is connected to the PICAXE-18X 4-Channel Datalogger circuit.
www.siliconchip.com.au
Installing the RTC
Installation of the RTC upgrade on the
datalogger PC board is very straightforward
February 2004 73
Table 1: DS1307 Registers
time/date data is in BCD (binary-coded decimal) format,
which makes it very easy to interpret using normal hex
notation. For example, 11:35am will contain $11 in the
hours register and $35 in the minutes register.
Address
Register (all BCD)
00
Seconds (00-59)
01
Minutes (00-59)
Setting the Time/Date
02
Hours (00-23)
03
Day of Week (01-07)
04
Date (01-31)
05
Month (01-12)
06
Year (00-99)
To initialise the chip after the circuit is first powered
up, the current time must be written to the registers.
The example program that follows sets the time to
11:59:00 on Thursday 25/12/03 using the “writei2c”
command. This is carried out by loading the registers in
order from address 00 upwards (seconds then minutes,
hours, etc.)
07
Control (set to $10)
08-$3F
General-purpose RAM
and should take you less than a minute!
First, insert the DS1307 IC into the empty 8-pin
IC socket (IC2), making sure that you have the pin 1
(notched) end oriented correctly. Next, slip the CR2032
lithium cell into its holder with the positive (+) side
facing up. That’s it! Note however that the chip will not
operate until the current time/date is set.
I2C Slave Parameters
The following I2C slave details can be found in the
DS1307 datasheet (available from www.maxim-ic.com):
slave address
address size
bus speed
1101000x
1 byte
100kHz
This means that the PICAXE i2cslave command is as
follows (see last months article for further explanation):
i2cslave %11010000, i2cslow, i2cbyte
The DS1307 registers are defined in Table 1. All the
74 Silicon Chip
i2cslave %11010000, i2cslow, i2cbyte
writei2c 0, ($00, $59, $11, $03, $25, $12, $03, $10)
end
After this program is downloaded, the green LED
(LED1) should flash once every second.
Using the DS1307
Reading the time and date from the DS1307 is best
shown by example. The program given in Fig.5 acts as
an “alarm clock” datalogger, checking the time every 23
seconds. When the time is exactly 07:00, the temperature and light sensors are read and stored in EEPROM.
The program runs for 30 days, after which the red LED
comes on to show that the mission is complete.
Datalogger Wizard
Of course, this is a relatively simple example. ProFig.2: the pinouts for the
DS1307 real-time clock chip.
A 3V lithium cell connected
to pin 3 will keep the device
functioning even when main
circuit power is removed.
www.siliconchip.com.au
Fig.5: Datalogger Program
Fig.3: installing the RTC upgrade on the Datalogger PC
board is easy – just plug DS1307 IC into the empty 8-pin
IC socket (IC2) and slip the CR2032 lithium cell into its
holder.
grams that make full use of the datalogger’s resources
are considerably more complicated. However, the
PICAXE Programming Editor software includes a “Wizard” which will automatically generate the more complex BASIC code for you with just a few mouse clicks!
To use the Wizard:
(1). Start the Programming Editor software (v3.5.1 or
later).
(2). Select View -> Options and choose PICAXE-18X
mode. Click OK.
(3). Select PICAXE -> Wizards -> AXE110 Datalogger ->
Start New Datalogger Mission.
(4). Chose the desired options and click on the “OK”
button.
The Datalogger Wizard dialog is shown in Fig.4. Most
of the Wizard’s options are self-explanatory and as you
can see, two timing options make use of the DS1307
clock upgrade. One sets an accurate timing interval
while the other sets an alarm clock style time/date for
logging.
Once the “OK” button is clicked, the Wizard gener-
main:
high 5
let b13 = 0
‘write protect EEPROM
‘reset address counter
loop:
i2cslave %11010000, i2cslow, i2cbyte
sleep 10
readi2c 0, (b0, b1, b2)
if b2 <> $07 then loop
if b1 <> $00 then loop
‘set DS1307 slave
‘wait 23 sec
‘read sec, min, hour
‘if hour not 07 loop
‘if min not 00 loop
high 3
low 5
‘LED green
‘write enable
readadc 0,b3
i2cslave %10100000, i2cfast, i2cbyte
writei2c b13,(b3)
pause 10
‘read light value from 0
‘set block 0 parameters
‘write the value
‘wait EEPROM write time
readtemp 7,b4
i2cslave %10100110, i2cfast, i2cbyte
writei2c b13,(b4)
pause 10
‘read temp value from 7
‘set block 4 parameters
‘write the value
‘wait EEPROM write time
high 5
low 3
‘write protect EEPROM
‘LED off
pause 60000
‘wait 1 minute
let b13 = b13 + 1
if b13 > 30 then stop
goto loop
‘increment address
‘30 days up?
‘no so loop
stop:
high 2
goto stop
‘LED red
‘loop forever
ates a BASIC program to perform a complete mission
based on the options you have selected. This is displayed on-screen and can be edited just like any other
BASIC program if desired.
Note that once a datalogger program is downloaded,
the mission starts immediately. It’s not possible to
restart a mission by pressing the RESET button. Instead,
you must download the program again.
Summary
The DS1307 is an easy-to-use real-time clock that
can be used to add long-term accuracy to your datalogging missions. It is a low-cost versatile addition to your
PICAXE datalogger system.
The DS1307 real-time clock and CR2032 lithium
cell can be purchased together (Part No. AXE034) from
www.microzed.com.au. The full datalogger kit is available as Part No. AXE110.
Next month: expanding your datalogger’s memory,
displaying information on an LCD and adding a humidSC
ity sensor.
About the Author
Fig.4: the Datalogger Wizard does all the hard work for
you – just select the options you want and click the OK
button to load the code.
www.siliconchip.com.au
Clive Seager is the Technical Director of Revolution
Education Ltd, the developers of the PICAXE system.
February 2004 75
|