OneWire Lib

Posted on
  • Hi,
    I'm using ST Discovery VL to run espruino. When i try to initialize the OneWire object using
    var ow = OneWire(A10);
    it returns the following message:
    {"proto":prototype,"constructor":OneWire,"pin":­A10}
    What i'm doing wrong?

  • I don't think you're doing anything wrong at all - that's just what the OneWire object looks like.

    You can then just call methods on it - see the DS18B20 library for an example:

    http://www.espruino.com/modules/DS18B20.­js

  • Woow...I supposed it was same sort of error message.

    Going deep further the following code for DS1820 works like a charm ;)
    Thank you Gordon!

    readTemp = function(){
    var ow = new OneWire(C10); 
    ow.device = ow.search()[0];
    ow.reset();
    ow.select(ow.device); 
    ow.write(0x44, true);
    ow.reset();
    ow.select(ow.device);
    ow.write(0xBE);
    var a = 3-1;
    var temp_l = ow.read();
    var temp_h = ow.read();
    var th = ow.read();
    var tl = ow.read();
    var res1 = ow.read();
    var res2 = ow.read();
    var count_remain = ow.read();
    var count_per_c = ow.read();
    var temp_read = ((temp_l + (temp_h<<8))-1)/2;
    var t = temp_read - 0.25 + (count_per_c - count_remain)/count_per_c;
    return t;
    };
    
  • Thanks! Do you know what the difference is between the DS1820 and the DS18B20? I'm just wondering if it's possible to make a new library that does both (as my library only reads the first 2 bytes)...

  • Hello Gordon,

    Do you know what the difference is between the DS1820 and the DS18B20?

    as I know, DS1820 is the predecessor of the DS18B20. The resolution of the 18B20 is configurable between 9, 10, 11 and 12 bits.

  • Sure Gordon...it is possible to make a lib to handle both DS1820 and DS18B20!

    The only difference (as @tickTock correctly sad) is the resolution:

    • DS1820 has fixed 9bit resolution (0,5°C per bit);
    • DS18B20 has a register (Byte 4 of the scratchpad) that permits to choose the resolution( 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively);

    What i've done in the previous post, was to raise the fixed 9 bit resolution by using some additional register value in the scratchpad.

    In a nutshell for:

    • DS1820 9bit resolution : temp = (byte_0 + (byte_1<<8)) * 0,5;
    • DS1820 higher resolution: temp = ( (byte_1<<8) + byte_0 - 1)/2-0,25+(byte_7-byte_6)/byte_6;
    • DS18B20 : temp = (byte_0 + (byte_1<<8)) * RES (where RES=0.5, 0.25, 0.125 or 0.0625 depending from the resolution set)

    Note: is it possible that formulas above doesn't work for temp below zero....in that case you have to play with bits (2 complements...and so on)

    ...finally reading the LSB of 64bit ROM you can understand if you are using either the DS1820 sensor (0x10) or DS18B20 (0x28) one.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

OneWire Lib

Posted by Avatar for Raffaele @Raffaele

Actions