ds18s20 reads incorectly


Closed Thread
Results 1 to 40 of 43

Hybrid View

  1. #1
    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!!!!!

  2. #2


    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

  3. #3
    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.

  4. #4
    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.

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


    Did you find this post helpful? Yes | No

    Red face

    Beginners luck.

  6. #6
    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

  7. #7
    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.

  8. #8
    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

  9. #9
    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.

  10. #10
    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 : 0

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