April
1999, Issue 105
Dallas
1-Wire Devices, (Part 1):
One
on One
ONE
ON ONE
Each
device has a unique serial number associated with it,
but when only one device is connected on the 1-wire
bus, communication is simplified. The SKIP_ROM command
(CCH) is the only command necessary to activate the
device. SKIP_ROM commands the attached device to immediately
wake up and take notice.
Its
not necessary to know the ID number of the device. If
you dont know the family of the device you're
talking to (it may be any touch device), you can issue
READ_ROM and the attached device will respond with its
family code, ID number, and cyclic redundancy check
(CRC) byte. From the family code byte you can determine
what kind of device is connected.
The
8-byte ROM code contains the family code byte, 6 bytes
of ID code, and a CRC byte. The CRC byte is based on
the previous 7 bytes and can be used to authenticate
the validity of the 8-byte transmission. Although a
wrong CRC byte might be because of a bad data transfer,
it could also mean that multiple devices are talking
at the same time.
This
time around, Im limiting the discussion to single
devices tied to an I/O pin. Figure
2 shows how the CRC byte is calculated. Each bit
shifted into the CRC byte is XORd with bits 4,
5, and 8 of the CRC before shifting.
Although
six assembly-language routines are shown in Listing 1reset, write-1, write-0,
read, write-byte, and read-byteonly the first
four of these routines are necessary. With these four
routines, you can make contact with almost any 1-wire
device.
My
first program was written as a generic program enabling
you to experiment with various 1-wire devices. It was
written for the PicStic using PicBasic but it can be
used with almost any PIC microprocessor.
I
like to use the PicStic because it contains all the
essentials you need every time you build a PIC circuita
5-V regulator, crystal, and supporting capacitors. And
because the PicStic is electrically reprogrammable (it
uses a 16F84), my debugging time is reduced. PicBasic
lets me embed assembly language where necessary and
still use the higher level commands to quickly build
the framework.
Youll
also need to know the specifics for each 1-wire device
(i.e., which commands to use for which devices and how
many bytes a command will return, if any). Refer to
the individual 1-wire datasheets for this information.
After
wiring up the circuit in Figure 3, programming the PicStic, and applying power, you
are presented with three choicesreset the 1-wire
bus, send a command, and request data.
Commands
are broken down into function and memory commands. A
reset is required before any function command, and a
memory command may follow a function command. The function
and memory commands are write only, but either command
may offer a response.
Lets
try using the program on a DS1820 connected to the 1-wire
bus. Following these steps, we receive the responses
displayed in Figure 4.
RESET <presence>
Command (hex)?
CC (SKIP_ROM)
Response (bits-dec)?
0
Command (hex)?
BE (READ_SCRATCHPAD)
Response (bits-dec)?
72
<temp> <pos/neg>
<user1> <user2> <res1> <res2>
<cr> <cpc> <crc>
48 0 0 0 255 255
41 81 51
Figure 4My
first program was flexible enough to enable me
to enter any of the 1-wire command bytes and receive
responses from any of the devices connected. Here
you can a SKIP_ROM command with no response and
a READ_SCRATCHPAD command with a 9-byte (72-bit)
response.
|
If
the devices VCC lead is connected to ground (true
1-wire connection) you must not use the 1-wire bus for
500 ms after sending a convert temp command (44h). Because
the device uses parasitic power in this configuration,
it must see a high for this duration.
After
that, you can proceed with reading the conversion. However,
if you connect its VCC lead to VCC, you can poll the
device for end of conversion status with Response (bits-dec)?
8 <busy>.
The
response to READ_SCRATCHPAD has the temperature in the
first byte. If you know its above 0°, you can
stop reading after the first byte.
The
conversation the master has with a 1-wire device can
be terminated at any time. The temperature reply is
in 0.5°C increments. To get a true temperature in Celsius
degrees, divide this by 2 (ditch the 0.5 bit).
To
convert this temperature to Fahrenheit, perform the
conversion calculation:

or,
in our case (because Celsius is in 0.5° increments):

This
circuitry and software can easily be massaged into displaying
the present temperature in both Celsius and Fahrenheit
with a bit more software.
Using
a serial LCD can eliminate the serial connection to
your PC that would be used in the experimental and debugging
process. Listing 2 shows you what my BASIC program
looks like (minus the same assembly-language routines
that are used in Listing 1).
Next
month, Im going to look at the 1-wire bus with
multiple sensors. Ill show you how to get these
devices to act civilly with one another.
You
might want to check out Dallass web site for documentation
on 1-wire devices. They have serial and parallel port
dongles for reading external touch memory devices.
Software
for reading these on the PC is also available, if youre
into that. I prefer to provide my micros with the ability
to use these devices directly. I hope thats your
direction, too.
Jeff
Bachiochi (pronounced"BAH-key-AH-key") is
an electrical engineer on Circuit Cellar INKs
engineering staff. His background includes product design
and manufacturing. He may be reached at jeff.bachiochi@circuitcellar.com.