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

    I feel your pain!! the math was WAY over my head also... luck was in my favor and I stumbled across an INCLUDE file by Darrel Taylor. His routine will convert between C, F, Kelvin and possibly more!!!

    Here is my temperature conversion... after reading a DS18B20 one wire temp sensor.

    bear in mind that this routine is for a One Wire DS18b20 temp sensor... and the value is a two BYTE value that can be programmed for 9-12 bit resolution and the value is initially read in deg C


    Code:
    '=======TEMPERATURE subroutine==============================================
    ShowTemp:
        while PortA.3=0 : wend
        PAUSE 500
        OWOUT  Comm_Pin, 1, [$CC, $4E, 0, 0, DS18B20_9bit]  'set resolution of sensor
    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]' Skip ROM search & read scratchpad memory
        OWIN   Comm_Pin, 2, [Raw.LOWBYTE, Raw.HIGHBYTE]' Read two bytes / end comms
    Convert_Temp:
        Sign="+"
        IF Cold_Bit = 1 THEN    'it's below zero Celsius
        C=(ABS Raw>>4)*-10      'so shift the ABS value right and mult X -10
        ELSE
        C=(Raw>>4)*10           'else shift value right and mult X 10
        ENDIF
    
    @ CtoF _C, _F             ; Convert Celsius to Fahrenheit
            'converted value will be X 10, ie. 756=75.6 deg F
            'so devide by 10 to get whole degrees
    
        IF f.15=1 THEN Sign="-" 'if converted value is below zero F then sign is "-"
        TempF = (ABS f)/10      'take tha ABS value and /10
        IF f//10 >4 THEN TempF=TempF+1 'check remainder, if >4 then round up
    
        IF TempF <10 THEN         '1 digit value plus "deg F"
            len=3
        elseif TempF <100 THEN    '2 digit value plus "deg F"
            len=4
        ELSE                      '3 digit value plus "deg F"
            len=5
        ENDIF
        arraywrite msg,[Sign,DEC TempF,"*"] ' *= lookup for "deg F"
        GOSUB Message                       ' display current temp
    
    if intrpt=1 then SleepNow  'if here by interrupt then return to SleepNow
    IF mode=0 THEN Motto        'if here from motto return to motto 
    GOTO Icon                   'else return to icon
    Here are most of the variables associated with the temp conversion routine.

    Code:
    '-------------[variables and constants for temperature routine]--------------
    DS18B20_9bit  CON 011111   ' set resolution on DS18B20 93.75ms, 0.5 C
    Comm_Pin      VAR   PortA.4   ' One-wire Data-Pin "DQ" on PortA.4
    Busy          VAR BIT         ' Busy Status-Bit
    Raw           VAR   WORD      ' RAW Temperature readings
    TempF         VAR WORD        ' Temp in deg F
    Cold_Bit      VAR Raw.BIT11   ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
    Sign          VAR BYTE        ' +/- sign for temp display
    Don't forget to add this line to your program...

    INCLUDE "Temp_Convert_CtoFonly.pbp"

    also you will need to delete the .txt extension off from the attached include file.

    I hope this helps... ask questions if you need to.

    possibly most if what you need is Darrels (as always excellent) include file.
    Attached Files Attached Files
    Last edited by Heckler; - 23rd September 2011 at 02:59.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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

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

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

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

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

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

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

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