Why? Temp conversion C to F code from Rentron (Bruce)


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Why? Temp conversion C to F code from Rentron (Bruce)

    The first IF condition in the original displays 1 more digit prior to the decimal point.
    Yes, I could see that in the original code, the first debug was there to display one more digit for temp>99.

    That is why I thought that I could remove the second "TempF = TempF + 3200".

    But it appears that I need to keep both of them in the code to get the correct temperature conversion??

    Or am I missing something?

    My goal is to have a section of code that will return degrees F for at least a range of say -20 F to +120 F (-30 to +50 C) I dont care about fractional digits. I am also trying to keep it small, as I am running out of room in my PIC. I don't need extreme accuracy +- 1 deg or so is ok.

    Here is my(Bruce's) complete subroutine which seems to work correctly:
    Code:
     
    '===========subroutine==========================
    GetTemp:
    OWOUT comm_pin, 1, [$CC, $4E, 0, 0, DS18B20_9bit] 'set resolution 9 bit Start_Convert:
    OWOUT Comm_Pin, 1, [$CC, $44] 'Skip ROM search & do temp conversion
     
    Wait_Up:
    OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
    IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!
    OWOUT Comm_Pin, 1, [$CC, $BE]
    OWIN Comm_Pin, 2, [R_Temp.Lowbyte, R_Temp.Highbyte] ' Read two bytes
    GOSUB Convert_Temp
    RETURN
    '================================================================================ 
    Convert_Temp: ' +32.0 to +257 F 
    IF Cold_Bit = Real_Cold THEN Yikes ' If Cold_Bit = 1, it's below "0" deg C
    Sign = "+"
    Dummy = 1125 * R_Temp
    TempF = DIV32 100
    IF TempF >6795 THEN ' Over 99.5 deg F..?
    TempF = TempF + 3200
    ELSE
    TempF = TempF + 3200
    ENDIF
    tempf=tempf/100 'keep only whole digits
    RETURN
    '-----------------------------------------------------------------------------
    Yikes: ' Display full range -C to -F conversion
    Sign = "-" ' Display - symbol for negative temp
    TempF = ~R_Temp / 16 ' Begin conversion from -C to deg +/-F
    IF TempF >=18 THEN ' Check for -degrees F "-18 C = -0.4 F"
    TempF = ((((TempF + 50) * 9) /5) -122) ' -C to -F below -17 deg C
    ELSE ' Else result = +deg F
    TempF = ((((-TempF + 50) * 9) /5) -58)
    ENDIF
    RETURN
    '==================================================
    But It seems that I still need both instances of... TempF = TempF + 3200
    and that is what is puzzling me.

    Isn't it the case that only one or the other will be executed? Yet if I remove one of them then I get the wrong temperature??

    Possibly(likely) I need to go back and do some more testing.


    Thanks


    Last edited by Heckler; - 17th May 2011 at 17:08.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Why? Temp conversion C to F code from Rentron (Bruce)

    ...

    Bruce first compares TempF to 6795, then he adds 3200 to complete the calculation.

    That's why it's in both sides of the IF.
    Last edited by Demon; - 17th May 2011 at 17:20.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  3. #3
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Why? Temp conversion C to F code from Rentron (Bruce)

    Robert,

    your version makes sense to me... and that is what I was trying to accomplish. Yet when I removed one (or took it outside of the IF/Then) it did'nt seem to work right.

    I will have to do some more testing tonight.
    (dang work is getting in the way right now)

    thanks


    PS. as I listed in an earlier post I am trying to tighten up my code. I know Bruces code is optimized to give accurate fractional degrees, which I don't need.

    If anybody has more compact code that only gives whole numbers I would be grateful. If anywhere I am weak in PIC Basic programming, it is in the Math area.

    Thanks
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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