Various sensors manufactured by Embedded Data Systems contain DS18B20 sensors by Maxim Integrated. This knowledge base article discusses how to work with the DS18B20 sensor using the HA7E and HA7S 1-Wire bus master.
The general process for interacting with the DS18B20 temperature sensor is as follows:
- Discover the device
- Set the desired temperature conversion resolution (9 - 12 bits)
- Initiate the temperature conversion
- Wait for the conversion to complete
- Read the results
- Parse the results
The following is an annotated example of the specific commands to send and typical responses received from the HA7 to accomplish the steps above. In the examples below, TX= data transmitted to the HA7E, and RX= data received from the HA7. The manufacturer's datasheet on the DS18B20 can be downloaded from https://www.embeddeddatasystems.com/assets/images/DS18B20.pdf.
Discover the Device
In this example, the DS18B20 is the only device on the 1-Wire bus so we simply use the 'S'earch Rom command to identify the first device:
TX- S | Performs a standard device search. |
RX- CD00000027C5CD28 | The ROM Id of the DS18B20. |
Set Desired Temperature Conversion Resolution
The DS18B20 supports anywhere from 9 to 12 bits of resolution during the conversion process. The time required to complete the conversion varies depending on the desired resolution. The conversion resolution is stored in the 'Scratchpad' memory of the DS18B20. This example starts by reading the existing scratchpad, then alters its contents, and writes the new scratchpad back to the DS18B20. By reading the existing scratchpad from the device, other parameters that are also stored in the scratchpad are preserved (i.e.alarm thresholds, etc.) For more details on the DS18B20 scratchpad memory, please reference the DS18B20 datasheet..
DS18B20- Reading scratchpad from physical device.
TX- ACD00000027C5CD28 | Addressing Device having ROM Id CD00000027C5CD28. |
RX- CD00000027C5CD28 | |
TX- W0ABEFFFFFFFFFFFFFFFFFF | Writing block of data: BEFFFFFFFFFFFFFFFFFF. |
RX- BE50054B461FFF0C101C | |
TX- R | Resetting 1-Wire bus. |
RX- |
Now the existing scratchpad should be altered to indicate the desired resolution (see "Configuration Register" within the datasheet), then written back to the device as follows:
TX- ACD00000027C5CD28 | Addressing device. |
RX- CD00000027C5CD28 | |
TX- W044E4B467F | Writing block of data: 4E4B467F. |
RX- 4E4B467F | |
TX- R | Resetting 1-Wire bus. |
RX- |
Initiate Temperature Conversion
The temperature conversion process on the DS18B20 is initiated by transmitting command byte '0x44' to the device, as follows:
TX- ACD00000027C5CD28 | Addressing device. |
RX- CD00000027C5CD28 | |
TX- W0144 | Writing block of data: 44. |
RX- 44 | |
TX- R | Resetting 1-Wire bus. |
RX- |
You must now wait for the conversion to take place before you can read the results back from the device. This conversion time varies depending on the configured resolution. For conversion time details, please reference the DS18B20 datasheet. If the DS18B20 is configured for parasitic power (not being externally powered), then no other activity can take place on the 1-Wire bus during the conversion or the device will be power-starved and the conversion will fail.
Read the Results
The resulting temperature is stored in the DS18B20's scratchpad memory. To obtain the temperature data, simply read the scratchpad from the device and parse the temperature data out of the scratchpad. For details on how the temperature data is encoded into the scratchpad, please refer to the DS18B20 datasheet.
DS18B20- Reading scratchpad from physical device.
TX- ACD00000027C5CD28 | Addressing device. |
RX- CD00000027C5CD28 | |
TX- W0ABEFFFFFFFFFFFFFFFFFF | Writing block of data: BEFFFFFFFFFFFFFFFFFF. |
RX- BE90014B467FFF101092 | |
TX- R | Resetting 1-Wire bus. |
RX- |
The read above returned the following scratchpad bytes: 90014B467FFF101092. Please refer to the DS18B20 datasheet for calculating the scratchpad data.
Advanced Topics
The DS18B20 supports two modes of power. The first is known as parasitic power, in which the device derives its power directly from the 1-Wire signal line. The second is known as external power, in which Vcc is supplied directly to the power pin on the DS18B20. The following method can be used to interrogate which mode the DS18B20 is operating in. If you determine that the DS18B20 is being externally powered then you may be able to optimize performance of your system since other communications can occur on the 1-Wire bus during the temperature conversion process.
DS18B20- Reading power supply status.
TX- ACD00000027C5CD28 | Addressing the device. |
RX- CD00000027C5CD28 | |
TX- W02B4FF | Writing block of data: B4FF |
RX- B400 | |
TX- R | Resetting 1-Wire bus. |
RX- |
Comments
0 comments
Article is closed for comments.