Returning whole numbers for DS1820?


Closed Thread
Results 1 to 7 of 7
  1. #1
    jessey's Avatar
    jessey Guest

    Default Returning whole numbers for DS1820?

    Hello,

    I'm using an example code for a DS1820 that does the math for the display to show either Fahrenheit or Celsius to 2 decimal places. I don't understand the math that's done here and was wondering if anyone could suggest a way to do the conversion without the decimal places.

    This works fine and isn't a problem in my programs but I'd like to use Darrel Taylor's "Temp_Convert" include file so I can display negative temperatures and I think the math here is screwing up the conversion in the include file. In his example code "Test_Temp_Convert" the temperatures are declared before the conversion with whole numbers so I thought if I could get whole numbers returned with the code I'm using below then the include file might work. Has anyone used Darrels "Temp_Convert" include file with a DS1820? I'd be grateful for any help.

    Thanks
    jessey

    Code:
    Update_Pond_Temperature: 
     OWOut Sensor_Input, 1, [$CC, $44]    ' Start Water_Temp conversion
    waitloop:
     OWIn Sensor_Input, 4, [count_remain] ' Check for still busy converting
     IF count_remain = 0 THEN waitloop
    
     OWOut Sensor_Input, 1, [$CC, $BE]    ' Read the Water_Temp
     OWIn Sensor_Input, 0, [Water_Temp.LOWBYTE, Water_Temp.HIGHBYTE, _
                            Skip 4, count_remain, count_per_c]
    
      IF Temp_Displayed = Celsius then
         ' Calculate Water_Temp in degrees C to 2 decimal places 
         Water_Temp = (((Water_Temp >> 1) * 100) - 25) _
                              + (((count_per_c - count_remain) * 100) / count_per_c)
       RETURN 
      ENDIF
     
      IF Temp_Displayed = Fahrenheit then
         ' Calculate Water_Temp in degrees C to 2 decimal places 
         Water_Temp = (((Water_Temp >> 1) * 100) - 25) _
                              + (((count_per_c - count_remain) * 100) / count_per_c)
         ' Convert Water_Temp from Celsius degrees to Fahrenheit to 2 decimal places 
         Water_Temp = (Water_Temp */ 461) + 3200
       RETURN 
      ENDIF
    Then to display I use this:
    Code:
      IF Temp_Displayed = Fahrenheit then 
        LCDOut $fe, $c0,"H2o=",DEC Water_Temp / 100,0,"F "  
      ENDIF
      IF Temp_Displayed = Celsius then   
        LCDOut $fe, $c0,"H2o=",DEC Water_Temp / 100,0,"C " 
      ENDIF

  2. #2
    Join Date
    Oct 2005
    Location
    Pinckney, Michigan
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    If I understand your question correctly, I think all you need to do is to shift right (not rotate right) the intial temperature result.

    On the DS18S20, each count of the 9-bit result is one-half degree Celsius. In other words the least significant bit is the half-degree "count". After shifting right once, this bit is "tossed" and the least significant bit will hold the whole-degree "count".

    Thereafter, you can process the result as an 8-bit whole number.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Jessey,

    Try this...
    http://www.picbasic.co.uk/forum/showthread.php?p=20470

    Should work with both Positive and Negative temperatures.
    <br>
    DT

  4. #4
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Thanks Darrel

    Hi Darrel,

    Are you suggesting that your code snip will work without using your "Temp_Convert" include file with my DS1820? I've tried it every which way with & without the include file and I can't seem to get it to work. Is there a sample on the list here that shows how its set up using a DS1820?

    Code:
    Print_To_2nd_Line_Of_Lcd:' ,".",DEC2 Water_Temp,
         Sign = TempC.15 
         TempC = ABS(TempC) 
         TempC =(TempC>>1)*10 + (TempC.0 * 5)
       IF Sign then
         IF Its = Dark THEN
           IF Temp_Displayed = Fahrenheit then 
             LCDOut $fe, $c0,1,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"F"  
           ENDIF
           IF Temp_Displayed = Celsius then   
             LCDOut $fe, $c0,1,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"C"
           ENDIF
         ELSE'if its light then show the print below
           IF Temp_Displayed = Fahrenheit then   
             LCDOut $fe, $c0,2,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"F"
           ENDIF 
           IF Temp_Displayed = Celsius then   
             LCDOut $fe, $c0,2,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"C"
           ENDIF
         ENDIF
       ELSE 
         IF Its = Dark THEN
           IF Temp_Displayed = Fahrenheit then 
             LCDOut $fe, $c0,1,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
                                                                "F ",DEC Lite_Level  
           ENDIF
           IF Temp_Displayed = Celsius then   
             LCDOut $fe, $c0,1,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
                                                                "C ",DEC Lite_Level
           ENDIF
         ELSE'if its light then show the print below
           IF Temp_Displayed = Fahrenheit then   
             LCDOut $fe, $c0,2,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
                                                                "F ",DEC Lite_Level
           ENDIF 
           IF Temp_Displayed = Celsius then   
             LCDOut $fe, $c0,2,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
                                                                "C ",DEC Lite_Level
           ENDIF
         ENDIF
       ENDIF
    RETURN
    This is how I read the temperature:
    Code:
    Update_Pond_Temperature: 
     OWOut Sensor_Input, 1, [$CC, $44]    ' Start Water_Temp conversion
    waitloop:
     OWIn Sensor_Input, 4, [count_remain] ' Check for still busy converting
     IF count_remain = 0 THEN waitloop
    
     OWOut Sensor_Input, 1, [$CC, $BE]    ' Read the Water_Temp
     OWIn Sensor_Input, 0, [Water_Temp.LOWBYTE, Water_Temp.HIGHBYTE, _
                            Skip 4, count_remain, count_per_c]
        ' Calculate Water_Temp in degrees C to 2 decimal places 
         Water_Temp = (((Water_Temp >> 1) * 100) - 25) _
                              + (((count_per_c - count_remain) * 100) / count_per_c)
    
      IF Temp_Displayed = Celsius then RETURN
    
     'IF the Temp_Displayed = Fahrenheit then Convert C to F before returning   
      Water_Temp = (Water_Temp */ 461) + 3200
    RETURN
    When the temperature drops below 32 degrees F then it displays 556 instead of 31. I'd surely appreciate any further help you could provide.

    Thanks
    jessey

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Oops, I should have looked closer. Didn't see you're using the extended resolution. That routine was for .5 deg resolution.

    OK, so when the temp drops below 32°F, it means that the °C is going negative. And negative values can't be multiplied or divided in PBP. So the "extended Resolution" formula gives the wrong answer.

    Same problem with the Fahrenheit conversion, And the LCDOUT statements.

    Here's what I think should work...(untested)

    After reading the DS1820, change the second half of Update_Pond_Temperature: to this...
    Code:
        ' Calculate Water_Temp in degrees C to 2 decimal places 
         Sign = Water_Temp.15
         Water_Temp = ((ABS(Water_Temp) >> 1) * 100)
         if Sign then Water_Temp = -Water_Temp
         Water_Temp = Water_Temp - 25 _
                         + (((count_per_c - count_remain) * 100) / count_per_c)
    
      IF Temp_Displayed = Celsius then RETURN
    
     'IF the Temp_Displayed = Fahrenheit then Convert C to F before returning   
      
         Sign = Water_Temp.15
         Water_Temp = (ABS(Water_Temp) */ 461)
         if Sign then Water_Temp = -Water_Temp
         Water_Temp = Water_Temp + 3200
    RETURN
    Then change the lcd routine to this...
    Code:
    Print_To_2nd_Line_Of_Lcd:
       LCDOut $fe, $c0
       IF Its = Dark THEN
           LCDOUT 1
       else 
           LCDOUT 2
       endif
       LCDOut "H2o = "
       if Water_Temp.15 then LCDOut "-"
       Water_Temp = ABS(Water_Temp)
       LCDOut DEC Water_Temp / 100,".",DEC2 Water_Temp,0
       IF Temp_Displayed = Fahrenheit then
           LCDOut "F "
       else
           lcdout "C "
       endif
       IF Its <> Dark THEN
           lcdout DEC Lite_Level
       endif
    RETURN
    You don't need the Temp_Convert include file.

    HTH,
    DT

  6. #6
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Thanks Darrel

    Hi Darrel,

    Looks like I owe you some more beer, your code works excellent. My Pond Pump Controller circuit works great now. Really appreciated.

    Thanks
    jessey

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Woohoo! It's like Beer in the bank.

    Now I just need to find a local Branch office so I can make a withdrawl.

    __________________
    DT

    P.S. That's gotta be one COLD pond in B.C. in February.
    <br>

Similar Threads

  1. Replies: 4
    Last Post: - 15th April 2009, 01:54
  2. Numbers and Letters After PIC Number
    By kiwipiper in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th October 2007, 09:13
  3. Working with 3 byte numbers
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th July 2007, 22:59
  4. Splitting numbers and recombining them(EEPROM Error)
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd June 2007, 06:40
  5. Storing numbers for caller ID?
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th March 2005, 10:27

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