1-Wire, DS18B20... How do I search the bus?

guys

Let me start by saying hats off to you on the work you have done. I am obviously new, so this question may be answered elsewhere... I have two DS18b20's working it reports the addresses. But thinking of this as a comercial application... How do I tell the system which sensore is located where? I have a memory chip setup, so once I figure it out I can store the address... But how do I make it so a user can set it up and insure the temp I am reading is located where I think it is...

I think I found a bug on read_bit(void) function of current library.

This library is works good on most of devices but my device doesn't happy with this library.

It doesn't submit correct values on ReadROM command properly. Searched a lot and find something:

According to Maxim-IC,
Read Time Slot (Read Zero):
Defines A to BR time as 9 micro second, than A to CR time as 18 us.
So, we needed to wait at least 18uS after submitting 0 for reading the line value.
But current Arduino code is slightly different, Sample time is (3+9) 11uS after submitting 0 that generates the bug on my device.

Current OneWire Lib 2.0:

uint8_t OneWire::read_bit(void){
          uint8_t mask=bitmask;
      volatile uint8_t *reg asm("r30") = baseReg;
      uint8_t r;

      cli();
      DIRECT_MODE_OUTPUT(reg, mask);
      DIRECT_WRITE_LOW(reg, mask);
      delayMicroseconds(3);
      DIRECT_MODE_INPUT(reg, mask);      // let pin float, pull up will raise
      delayMicroseconds(9);
      r = DIRECT_READ(reg, mask);
      sei();
      delayMicroseconds(53);
      return r;
        }

Why do we needed to sample this bit at 11us? Spec allows us up to 30uS.

Changing it via

uint8_t OneWire::read_bit(void){
uint8_t mask=bitmask;
        volatile uint8_t *reg asm("r30") = baseReg;
        uint8_t r;
        
        cli();
        DIRECT_MODE_OUTPUT(reg, mask);
        DIRECT_WRITE_LOW(reg, mask);
        delayMicroseconds(9);//Increased to make it compatible to Spec
        DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
        delayMicroseconds(15);//This increased too to iterates sampling time to 21uS after zero pulse. More compatibility.
        r = DIRECT_READ(reg, mask);
        sei();
        delayMicroseconds(41); //decreased 12uS(6uS+6uS).  So no speed degrade with this code.
        return r;
        }

Makes the job well. I don't know exactly if this bug might be corrupting search algorithm too at beginning pages.

Regards,
Erdem U. Altinyurt

The DS18B20 is probably the most common 1-wire device. Here is the datasheet:

http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf

Please look at Figure 16 "Recommended Master Read 1 Timing" on page 17 of 22. The diagram shows the line is supposed to be sampled shortly before 15 us after the initial low pulse began. The timings in the current code are 3 us low pulse, plus 9 us. That's 12 us, which allows the small amount of extra overhead from code execution to be as much as 3 us (if the CPU is running at 8 MHz or slower).

The page you linked suggests 18 us from the initial pulse until sampling (or "A to CR"). That's 3 us beyond the maximum recommended by the DS18B20. Also, your code below generates a 9 us pulse plus a 15 us delay, which is a total of 24 us from "A to CR", or 6 us more than the page you linked, and 9 us more than suggested for the DS18B20. That doesn't seem like such a good approach....

I am open to increasing the timings, but since the DS18B20 and similar chips are so very common, I really think it's a not good to increase the total "A to CR" more than 14 us (it's currently 12), since it's supposed to be less than 15 for those incredibly common parts.

Does it work on your device if you use 4 us pulse and 10 us delay before reading?

Can you capture the actual signal on your device with a digital scope?

Thanks for quick response. Yes you have right. I don't have exact 1 wire specification pdf. So searched over internet and found that it's recommended to use 18uS. Don't looked the spec of DS18B20. Read Valid Time(RVT) is 15uS for this chips. But they also don't recognize first 1uS I think. :wink:
Since I have Deumilanove, got 16Mhz cpu. So that 3 us overhead not count for my setup.

I am really happy with maxims 18uS spec. Since sample time is valid up to 30uS depending on wiki, I thought it will be more compatible by adding 6us to second delay. I can still read DS18B20 in this configuration but yes, second delay could corrupt some low quality DS chips.

I played with delays and found that Also 15uS (3+12) is working ok but anything lower than 15us generates corrupted read. Also checked my (4) all DS18B20's output is valid readable up to 26uS in both Powered or Parasite Powered configurations in reality. I guess 11uS margin (15uS - 26uS) is little much for the safety of reading this chips.
Sample time is required to > 15uS at Wikipedia. Wiki says device needed to hold bit 30uS also. It's not compatible with DS18B20's spec sheet since VRT is 15us. I think there is small anarchy over specs of OneWire protocol. 18uS better for proper protocol that in my mind or for Maxims :). 15uS (3+12) will not crash any code than I believe. But it's RVT Max and could be ~+1uS higher than DS18B20's RVT. I think RVT + 2~3uS will be in safety line.


Also chip cannot detect 1.st uS since voltage is started to dropping but not grounded, so line is in set state. And line will not be in set state after RVT immediately, due both voltage start rising + safety margins of the chips. It's clearly seen on old DS1820's spec (without B) Figure 18 (Line start rising up after ~17.th uS). Spec sheet says sample time required < 15uS also but It will work properly in even 17.uS sample time depending graph. Slow CPU's will iterate to 18uS but read operation will be in successful since DS18B20's grounds line for >25us in reality.
Also new spec sheets defines RVT 15uS but there is 3uS gap between of line starting to drop down to zero and RVT start. (I counted the pixels ;D )

So 12uS is too low and overkill for the 1Wire protocol, 14uS is matches for DS18B20's but better to move 15us for more compatibility with the protocol. I think it's sweet spot. I advice to using 16uS. It will be look more compatible with 1Wire specs. Thats all works with DS18B20's.

I don't have scope. But will try to make one from my arduino one day... :wink:

Regards,
Erdem

What value of pullup resistor are you using?

Perhaps you could try using a lower value resistor? Or if you have a long cable, try using a lower capacitance cable.

I guess 11uS margin (15uS - 26uS) is little much for the safety of reading this chips.

Did you test at the temperature extremes, or only at room temperature? The wide timing margin is needed to accommodate changes in the timing inside the 1-wire chips, which lack accurate clocks and can change substantially over temperature. They can also change from batch to batch.

Ignoring the the 15 us maximum would not be a good practice. While it worked with the 4 devices you tested, at whatever temperature you tested, going beyond the spec is just asking for trouble.

My guess is your pullup resistor is a bit weak. Using a stronger (lower value) resistor will likely pull the line up faster, enabling you to use the DS18B20 timing.

I used 4.7k standard pull-up for 1Wire device bus. I don't tested under chilled temperatures. But 26uS gives correct reads >127 C degrees with DS18B20's without single bit of error with both powered & parasite power connections... I don't wait different results under -55 C degrees. It might change it's characteristic on different batches +- 1 or 2 uS. But there is ~10uS margin surely will protects us. So +1uS will not arise trouble.

At my situation, I think it's required to use higher value of resistor since I wanted to longer "0" pulses, not shorter one, that slow downs line pull-up. I can turn around this problem on my local setup via using altered OneWire library code or electronically by using new resister. But I just wanted to make Arduino's OneWire library more compatible with OneWire spec. At least, please think about adjusting reading time to 14uS that matches with DS18B20 master sample time.
Regards