0

I am working on the DS18B20 temp sensor implementation. Code is running well. But have a question related to the "conversion time".

Chip datasheet states I should wait the given time (hundreds of ms) after issuing the "start_conversion" command, before I can go on reading the data registers (to get the temperature).

What I saw is, and here is my question, I do not HAVE to wait. My code runs using both 750ms or 0ms as a wait time. Data register have some values and I can get them without problems.

So how it works? Have I wait or not. What's the data in the data register if the wait time is shorter then required ?

My personal filling is that I do not have to wait, I just CAN wait for THIS conversion. But I can read register any time but will just get the latest stored temperature. Could be not the one I asked for starting conversion few clock cycles before ...

2
  • "If the DS18B20 is powered by an external supply, the master can issue read time slots after the Convert T command and the DS18B20 will respond by transmitting a 0 while the temperature conversion is in progress and a 1 when the conversion is done." Commented Nov 30, 2015 at 11:59
  • sure, but we are talking about the sequance of commmands. Commented Nov 30, 2015 at 12:13

1 Answer 1

0

You do have to wait for the conversion to finish. But the standard way of doing this is to read the last result, then start a new conversion to read later.

loop
    start conversion
    yield for at least 750 ms
    read result
    loop

Notice that the code reads the result, then immediately jumps to the top of the loop to start the next conversion.

Is this what your code is doing?

EDIT in response to comment:

Hanging the CPU is a bad approach. That's why I suggested not doing that.

As for reading temperature values while a conversion is in progress, I don't see an internal register other than the TH and TL. What that says to me is that the bits of these registers will be changed from the high bits to the low bits as the conversion progresses. If you read during a conversion, you will get current values for the high bits but outdated values for the lower bits. This means you will get poor precision, and potentially very wrong results.

For example, the old result could end with 0111111. If the new result is a little higher, then it might end in 1000000. During the conversion, the result will hold 1111111 instead, which is much higher than both the old and new results.

The unit seems to respond with 0 while the conversion is in progress. You can check to see if it is safe to read the value.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes it looks like that .. but hanging MCU (Atmel atmega) for 750 ms is a huge waste of time. My idea would be to separate the process into two actions: issuing the "start_converstion" command (like every 1000ms) and just a poor data register read (in other process whenever I need last temperature). The question is if I can do both actions separately.
"I don't see an internal register other than the TH and TL." - I do: Byte #5...#7 are "reserved", as well as 6 bits of the configuration register. Plenty of room for the device to keep internal data. Plus: "Following the conversion, the resulting thermal data is stored in the 2-byte temperature register in the scratchpad memory" - One could read from that that the temperature register is only changed at one point after the conversion and not continuously while the conversion is running.
Made some tests and have found there is no reason to wait for THIS conversion result, data register can be read any time. So good enough and working solution for me is to call, every second, a sequence "read_data_register" , "start_conversion". Sure the data_register is empty for first call but starting from next one there is last temperature inside. This way my measurements are one second late, but do not have to wait.
Can not edit comment above so little errata here: I mean scratchpad_data_registers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.