DS18B20 Library Unit

This is an entirely new library unit for taming the DS18B20 digital temperature sensor. It covers all the possible commands and can be used with single-drop and multi-drop buses, and in powered or parasitic modes.

Overview


Note that his library unit depends upon the OneWire_Alt library unit, which is called automatically during compilation.

I've provided eleven commands for the device. The commands fall into two categories. The diagnostic commands are used to check things out during the learning or debugging phase, while the high-level commands are intended for a finished project.

Don't forget that the commands from the OneWire_Alt library unit are inherited and hence available here too. The most useful would be the five search commands.

This library may be used in a single-drop or multi-drop environment, which is established by the absence or presence of the conditional compiler constant MULTIDROP. Note that various function or procedure parameters may differ depending upon this setting. (Single-drop procedures don't require a serial number address since only one device is on the bus.)

Moreover, both standard and parasitic powering are supported. In the latter case, a hard pull-up transistor is controlled by an additional port line when required.

Compilation Constants


Seven conditional compiler constants are recognized:

OW_PORT       -- port for the 1-Wire data line
OW_PIN        -- pin number for the above
OW_CTRL_PORT  -- port for the hard pull-up transistor
OW_CTRL_PIN   -- pin number for the above
MULTIDROP     -- more than one device on the bus (default is single-drop)
DS_PARASITIC  -- parasitic powering (default is standard power supply)
DS_RESOLUTION -- temperature resolution (value = 9, 10, 11, 12);                (default is 12-bits)

Global Constants and Variables


This unit, and the one it depends upon, provide a number of global constants relating to bus timing and device commands should you need them.

Also coming along are the port pin variables for the bus and the hard pull-up transistor (if used). These are established by the conditional constant defines mentioned above. Their associated TRIS bits are automatically taken care of.

There are two additional global variables used to minimize lots of parameter passing. OW_Buffer[] is an array which may hold either the eight bytes of a device serial number, or the nine bytes of scratchpad memory in the DS18B20. It is also used by the search commands defined in the OneWire_alt library unit. Finally, OW_CRC is the variable which accumulates the CRC check byte for either ROM or scratchpad.

In your main program code, if using a multi-drop bus you should define the constant array DS_IDs which contains the serial number IDs (8 bytes each, ordered low to high) of the devices connected to the bus. For example, in the main code, you might have something like this for two sensors:

DS_IDs : array[16] of byte = 
         ($28, $75, $98, $02, $06, $00, $00, $32,
          $28, $00, $EA, $00, $06, $00, $00, $A8);

The multi-drop commands require that you pass in this array, along with the device number (0, 1, 2,...) of the sensor you wish to apply the command to.

An output parameter returns a message code in general:

0 = okay, command executed properly
1 = bus not responding or no devices
2 = device number out of range
3 = CRC failed
4 = illegal resolution
5 = external power detected
6 = parasitic power detected

If using parasitic power, then the temperature conversion time is fixed depending upon the resolution you have chosen. With an external power supply, the conversion time is variable and is determined by the device itself; this will in general be more efficient.

Commands


Some commands are only available in one mode only. Here are the ones you can use with a single-drop bus.

procedure readPowerSupply(var message : byte);
This command is available on either a single- or multi-drop bus  message: 1 = bus not alive, 5 = external power, 6 = parasitic power

procedure convertAll(var message : byte);
Force all sensor to carry out their conversions. This may be used on a single-drop or multi-drop bus.

procedure getDS_Temp(var Celsius : word; var message : byte);
Return temperature reading. message : 0 = okay, 1 = bus not responding, 2 = device number out of range, 3 = CRC failed

procedure getDS_ID(var message : byte);
The 8-byte serial number is left in global array OW_Buffer[], and computed CRC value is left in global variable OW_CRC.

procedure ScratchToEeprom(var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure writeScratch(var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure EepromToScratch(var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure readScratch(var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure setResolution(resolution : byte; var message : byte);
message : 0 = okay, 1 = bus not responding, 5 = illegal resolution

procedure setAlarms(lowTempAlarm, highTempAlarm : byte;  var message : byte);
message : 0 = okay, 1 = bus not responding or no device

The following commands are available for use with a multi-drop bus:

procedure readPowerSupply(var message : byte);
This command is available on either a single- or multi-drop bus message: 1 = bus not alive, 5 = external power, 6 = parasitic power

procedure convertAll(var message : byte);
Force all sensor to carry out their conversions. This may be used on a single-drop or multi-drop bus.

procedure getDS_Temp(const romable DS_IDs : array of byte;                  deviceNum : byte; var Celsius : word; var message : byte);
Address a particular device (1, 2, 3,...), and return temperature reading.  message : 0 = okay, 1 = bus not responding, 2 = device number out of range, 3 = CRC failed

procedure ScratchToEeprom(const romable DS_IDs : array of byte;                deviceNum : byte; var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure writeScratch(const romable DS_IDs : array of byte;                       deviceNum : byte;  var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure EepromToScratch(const romable DS_IDs : array of byte;           deviceNum : byte; var message : byte);
message : 0 = okay, 1 = bus not responding or no device

procedure readScratch(const romable DS_IDs : array of byte;                    deviceNum : byte; var message : byte);
message : 0 = okay, 1 = bus not responding or no device 

procedure setResolution(const romable DS_IDs : array of byte;                        deviceNum : byte; resolution : byte; var message : byte);
message : 0 = okay, 1 = bus not responding, 5 = illegal resolution

procedure setAlarms(const romable DS_IDs : array of byte;                  deviceNum : byte; lowTempAlarm, highTempAlarm : byte; var message : byte);
message : 0 = okay, 1 = bus not responding or no device

Add It to Your Library Collection


And here's the new library unit, "DS18B20.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.

No comments:

Post a Comment