Shop OBEX P1 Docs P2 Docs Learn Events
Help with DS18B20 Code — Parallax Forums

Help with DS18B20 Code

SassySassy Posts: 21
edited 2010-03-19 14:22 in BASIC Stamp
Hi All and Merry Christmas,

I have purchased a PicAxe one-wire Controller from Mr Anderson. I am using his example program to display Temperatures. The Code is fine for
displaying Temperatures above 32 Degrees, but when it gets below or at 32 Degrees Two things happen. SignBit for Negative Temperatures gets displayed at or Below·32 Degrees and the second problem I have is that when Temperatures are below 32 Degrees the Program starts Adding upwards as the Temperature decrease below 32 Degrees. I realize that Mr Andersons program is dealing with Degrees C, but I did insert x=x+3200 to try and convert to F. It does produce fairly accurate temperatures aboue 32 Degrees.... Any Help to solve these two problems would be appreciated.
Thank You in advance
Sassy




'Now do the Calculations

··· SignBit = X / 256 / 128
··· IF SignBit = 1 THEN ' its negative
······ X = X ^ $ffff + 1 ' take the two's comp
··· ENDIF
'Multiply by 11.25 - This is 100 * Tf
··· Y = X / 4··································································· '0.25
··· X = X * 11 + Y
··· x = x + 3200································································ 'Add 32 degrees·************
··· Whole = X / 100
··· Fract = X // 100

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-12-13 17:59
    Is this for a PICAXE or a BASIC Stamp?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • SassySassy Posts: 21
    edited 2007-12-13 18:44
    Sorry Chris,

    The PicAxe is the remote 1-wire device. Here is the completes Code for a Basic Stamp that Ph Anderson uses as an example. Problem I have I want to be able to view below 32 degrees and negative Temperatures.

    Basic Stamp (BS2) Routines

    DS18B20_1.BS2

    ' {$STAMP BS2}' {$PBASIC 2.5}' DS18B20_1.BS2 (Basic Stamp 2)'' Illustrates the use of the ONEWIRE PIC in interfacing with a DS18B20 on' Channel 0.'' BS2-IC ONEWIRE PIC +5 VDC' |' P8 (term 13)
    > "RX" (term 5) 4.7K DS18B20' P7 (term 12) <
    "TX" (term 6) |' "0" (term 13)
    DQ (term 2)' OPEN --- "BAUD" GRD ---- (term 1)' GRD ---- (term 3)'' 9600 baud.'' Copyright, Peter H. Anderson, Baltimore, MD, Nov 14, '06 BaudMode CON 84 '9600 True X VAR Word Y VAR Word SignBit VAR Byte Whole VAR Byte Fract VAR Byte DIR7 = 0 ' serial input DIR8 = 1 ' serial output OUT8 = 0 ' be sure SerOut pin is stable at zero PAUSE 1000 DOAGN: SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "S044"] 'perform temp meas ' note strong pullup PAUSE 1100 ' wait for conversion to complete SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "W0be"] ' send temperature data SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"] ' fetch data SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.LOWBYTE] PAUSE 100 SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"] ' fetch data SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.HIGHBYTE] PAUSE 100 ' now do the calculations SignBit = X / 256 / 128 IF SignBit = 1 THEN ' its negative X = X ^ $ffff + 1 ' take the two's comp ENDIF ' multiply by 6.25 - This is 100 * Tc Y = X / 4 ' 0.25 X = X * 6 + Y Whole = X / 100 Fract = X // 100 IF SignBit = 1 THEN DEBUG "-", DEC Whole, ".", DEC2 Fract, CR ELSE DEBUG DEC Whole, ".", DEC2 Fract, CR ENDIF PAUSE 3000 ' wait 3 secs LOOPTimeOut: DEBUG "Timeout Error", CR GOTO AGN
  • phil kennyphil kenny Posts: 233
    edited 2007-12-15 08:11
    Hello Sassy,

    Your initial code has problems below 32 deg. F and can be fixed by
    adding the following snippet:

    IF SignBit = 1 THEN ' its negative
    X = X ^ $ffff + 1 ' take the two's comp
    ENDIF

    As well as changing the name of the raw temperature value read
    from the DS18B20 from X to Temp_Raw

    Change the code to read:

    =====================================

    'Now do the Calculations

    ' Note that the original raw temperature is stored in a variable named
    ' Temp_Raw and X is a completely different variable

    SignBit = Temp_Raw/ 256 / 128

    IF SignBit = 1 THEN ' its negative
    Temp_Raw = Temp_Raw ^ $ffff + 1 ' take the two's comp to make it positive
    ENDIF

    'Multiply by 11.25 - This is 100 * Tf
    Y = Temp_Raw / 4 '0.25
    X = Temp_Raw * 11 + Y

    IF SignBit = 1 THEN ' its negative
    X = X ^ $ffff + 1 ' take the two's comp to make X negative, if needed
    ENDIF

    x = x + 3200 'Add 32 degrees ************
    Whole = X / 100
    Fract = X // 100

    ====================================

    and it should work down to 0 degrees F.

    phil kenny

    Post Edited (phil kenny) : 12/15/2007 8:47:48 AM GMT
  • phil kennyphil kenny Posts: 233
    edited 2007-12-16 20:15
    Hello Sassy,

    Attached is code that works over the full range from -55 C to +124 C.

    It has been tested using the values shown in Table 2 of the DS18B20
    data sheet.

    The code can be simplified, but I felt it would be easier to understand
    the logic if it was more verbose.

    Hope this helps.

    phil kenny

    Post Edited (phil kenny) : 12/16/2007 8:42:37 PM GMT
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2007-12-17 17:57
    Phil and Sassy,

    Here is a routine that reads the DS18B20 and calculates degC and degF in two lines. Note that the sign of each is defined as an alias to bit15 in each variable.

     ' ----------------------------------------------------------------------------------------------
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' Tracy Allen, EME Systems
    ' DS18B20 (12 bit) program displays temperature, -55 to +125 Celsius +/- 0.1 degree
    ' For a single DS18B20 on the one-wire buss--the skip ROM command is used here.
    ' If more than one is on the buss, then the match ROM command must be used.
    ' Note that this routine is not good for the older 8-bit DS1820 nor the DS18S20.
    degC VAR Word
    signC VAR degC.bit15   ' alias for sign bit
    degF VAR Word
    signF VAR degF.bit15   ' alias for sign bit
    DO
      OWOUT 0,1,[noparse][[/noparse]$CC, $44]
      DO : OWIN 0,4,[noparse][[/noparse]degC] : LOOP UNTIL degC  ' wait for done
      OWOUT 0,1,[noparse][[/noparse]$CC, $BE]
      OWIN 0,2,[noparse][[/noparse]degC.byte0, degC.byte1]
      degC = -signC ^ (abs degC */ 160) + signC   ' convert to degC*10, good for -degC too.
      degF = degC + 1000 * 9 / 5 - 1800   ' convert to F using offset trick
      
      DEBUG "degC=", REP "-"\signC, DEC ABS degC/10, ".", DEC1 ABS degC,TAB
      DEBUG "degF=", REP "-"\signF, DEC ABS degF/10, ".", DEC1 ABS degF, CR
      PAUSE 1000
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • phil kennyphil kenny Posts: 233
    edited 2007-12-18 17:38
    Tracy,

    Thanks for showing the 'Offset trick'. Always looking for ways
    to reduce the code, Nice technique.

    phil
  • Dave-WDave-W Posts: 94
    edited 2007-12-18 20:06
    Tracy,
    I tried your code today on a DS18B20 and the program runs fine. However the temp readings I am getting are not correct. At 67 degrees F I am reading 33 degrees F. I looked over the code 20 times and I do not see why the readings are off? Everything looks correct. Do you have an idea?
    Dave

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    D. A. Wreski
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2007-12-19 02:52
    Sorry, I forgot to add 320 for the degF*10 conversion. It should read,

    ' ----------------------------------------------------------------------------------------------
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' Tracy Allen, EME Systems
    ' DS18B20 (12 bit) program displays temperature, -55 to +125 Celsius +/- 0.1 degree
    ' For a single DS18B20 on the one-wire buss--the skip ROM command is used here.
    ' If more than one is on the buss, then the match ROM command must be used.
    ' Note that this routine is not good for the older 8-bit DS1820 nor the DS18S20.
    degC VAR Word
    signC VAR degC.bit15   ' alias for sign bit
    degF VAR Word
    signF VAR degF.bit15   ' alias for sign bit
    DO
      OWOUT 0,1,[noparse][[/noparse]$CC, $44]
      DO : OWIN 0,4,[noparse][[/noparse]degC] : LOOP UNTIL degC  ' wait for done
      OWOUT 0,1,[noparse][[/noparse]$CC, $BE]
      OWIN 0,2,[noparse][[/noparse]degC.byte0, degC.byte1]
      degC = -signC ^ (abs degC */ 160) + signC   ' convert to degC*10, good for -degC too.
      degF = degC + 1000 * 9 / 5 - 1800 + 320 ' convert to degF*10 using offset trick <---------
      
      DEBUG "degC=", REP "-"\signC, DEC ABS degC/10, ".", DEC1 ABS degC,TAB
      DEBUG "degF=", REP "-"\signF, DEC ABS degF/10, ".", DEC1 ABS degF, CR
      PAUSE 1000
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • planktonplankton Posts: 18
    edited 2009-07-23 08:19
    All,

    Can you use the DS18B20 in 'power steal mode' where pins 1 and 3 are tied to ground?

    Thanks

    Scott
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-07-23 15:51
    Scott,

    The Stamp does not have the capability to switch over to the "strong pullup" needed during the conversion. This came up in another thread recently. PhiPi suggested an word-around for a two-wire implementation, where a diode and capacitor local at each node would store the necessary power to operate the sensor in 3-wire mode.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • planktonplankton Posts: 18
    edited 2009-07-27 03:04
    Tracy - I tried using the DS18B20 in 'power steal mode' but just wasn't stable no matter how much time I gave the sensor to 'recharge'. I ended up running 4-wire phone cable so I could provide +5VDC and that solved the problem. Your logic works perfectly and now I can read temp from two sensors.

    Thanks as always for you help.

    Scott
  • YoshtiYoshti Posts: 108
    edited 2010-03-18 15:43
    Hi all,
    I got the DS18b20 working. Some site does not show the pull-up resister. Finally in the OWIN help file, it shows I must use
    the pull-up 4.7k for the 1 wire. Yes got it working !

    Now, I'll be using many 18B20, and trying to get the ID off of it.
    I issue a OWOUT 0,1,[noparse][[/noparse]$33]
    then OWIN 0,2,[noparse][[/noparse]serial]
    and all i get is a (
    hum... serial is defined as a var Word.
    I'm using the BS2px ...

    I'm totally lost here...

    Sorry to bring that old thread.. but I'm way out of my league here...

    Any help ..pretty please!
    Cheers
    Yosh
  • YoshtiYoshti Posts: 108
    edited 2010-03-19 14:22
    Hi all,
    Ok found the solution! Not very clear but all sorted out!

    On this thread: http://forums.parallax.com/forums/default.aspx?f=5&m=89924

    A fairly well explained (not for a noob and old fart like me) post , that is giving the code for it.

    For the noob , the file owsearch2 is only a part of what you need.

    If you read carefully, you have to get the code on the nuts and volts provided in the same thread.

    Be carefull ! for which BS you use, so modify your directive accordingly.
    Mine had to be set to BS2px and pbasic 2.5 ..and the file extension was change to .BPX .
    So, to help, my searchdemo.pbx directive was set to:
    STAMP BS2px, OWSEARCH2.BPX,DSNAMES.BPX
    PBASIC 2.5

    So it works... finally
    Just helping anyone who might be getting frustrated... smile.gif
    Cheers
    Yosh
Sign In or Register to comment.