Various sensors manufactured by Embedded Data Systems contain DS18S20 sensors by Maxim Integrated. This knowledge base article discusses how to work with the DS18S20 sensor using an HA7E and HA7S 1-Wire bus masters.
The general process for interacting with the DS18S20 temperature sensor is as follows:
- Discover the Device
- 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 HA7, and RX= data received from the HA7. This example is written for a DS18S20 having ROMId '0C00080082FD3B10'. The manufacturer's datasheet on the DS18S20 can be downloaded from https://www.embeddeddatasystems.com/manuals/DS18S20.pdf.
Discover the Device
In this example, the DS18S20 is the only device on the 1-Wire bus so we simply use the HA7 'S'earch Rom command to identify the first device:
TX- S | Performs a standard device search. |
RX- 0C00080082FD3B10 | The ROM Id of the DS18S20. |
Initiate Temperature Conversion
The temperature conversion process on the DS18S20 is initiated by transmitting command byte '0x44' to the device, as follows:
TX- A0C00080082FD3B10 | Addressing Device having ROMId 0C00080082FD3B10. |
RX- 0C00080082FD3B10 | |
TX- W0144 | Writing block of data: 0x44. |
RX- 44 | |
TX- R | Resetting 1-Wire bus. |
RX- |
Wait for conversion
You must now wait 750mS for the conversion to take place before you can read the results back from the device. If the DS18S20 is being powered parasitically, then no other activity can occur on the 1-Wire bus during the temperature conversion, or the DS18S20 will be power starved. Note that the HA7 automatically provides strong pull-up power on the 1-Wire bus during the conversion.
Read the Results
The resulting temperature is stored in the DS18S20'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 complete details on how the temperature data is encoded into the scratchpad, please refer to the DS18S20 Datasheet.
Reading the DS18S20's scratchpad.
TX- A0C00080082FD3B10 | Addressing Device having ROMId 0C00080082FD3B10. |
RX- 0C00080082FD3B10 | |
TX- W0ABEFFFFFFFFFFFFFFFFFF | Writing block of data: BEFFFFFFFFFFFFFFFFFF. |
RX- BE34004B46FFFF0B1096 | Scratchpad data |
TX- R | Resetting 1-Wire bus. |
RX- |
The read above returned the following scratchpad bytes: 34004B46FFFF0B1096.
Parse the Results
Based on the DS18S20 Memory Map on page 6 of the datasheet, we can parse the scratchpad bytes returned above to determine the following:
Byte 0 = Temperature LSB = 0x34
Byte 1 = Temperature MSB = 0x00
Byte 2 = TH Register = 0x4B
Byte 3 = TL Register = 0x46
Byte 4 = Reserved = 0xFF
Byte 5 = Reserved = 0xFF
Byte 6 = Count Remain = 0x0B
Byte 7= Counter Per C = 0x10
Byte 8 = CRC = 0x96
The DS18S20 datasheet provides the following formula for calculating the temperature in Celsius:
TEMPERATURE = (TEMP_READ - 0.25 + ( (COUNT_PER_C - COUNT_REMAIN) / COUNT_PER_C)) / 2
The memory map of the DS18S20 works out such that the Temperature_MSB simply represents the sign of the temperature. That is, if Temperature_MSB is > 0, then the temperature reading is less than 0. Based on this, TEMP_READ is calculated in a manner similar to the following:
TEMP_READ = Temperature_LSB
If Temperature_MSB > 0 Then
TEMP_READ = TEMP_READ * -1
End If
For our example, TEMP_READ = 0x34. Since Temperature_MSB is 0, the reading is positive. Now making substitutions into the equation above, we have the following:
TEMPERATURE = (0x34 - 0.25 + ( (0x10 - 0x0B) / 0x10) ) / 2
Converting to decimal:
TEMPERATURE = (52 - 0.25 + ( (16 - 11) / 16) ) / 2
This gives us TEMPERATURE = 26.03125 C.
Advance Topics
Power Modes
The DS18S20 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 DS18S20. The following method can be used to interrogate which mode the DS18S20 is operating in. If you determine that the DS18S20 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.
Reading the power supply status:
TX- A0C00080082FD3B10 | Addressing Device having ROMId 0C00080082FD3B10. |
RX- 0C00080082FD3B10 | |
TX- W02B4FF | Writing block of data: B4FF. |
RX- B400 |
The status byte returned here was '00'. This indicates that the sensor is deriving its power from the 1-Wire bus (parasitic power.) A non-zero value would indicate that the sensor is being powered via its Vdd pin.
Writing to the TH and TL Register
The TH and TL register may be utilized as either general purpose memory or temperature alarm registers. In either application, it will be necessary to read and write the TH and TL bytes. The TH and TL bytes are read following the same procedure outlined in the “Read the Results” section. Writing “1234” to the TH and TL Bytes can be achieved through the Wire Scratchpad (4E) command below:
TX- A0C00080082FD3B10 | Addressing Device having ROMId 0C00080082FD3B10. |
RX- 0C00080082FD3B10 | |
TX- W034E1234 | Writing block of data: 4E1234. |
RX- 4E1234 | |
TX- R | Resetting 1-Wire bus. |
RX- |
Writing TH and TL Register to EEPROM
In situations where maintaining the TH and TL Register's data through power-up is desired, the following commands can be used to copy the contents to EEPROM:
TX- A0C00080082FD3B10 | Addressing Device having ROMId 0C00080082FD3B10. |
RX- 0C00080082FD3B10 | |
TX- W034E1234 | Writing block of data: 4E1234. |
RX- 4E1234 | |
TX- R | Resetting 1-Wire bus. |
RX- |
Comments
0 comments
Article is closed for comments.