The four basic read, write, reset and CRC commands are derived from Philippe Paternotte's One-Wire library unit, but rewritten to use the labels from the chip data sheet.
I've added five new search commands, and two additional low-level commands. These are based upon the C language versions in Maxim Application Note 187. The original comments are retained here, with a few new ones thrown in.
All labels have been revised for consistency.
Three conditional compilation constants are recognized:
OW_PORT -- port for the 1-Wire data line, e.g., PortA, PortB, etc.
OW_PIN -- pin number for the above, e.g., 0, 1, 2,...
MULTIDROP -- more than one device on the bus (default is single-drop)
Commandsfunction OW_Reset : byte;
Reset bus, detect presence pulses from all devices.
Return 0 : okay, 1 : the devices failed to respond
procedure OW_WriteByte(data : byte);
Write a complete byte.
function OW_ReadBit : boolean;
Read a single bit.
function OW_ReadByte : byte;
Read an entire byte.
procedure OW_DoCRC(X : byte);
This procedure factors in bytes one after another to compute a final cyclic redundancy check number (also a byte). The result is accumulated in the global variable OW_CRC, which should be initialized to 0 when the procedure is first called.
function OW_FindFirst(command : byte) : boolean;
Use this command to initiate a search (first device). The command byte parameter specifies the type of search, e.g., SEARCH_ROM, ALARM_SEARCH, etc. The serial ID of the found device will be left in the OW_Buffer array. Return TRUE : device found, FALSE : device not found, end of search.
function OW_FindNext(command : byte) : boolean;
Use this command to continue a search for the next device. The command byte parameter specifies the type of search, e.g., SEARCH_ROM, ALARM_SEARCH, etc. The serial ID of the found device will be left in the OW_Buffer array. Return TRUE : device found FALSE : device not found, end of search.
procedure OW_FindOnly(family_code : byte);
This command restricts the search to devices of a particular family only. Call this procedure first, then OW_FindNext() as usual.
This command restricts the search to any devices NOT in a particular family. Call this procedure after OW_FindFirst() has found a member of the family you wish to skip.
function OW_Verify : boolean;
Verify the desired device is on the bus. Before calling, place the serial ID of the device to check on in the OW_Buffer array. Return TRUE : device verified present, FALSE : device not present
Add It to Your Library Collection
And here's the new library unit, "OneWire_alt.pas" for you to download:
Be sure to read over the source code for additional details on how to use it.
Try It Out
If you'd like to see it in operation, then head over to the exercises. There are three there using the DS18B20 which operates on the One-Wire bus. Click here to go to the first one in the series.