Temprature conversion --- C to F


Closed Thread
Results 1 to 14 of 14

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Here is a link to an article that I wrote that describes reading the ONE WIRE DS18b20 temp sensor...


    http://www.picbasic.co.uk/forum/cont...r-nine-of-them
    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 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Thank You Heckler!

    That looks like exactly what I need!
    I should have known that Darrel had already written something cool to cover it. (Thank You Darrel !!)

    I'll have a read through that stuff this evening and report back tomorrow.

  3. #3
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    F = C * 9 'DO LESS THE BIT.7 NEG/POS BIT
    F = F / 5
    IF T_MSB.7 = 1 THEN 'if the sign bit is "1", is minus C
    F = 32 - F
    else
    F = 32 + F
    ENDIF

    Norm

  4. #4
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Quote Originally Posted by Normnet View Post
    F = C * 9 'DO LESS THE BIT.7 NEG/POS BIT
    F = F / 5
    IF T_MSB.7 = 1 THEN 'if the sign bit is "1", is minus C
    F = 32 - F
    else
    F = 32 + F
    ENDIF

    Norm
    Try again:
    F = C * 9 'DO LESS THE NEG/POS BIT
    F = F / 5
    IF T_MSB.7 = 1 THEN 'if the sign bit is "1", is minus C
    F = F - 32
    else
    F = F + 32
    ENDIF

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


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Hey ByteButcher...
    bear in mind that my code does NOT result in an actual SIGNED value of the Temperature deg F. You end up with the correct temperature in the variable "tempF" and the sign of that value in the variable "sign". It is sutable for display on an LCD or something like that. But you will have to know the value of the "sign" if you want to do further calculations on the value.

    PS. I posted the wrong INCLUDE statement...
    YOU would use ...
    INCLUDE "Temp_Convert.pbp"
    (I tried to modify Darrels code to simplify it to only include the needed code to convert C to F and eliminate the Kelvin part. I was really tight on code space for my project.)


    There is some good informaiton about temperature conversion here (towards the bottom of the page)... http://www.rentron.com/PicBasic/one-wire2.htm

    @Normnet... is it really that simple to convert degrees C to degrees F? across the whole range of -55C to +125C ??
    Last edited by Heckler; - 23rd September 2011 at 15:36.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  6. #6
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Quote Originally Posted by Heckler View Post
    @Normnet... is it really that simple to convert degrees C to degrees F? across the whole range of -55C to +125C ??
    I was thinking on the run above but the following is complete.
    C value is * 1000 to hold 4 places.

    Code:
      C VAR LONG
      F VAR LONG
      iSIGN VAR BIT
    
      GOTO MAIN
    
      subC_TO_F:
        F = C * 9
        F = F / 5
        IF iSIGN = 1 THEN 'if the sign bit is "1" is minus C
          IF F > 320000 THEN
            F = F - 320000
            SEROUT2 sSEROUT_PIN,cBAUD,[" -",DEC C/10000,".",DEC4 C," C  = -",DEC F/10000,".",DEC4 F," F",13]
          ELSE
            F = 320000 - F
            SEROUT2 sSEROUT_PIN,cBAUD,[" -",DEC C/10000,".",DEC4 C," C  = ",DEC F/10000,".",DEC4 F," F",13]
          ENDIF
        ELSE
          F = F + 320000
          SEROUT2 sSEROUT_PIN,cBAUD,["  ",DEC C/10000,".",DEC4 C," C  = ",DEC F/10000,".",DEC4 F," F",13]
        ENDIF
      RETURN
    
      MAIN:
    
      SEROUT2 sSEROUT_PIN,cBAUD,[" ",13]
      SEROUT2 sSEROUT_PIN,cBAUD,["START",13]
    
      iSIGN = 1 'NEG
      FOR C = 100 TO 0 STEP -1 '-.0100 C TO 0 C
       GOSUB subC_TO_F
      NEXT
    
      iSIGN = 0 'POS
      FOR C = 0 TO 100 STEP 1 '0 C TO .0100 C
       GOSUB subC_TO_F
      NEXT
    
    
      iSIGN = 1
      FOR C = 550000 TO 0 STEP -10000 '-55.0000 C TO 0 C  'DO STEP -1 FOR COMPLETE BUT FOREVER
       GOSUB subC_TO_F
      NEXT
    
      iSIGN = 0
      FOR C = 0 TO 1250000 STEP 10000 '0 C TO 125.0000 C  'DO STEP 1 FOR COMPLETE BUT FOREVER
       GOSUB subC_TO_F
      NEXT
    
      STOP
    
      END
    Norm

  7. #7
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Now displays 4 decimals with round up.

    Norm

    Code:
    C VAR LONG
      F VAR LONG
      iSIGN VAR BIT
    
      GOTO MAIN
    
      subC_TO_F:
        F = C * 10
        F = F * 9
        F = F / 5
        IF iSIGN = 1 THEN 'if the sign bit is "1" is minus C
          IF F > 3200000 THEN
            F = F - 3200000
            F = F + 5
            F = F / 10
            SEROUT2 sSEROUT_PIN,cBAUD,[" -",DEC C/10000,".",DEC4 C," C  = -",DEC F/10000,".",DEC4 F," F",13]
          ELSE
            F = 3200000 - F
            F = F + 5
            F = F / 10
            SEROUT2 sSEROUT_PIN,cBAUD,[" -",DEC C/10000,".",DEC4 C," C  = ",DEC F/10000,".",DEC4 F," F",13]
          ENDIF
        ELSE
          F = F + 3200000
          F = F + 5
          F = F / 10
          SEROUT2 sSEROUT_PIN,cBAUD,["  ",DEC C/10000,".",DEC4 C," C  = ",DEC F/10000,".",DEC4 F," F",13]
        ENDIF
      RETURN
    
      MAIN:
      SEROUT2 sSEROUT_PIN,cBAUD,[" ",13]
      SEROUT2 sSEROUT_PIN,cBAUD,["START",13]
    
      iSIGN = 1
      FOR C = 100 TO 0 STEP -1 '-.0100 C TO 0 C
       GOSUB subC_TO_F
      NEXT
    
      iSIGN = 0
      FOR C = 0 TO 100 STEP 1 '0 C TO .0100 C
       GOSUB subC_TO_F
      NEXT
    
    
      iSIGN = 1
      FOR C = 550000 TO 0 STEP -10000 '-55.0000 C TO 0 C  'DO STEP -1 FOR COMPLETE BUT FOREVER
       GOSUB subC_TO_F
      NEXT
    
      iSIGN = 0
      FOR C = 0 TO 1250000 STEP 10000 '0 C TO 125.0000 C  'DO STEP 1 FOR COMPLETE BUT FOREVER
       GOSUB subC_TO_F
      NEXT
    
      STOP
    
      END

  8. #8
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: Temprature conversion --- C to F

    Well, I got lost in a world of fire wood this week and haven't had a chance to look through all these great suggestions. But THANKS to everyone for the input so far. Hopefully in the next day or 2 I'll get a chance to try again.

    Norm, you're killing me with those "Longs".... that's going to be tough to implement with my 16F887!

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