ds18s20 reads incorectly


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 43
  1. #1

    Cool ds18s20 reads incorectly

    i have tried several programs on this and several other sites to read temperature
    read a lot of posts on the subject and have been able to get most to function
    however the temperature readings are all low aprox 30 deg f this is only happening when i use pbp

    tried mikro and works perferct what the difference??

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Cool

    Quote Originally Posted by jcleaver View Post
    i have tried several programs on this and several other sites to read temperature
    read a lot of posts on the subject and have been able to get most to function
    however the temperature readings are all low aprox 30 deg f this is only happening when i use pbp
    tried mikro and works perferct what the difference??
    What's the difference? Plenty...signed vs un-signed integers is a good possibility for one thing...
    We might be able to help you out a bunch better if you were to post one of those 'several' programs from one of those 'several' sites...
    Or a search here reveals:
    http://www.picbasic.co.uk/forum/showthread.php?t=5349
    http://www.picbasic.co.uk/forum/showthread.php?t=7091
    just for a couple of examples...
    http://www.picbasic.co.uk/forum/showthread.php?t=7079
    http://www.rentron.com/PicBasic/PBP1-wire.htm
    Last edited by skimask; - 22nd July 2008 at 19:31.

  3. #3


    Did you find this post helpful? Yes | No

    Default program

    thanks for the prompt responce
    http://www.rentron.com/PicBasic/one-wire3.htm
    using this program with changes made for a different type of display
    noticed it used the 18d20 but cant seem to find a program that uses the 18s20
    nor after looking over several posts can i come up with the correct way to read this

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi,

    Just look at programs for DS1820 ...

    or compare the " Operation measuring temp " chapters of DS18B20 and DS18S20

    You will find a correct program for DS1820 in The Parallax Stamp applications ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5


    Did you find this post helpful? Yes | No

    Default Update

    Finally Got It Working Better
    Thanks For The Inputs
    Notice I Said Better Still Reads 10 F Deg Low
    But Better Really Dont Understand Why Mikroe Works Perfectly With The Same
    Hardware Using Pbp 5.0 And Mcsp 3.0
    Must Be Some Difference In The Complier No? Maybe?

  6. #6
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Default

    I posted a program that reads DS18S20s. I have 4 of them on onewire and I read them with no problem and I use PBP.
    I do not expect temps below zero so I have not made allowances for that.

    http://www.picbasic.co.uk/forum/showthread.php?t=9116

  7. #7


    Did you find this post helpful? Yes | No

    Default

    Hi jcleaver,

    You might want to check Maxim forum at:

    http://discuss.dalsemi.com/index.php?

    They had rejects on DS1820 family showing wrong temperature and there is a fix for it.

    Nick

  8. #8


    Did you find this post helpful? Yes | No

    Default Still ?

    I Do Appreicate Your Inputs
    Manwolf Your Program Looks Great But Way Beyond My Knowledge
    And As For As The Ds18 Goes I Have Tried Several

    Again To Ask ?
    Why Does Mikro Work Where Pbp Does Not
    There Has To Be Some Difference In The Compliers

    Using Exactly The Same Hardware Easypic4
    Could It Be The Math Conversion Using Pbp

    Both Programs Use The Same Formula For C To F

  9. #9
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Default

    How are you reading the temp form the DS18S20 in PBP?
    Are you using one-wire or something else?

  10. #10


    Did you find this post helpful? Yes | No

    Default programs

    i have attached the 2 programs one in pbp which reads temperature incorectly
    the other mikroe which works no hardware changes are made really interested in the difference


    thanks

    jack
    Attached Files Attached Files
    Last edited by jcleaver; - 23rd July 2008 at 00:54.

  11. #11
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jcleaver View Post
    i have attached the 2 programs one in pbp which reads temperature incorectly
    the other mikroe which works no hardware changes are made really interested in the difference


    thanks

    jack
    Speaking strictly about PBP....
    You know, just like it says in the manual, define's have to be in CAPS.
    Therefore, the
    define osc 20
    is ignored and the timing generated by the code is generated for an oscillator of 4mhz, but apparently you are running it at 20mhz, and therefore, probably far too fast.
    Did you write this code or copy/paste it? Why not use PBP's built-in OWIN/OWOUT commands?
    Last edited by skimask; - 23rd July 2008 at 02:41.

  12. #12
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Default

    maybe you should check the math.
    With a change to the math the program works correctly. Running on a EasyPIC5 board.

    Sorry I was sleeping that day in class, so I can't explain why PBP and MBasic work differently.

  13. #13
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink


    Why not use PBP's built-in OWIN/OWOUT commands?
    The solution simply is here ...

    MKB reads Byte by Byte and Pbp ... as you want !

    just need to program a correct reading with Pbp !



    Further ... TempF = TempC *9/5 + 32 ...

    Remember PbP works with UNSIGNED INTEGERS ...

    Alain

    PS: Ski, MKB has a built-in timing adjustment,( you check an option in the project window instead of writing a DEFINE ) and The "conversion complete" polling built-in. That surprised me a bit at first time, not to see it in the source listing ...
    Last edited by Acetronics2; - 23rd July 2008 at 09:01.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  14. #14


    Did you find this post helpful? Yes | No

    Default attempt

    the osc is correct dont know why it did not show caps
    tried going with 4 still read low
    ? what changes in math
    TEMP = ((TEMP*9/5)+32 ) is what i am using
    thanks for the inputs

    have not found any samples using owin with this chip

  15. #15
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Thumbs down

    Hi,

    Did you ever try SEARCH with the 1820 Keyword ???

    Surely NO !!!

    I remember having posted a while ago for the DS1820 ...

    Gives ... 22 Hits !

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  16. #16
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    I remember having posted a while ago for the DS1820 ...
    Gives ... 22 Hits !
    Alain
    23 Hits now!
    But, he's also using the DS18S20.
    I know...I know...not a lot of difference between the two that can't be handled by a simple read of the datasheet.

  17. #17
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jcleaver View Post
    the osc is correct dont know why it did not show caps
    tried going with 4 still read low
    ? what changes in math
    TEMP = ((TEMP*9/5)+32 ) is what i am using
    thanks for the inputs

    have not found any samples using owin with this chip
    Ok, try this:
    Code:
    @ DEVICE pic16F877A, HS_OSC
    @ DEVICE pic16F877A, WDT_ON
    @ DEVICE pic16F877A, PWRT_ON
    @ DEVICE pic16F877A, BOD_ON
    @ DEVICE pic16F877A, MCLR_on
    @ DEVICE pic16F877A, LVP_OFF
    @ DEVICE pic16F877A, CPD_OFF
    @ DEVICE pic16F877A, PROTECT_OFF
    define osc 20
    define loader_used 1
    define lcd_dreg portd
    define lcd_dbit 4
    define lcd_rsreg portd
    define lcd_rsbit 2
    define lcd_ereg portd
    define lcd_ebit 3
    define lcd_lines 2
    define lcd_bits 4
    define lcd_commandus 2000
    define lcd_dataus 50
    command var byte:i var byte:temp var word:dq var porta.5:dq_dir var trisa.5
    adcon1=255:porta=255:portb=0:trisa=255:trisb=0:cmcon=7
    Lcdout $fe,1,"Temp in degrees C"
    mainloop:	Gosub init1820:command=$cc:Gosub write1820:command=$44
    Gosub write1820:Pause 2000:Gosub init1820:command=$cc:Gosub write1820
    Pause 2000:command=$be:Gosub write1820:Gosub read1820:TEMP=((TEMP*9/5)+32)
    Lcdout $fe,1,dec(temp>>1),".",dec(temp.0*5)," degrees C":Goto mainloop
    init1820:	Low DQ:Pauseus 500:DQ_DIR=1:Pauseus 100
    If DQ=1 Then Lcdout $fe,1,"DS1820 not present":Pause 500:Goto mainloop
    Pauseus 400:Return
    write1820:	For i=0 to 7:low dq
    If command.0[temp] = 0 Then
    	Pauseus 60:DQ_DIR = 1
    Else
    	@ nop
    	DQ_DIR = 1:Pauseus 60
    Endif
    Next i:Return
    read1820:	For i=0 to 15:low dq
    		@ nop
    		dq_dir=1:temp.0[i]=1:if dq=0 then temp.0[i]=0
    		pauseus60:next i:return
    End

  18. #18


    Did you find this post helpful? Yes | No

    Default Update 2

    Tried This And Get Better Reading Only 6 Deg Low

    However It Does Not Respond To Any Temperature Changes

    Thanks

  19. #19
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jcleaver View Post
    Tried This And Get Better Reading Only 6 Deg Low
    However It Does Not Respond To Any Temperature Changes
    Thanks
    Ok, what DOES it put out for a temperature?

    What happens if you heat it up, read the temperature, shut it off, put it in the freezer for a few minutes, take it out, power it up and read the temp right away?

  20. #20


    Did you find this post helpful? Yes | No

    Question Answer

    Read 65.xxxx

    Does Not Update Hold Sensor And Nothing Changes

    Reloaded Several Programs And They Work However Still Get Low Tmp Reading

    Dont Think Its A Hardware Problem Because It Works Perfectly In Mikroe

    Guess I Could That But Have Better Luck With Pbp As For As Adding My Stuff

  21. #21
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jcleaver View Post
    Read 65.xxxx
    Does Not Update Hold Sensor And Nothing Changes
    Reloaded Several Programs And They Work However Still Get Low Tmp Reading
    Dont Think Its A Hardware Problem Because It Works Perfectly In Mikroe
    Guess I Could That But Have Better Luck With Pbp As For As Adding My Stuff
    65.xxx
    Hmmm....maybe 65.535? 65.535 F? 65.535 C ?
    Ever think that maybe your input was all 1's? And that it might've just been coincidence that your temp's are about 30 degrees low depending on the temp of the room?

    Try this, might help...
    http://www.picbasic.co.uk/forum/showthread.php?t=561

    AND:
    Last edited by skimask; - 23rd July 2008 at 20:05.

  22. #22
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi, Cleaver

    I suppose you work on an easyPic 4 or 5 ...

    try this with sensor connected to PORTE2 and Pullup on porta.0 and 1.

    After power on, type of sensor and ID is displayed.

    press RA0 Button ( active low ! ) and you turn to thermo mode ( thermostat, in fact , see RA0 and RA.1 use ... )

    Alain

    PS : Fully PbP Written Program...
    Attached Files Attached Files
    Last edited by Acetronics2; - 23rd July 2008 at 21:03.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  23. #23
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Default

    jcleaver

    Here is a couple of files for you to play with.

    pbptc.txt is your program that correctly displays the temp C and changes when the temp changes. You will need to change the LCD port, I use an easypic5 and you are using a different port.

    I also have a program for MikroB attached, again it is for the ep5 board.

    Take a few minutes and look at how the temp is being read in both. Once you figure that out you will see where the problem is.

    BTW the formula is from the DS18S20 datasheet.





    In your Mikro file did you notice that the temp was calculated before it is converted?

    if j2 = $FF then
    tmp_sign = "-" ' temperature sign
    j1= j1 or $FF ' complement of two
    j1= j1 + $01
    else
    tmp_sign = "+"
    end if
    j2 = (j1 and $01) * 5 ' Get decimal value
    j1 = j1 >> 1 ' Get temp value
    j1 = ((j1*9/5)+32) 'Convert C to F

    And in your PBP file nothing is calculated until after the conversion.

    TEMP = ((TEMP*9/5)+32 )
    Lcdout $fe, 1, dec (temp >> 1),".",dec(temp.0 * 5)," degrees C"

    Good Luck!

    Acetronics: May I play around with the thermostat program you posted?
    Attached Files Attached Files
    Last edited by manwolf; - 23rd July 2008 at 23:18. Reason: Brain Bubble

  24. #24


    Did you find this post helpful? Yes | No

    Default Thanks

    Want To Thank All Of You For Your Help Been Great To Work With
    Will Keep You Updated

    Also Would Like To Look At The Pb Code For The Thermostat Program

    If Not Giving Away Something

    Again Thanks

    Jack

  25. #25


    Did you find this post helpful? Yes | No

    Exclamation Interesting

    Manwolf
    Worked With Your Pbp And Your Right It Is Displaying The Correct C Temperature

    Its Pb Math Thats Incorect

    Any Ideas How ?

    Also The Formula Temp = ((temp*1.8)+32) Gives Corect Answer But Wont Compile
    On Pbp

  26. #26
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Post

    Jack

    As Acetronics stated:

    Remember PBP works with UNSIGNED INTEGERS ...

    So 9/5 = 1.8 still won't fly in PBP

    How about something like this?

    temp = (((temp >> 1) * 100) - 25) 'calculate degrees C

    temp = (temp */ 461) + 3200 'convert degrees C to F

    And before you ask, crack open the ugly green book and figure out what '*/' does.

    Hint: it uses all unsigned integers.

    The hardest part of writing any program is learning how to handle the math. If you can not get your head around the math none of this stuff makes any sense.

    Don't be afraid to throw some output in there to see what is happening. Run Debug, output to LCD or a serial port to see what it is changing.


    For a better explaination of the math go here - http://www.rentron.com/PicBasic/one-wire3.htm

    Ted
    Last edited by manwolf; - 24th July 2008 at 04:26. Reason: Forgot to answer the question

  27. #27
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Manwolf

    What do you mean by that :

    " Acetronics: May I play around with the thermostat program you posted? "

    Of course YOU can play with it ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  28. #28
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Your bit-banged read & write routines use a @ NOP for a 1uS delay, but you're running
    at 20MHz so these are only 200nS. You would need 5 x @ NOP for a 1uS delay.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  29. #29


    Did you find this post helpful? Yes | No

    Default Reply

    Thanks For The Math Help
    As For As Speed Goes I Have Treid 4 And 20 Does Not Make A Difference

    Will Work On The Math Issue

    Do You Have The Thermostat Program In Pbp Not Hex

    Thanks

  30. #30
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink I do not give the fish any more ...

    Hi, Jcleaver

    The thermostat program was for you to verify what you had to find ... and show you a different Bargraph view.

    NOT to copy / paste it ...

    You can find any parts of it on this forum, The DS18x20 subject has been widely explored during the past months.

    but that need some little efforts ... is it the problem ?

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  31. #31
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Unhappy

    Quote Originally Posted by Acetronics View Post
    NOT to copy / paste it ...
    ...............................
    but that need some little efforts ... is it the problem ?
    Awwwww!!!!!

  32. #32


    Did you find this post helpful? Yes | No

    Default

    This code works great and gives the correct temperature throughout the sensors range in both Fahrenheit and Celsius. The format is like this: TempC = 2250 = 22.50ºC, TempF = 7250 = 72.50ºF.

    Code:
    ReadDS18S20:
        owout sensor,1,[$CC, $44]                       ' Send Start Temperature Conversion command
        owout sensor,1,[$CC, $BE]                       ' Send Read Temperature command
        owin sensor,0,[STR dq\9]                        ' Retrieve all 9 bytes of data
        RawTemp.Byte0 = dq[0]
        RawTemp.byte1 = dq[1]
        if RawTemp.8 = 1 then                           ' Check if temperature is a negative reading
            SignC = Negative
            RawTemp.lowbyte = RawTemp.lowbyte ^ 255     ' Invert data
            else
            SignC = Positive
    	endif
        dummy = RawTemp.0                               ' Store the half degree indicator bit
        TempC = ((RawTemp.lowbyte) >> 1) * 100          ' Divide raw data by 2 to give real temperature
        TempC = TempC + (dummy * 50)                    ' Add the half degree is present
        if SignC = Negative then                        ' Only proceed if temperature is negative
            if TempC => 1770 then   
                SignF = Negative
                TempF = (TempC + 5000) * 900
                TempF = div32 500
                TempF = TempF - 12200
                return 
                else
                SignF = Positive
                TempF = (TempC + 5000) * 900
                TempF = div32 500
                TempF = 12200 - TempF
                return 
            endif    
        endif
        SignF = Positive
        TempF = TempC * 18 / 10 + 3200
        return

  33. #33
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Default

    Jack

    The reason your PBP version did not work was because:

    You attempted to convert C to F using the RAW temp from the DS18S20.

    TEMP = ((TEMP*9/5)+32 )

    Then tried to calculate the temp from the results.

    Lcdout $fe, 1, dec (temp >> 1),".",dec(temp.0 * 5)," degrees C"

    For the fun of it lets plug some numbers in assume 27.5 C is the temp today.

    From the datasheet : The temperature sensor output has 9-bit resolution, which corresponds to 0.5°C steps.

    so 27.5*2 = 55

    Temp = (55*9/5) + 32 = 131
    So Temp now is equal to 131.

    Next we shift right 131>>1 same as temp/2 = 65.5

    Hmmm 65.535 does that number ring a bell?

    Wonder what would happen if you were to shift first then convert C to F like all of the other code posted does?

    temp = (temp>>1*9/5) +32 = (27.5*9/5)+32 = 81.5 F

    Hopefully this explains to you why it works in MikroBasic and not in PBP. The math is not broken, just the order of operation.

    That is why I said check math, simple change would fix it.

    Acetronic: I ask because you posted for jcleaver, would not take without asking first.
    Last edited by manwolf; - 24th July 2008 at 20:21.

  34. #34
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by manwolf View Post
    You attempted to convert C to F using the RAW temp from the DS18S20.
    TEMP = ((TEMP*9/5)+32 )
    Then tried to calculate the temp from the results.
    Lcdout $fe, 1, dec (temp >> 1),".",dec(temp.0 * 5)," degrees C"
    Nice find. I should've caught that the first time around.

  35. #35
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Red face

    Beginners luck.

  36. #36
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: ds18s20 reads incorectly

    I am trying to follow your code in an effort to better understand it, but I don't understand
    SignC = Positive
    Code:
    ReadDS18S20:
        owout sensor,1,[$CC, $44]                       ' Send Start Temperature Conversion command
        owout sensor,1,[$CC, $BE]                       ' Send Read Temperature command
        owin sensor,0,[STR dq\9]                        ' Retrieve all 9 bytes of data
        RawTemp.Byte0 = dq[0]
        RawTemp.byte1 = dq[1]
        if RawTemp.8 = 1 then                           ' Check if temperature is a negative reading
            SignC = Negative
            RawTemp.lowbyte = RawTemp.lowbyte ^ 255     ' Invert data
            else
            SignC = Positive
    	endif
        dummy = RawTemp.0                               ' Store the half degree indicator bit
        TempC = ((RawTemp.lowbyte) >> 1) * 100          ' Divide raw data by 2 to give real temperature
        TempC = TempC + (dummy * 50)                    ' Add the half degree is present
        if SignC = Negative then                        ' Only proceed if temperature is negative
            if TempC => 1770 then   
                SignF = Negative
                TempF = (TempC + 5000) * 900
                TempF = div32 500
                TempF = TempF - 12200
                return 
                else
                SignF = Positive
                TempF = (TempC + 5000) * 900
                TempF = div32 500
                TempF = 12200 - TempF
                return 
            endif    
        endif
        SignF = Positive
        TempF = TempC * 18 / 10 + 3200
    return
    It would help if you shared the declarations of the variables used.
    sensor equates to the hardware pin connected to the sensor?
    RawTemp is a word?
    dq is an array of 9?
    dummy is a byte?
    TempC is a byte?
    TempF is a byte?

    I don't get SignC or SignF and how you can equate to 'Positive' or 'Negative', I couldn't find a reference in the PBP manual.
    Explanation would be appreciated.
    Regards,
    Steve
    Last edited by ecoli-557; - 9th May 2014 at 16:52. Reason: put in the code
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  37. #37
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: ds18s20 reads incorectly

    I don't get SignC or SignF and how you can equate to 'Positive' or 'Negative', I couldn't find a reference in the PBP manual.
    Explanation would be appreciated
    For positive temperatures the output increases from 0000h as the temperature increases from 0 deg C.
    For negative temperatures the output decreases from FFFFh as the temperature falls below -0.5 deg C.

    To check for negative any output bit can be checked for 1, when that bit is outside the temperature range of the sensor. The data sheet says

    The sign bit (S) indicates if the value is positive or negative: for positive numbers S = 0 and for negative numbers S = 1.

    In the program RawTemp.8 is chosen as the sign bit.

  38. #38
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: ds18s20 reads incorectly

    Thanks EarlyBird2, I did figure that part out. In the code that the 'Kid' supplied, I could not get it to work until I remarked the SignC=Negative portion.
    The code below works, but I have not tried for negative numbers yet..... Could you explain why?
    Thanks and Regards,
    Steve

    Code:
    ReadDS18S20:' Works, mostly.....------------------------------------------------
                      owout Tsensor,1,[$CC, $44]                                    'Send Start Temperature Conversion command
                      owout Tsensor,1,[$CC, $BE]                                    'Send Read Temperature command
                      owin Tsensor,0,[STR dq\9]                                     'Retrieve all 9 bytes of data
                      RawTemp.Byte0 = dq[0]
                      RawTemp.byte1 = dq[1]
                      if RawTemp.8 = 1 then                                         'Check if temperature is a negative reading
                         SignC = Negative                                            
                         RawTemp.lowbyte = RawTemp.lowbyte ^ 255                    'Invert data
                      else
                         SignC = Positive
                      endif
                      dummy = RawTemp.0                                             'Store the half degree indicator bit
                      TempC = ((RawTemp.lowbyte) >> 1) * 100                        'Divide raw data by 2 to give real temperature
                      TempC = TempC + (dummy * 50)                                  'Add the half degree is present
    '                 if SignC = Negative then                                      'Only proceed if temperature is negative - doesnt work for F, fine for C
    '                    if TempC => 1770 then   
    '                       SignF = Negative
    '                       TempF = (TempC + 5000) * 900
    '                       TempF = div32 500
    '                       TempF = TempF - 12200
    '                    return 
    '                  else
    '                      SignF = Positive
    '                      TempF = (TempC + 5000) * 900
    '                      TempF = div32 500
    '                      TempF = 12200 - TempF
    '                      return 
    '                   endif    
    '                 endif
                      SignF = Positive                                              'Sign is +
                      TempF = ((TempC * 18) / 10) + 3200                            'Convert from C to F
                      return
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  39. #39
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: ds18s20 reads incorectly

    Negative and Positive are declared variables or constants. In your code should be

    Negative CON 1
    Positive CON 0

    Does this help?

    Could be SYMBOL or ALIAS of course.

  40. #40
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: ds18s20 reads incorectly

    EarlyBird2-
    Thanks! I just tried it (I did NOT have them as CONstants), compiles fine but still gives me 0.00F.
    Code is below for clarification.
    Regards,
    Steve

    Code:
        Positive    con 0                                                           'Bit used for + in the DS18S20
        Negative    con 1                                                           'Bit used for - in the DS18S20
        rawtemp         var word                                                    'Raw temp var
        tempC           var word                                                    'Temp var for degrees C
        tempF           var word                                                    'Temp var for degrees F
        dq              var byte[9]                                                 'String of data from DS18S20
        SignC           var bit                                                     'Bit for + or - for degrees C
        SignF           var bit                                                     'Bit for + or - for degrees F
        dummy           var bit                                                     'Temp var
    
    ReadDS18S20:' Works, mostly.....--Thanks 'Kid' from Forum-----------------------
                      owout Tsensor,1,[$CC, $44]                                    'Send Start Temperature Conversion command
                      owout Tsensor,1,[$CC, $BE]                                    'Send Read Temperature command
                      owin Tsensor,0,[STR dq\9]                                     'Retrieve all 9 bytes of data
                      RawTemp.Byte0 = dq[0]
                      RawTemp.byte1 = dq[1]
                      if RawTemp.8 = 1 then                                         'Check if temperature is a negative reading
                         SignC = Negative                                            
                         RawTemp.lowbyte = RawTemp.lowbyte ^ 255                    'Invert data
                      else
                         SignC = Positive
                      endif
                      dummy = RawTemp.0                                             'Store the half degree indicator bit
                      TempC = ((RawTemp.lowbyte) >> 1) * 100                        'Divide raw data by 2 to give real temperature
                      TempC = TempC + (dummy * 50)                                  'Add the half degree is present
                     if SignC = Negative then                                      'Only proceed if temperature is negative - doesnt work for F, fine for C
                        if TempC => 1770 then   
                           SignF = Negative
                           TempF = (TempC + 5000) * 900
                           TempF = div32 500
                           TempF = TempF - 12200
                        return 
                      else
                          SignF = Positive
                          TempF = (TempC + 5000) * 900
                          TempF = div32 500
                          TempF = 12200 - TempF
                          return 
                       endif    
                     endif
    '                  SignF = Positive                                              'Sign is +
    '                  TempF = ((TempC * 18) / 10) + 3200                            'Convert from C to F
                      return
    This what I have that does work, just not for negatives.....

    Code:
    ReadDS18S20:' Works, mostly.....--Thanks 'Kid' from Forum-----------------------
                      owout Tsensor,1,[$CC, $44]                                    'Send Start Temperature Conversion command
                      owout Tsensor,1,[$CC, $BE]                                    'Send Read Temperature command
                      owin Tsensor,0,[STR dq\9]                                     'Retrieve all 9 bytes of data
                      RawTemp.Byte0 = dq[0]
                      RawTemp.byte1 = dq[1]
                      if RawTemp.8 = 1 then                                         'Check if temperature is a negative reading
                         SignC = Negative                                            
                         RawTemp.lowbyte = RawTemp.lowbyte ^ 255                    'Invert data
                      else
                         SignC = Positive
                      endif
                      dummy = RawTemp.0                                             'Store the half degree indicator bit
                      TempC = ((RawTemp.lowbyte) >> 1) * 100                        'Divide raw data by 2 to give real temperature
                      TempC = TempC + (dummy * 50)                                  'Add the half degree is present
    '                 if SignC = Negative then                                      'Only proceed if temperature is negative - doesnt work for F, fine for C
    '                    if TempC => 1770 then   
    '                       SignF = Negative
    '                       TempF = (TempC + 5000) * 900
    '                       TempF = div32 500
    '                       TempF = TempF - 12200
    '                    return 
    '                  else
    '                      SignF = Positive
    '                      TempF = (TempC + 5000) * 900
    '                      TempF = div32 500
    '                      TempF = 12200 - TempF
    '                      return 
    '                   endif    
    '                 endif
                      SignF = Positive                                              'Sign is +
                      TempF = ((TempC * 18) / 10) + 3200                            'Convert from C to F
                      return
    Regards,
    Steve
    Last edited by ecoli-557; - 22nd May 2014 at 23:13. Reason: Added what does work
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

Similar Threads

  1. DS18B20 VS DS18S20 & multiple
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 7th February 2008, 22:43
  2. Using A Ds18s20 And Servo
    By CrazyCooter in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th September 2007, 09:41
  3. DS18S20 displays wrong temperatures
    By Shozo Kanamori in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th August 2007, 03:18
  4. DS18s20 - negative teperature
    By Ceug2005 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd February 2005, 12:40
  5. 16F877, DS18S20 and Serial Comm Problem
    By YellowTang in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 26th April 2004, 10:36

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts