Shop OBEX P1 Docs P2 Docs Learn Events
Quick Routine to Convert deg C to deg F — Parallax Forums

Quick Routine to Convert deg C to deg F

John CoutureJohn Couture Posts: 370
edited 2006-03-14 16:24 in General Discussion
Ok, everyone is tired of hearing from me today but one last post.

For those of you that are working with temperature sensors that have deg Celsius and need to convert to deg Fahrenheit I worked up a quick routine that uses only integers.

The real equation:

·· deg F = (deg C * 1.8 ) + 32
68 deg F = (20 deg C * 1.8) + 32
68 deg F = (36 + 32)

Well, 1.8 is difficult to work with so what I did was multiply it by 2 then subtracted out the 0.2 after multiplying it also.

degC = 20
x1·· = degC * 2· ' this gives you 40
x2·· = x1········' make a copy of it
x1·· = x1 / 10···' I know it looks goofy, but it works
x2·· = x2 - x1·· ' 40 - 4 = 36
degF = x2 + 32·· '·go ahead, try it in a spreadsheet, it works for all temps > 0 deg C that a human can tolerate.
···

Finally, for those of you into trivia:

If zero degrees Celsius is the melting point of ice, what does zero degrees Fahrenheit represent?· (don't post the answer, make them look it up!)


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John J. Couture

San Diego Miramar College

Comments

  • Mike CookMike Cook Posts: 829
    edited 2006-03-06 02:21

    John,

    Thanks for posting this. I was working on something similar to this today:

    ºF = (ºC * 1.8) + 32

    What I came up with is attached, prints a HEX value that is in ºF when divide by 10. Still not finished, too many projects!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
    "OEM NMEA GPS Module" Now available on ebay for only $17.49
    http://www.allsurplus.net/Axiom/

  • Jeff DegeJeff Dege Posts: 85
    edited 2006-03-06 05:05
    Zero farenheit was the lowest temperature that Farenheit could achieve in the lab - the freezing point of the salt water with the lowest freezing point of any concentration of salt that he tried.

    100 farenheit was his body temperature.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-03-06 05:35
    Why not?

    degF = degC * 9
    degF = degF / 5
    degF = degF + 32

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-06 09:07
    And if you're keeping everything as bytes and want to avoid roll-over...

    degF = degC * 4
    degF = degF / 5
    degF = degF + degC
    degF = degF + 32

    ... this assumes, of course, that everything is positive.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-03-06 11:18
    Okay, I know that you have to adjust by 5/9th [noparse][[/noparse]or its inverse of 1.8], then offset by 32

    But is there a niffy way in Binary to deal with that awful 5/9ths?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • Mike WMike W Posts: 105
    edited 2006-03-06 16:08
    I have also been working with tempreature projects. Where I have been stuck is when the tempreature drops below 0. since where on the subject how do you convert negative temps.



    Mike
  • Mike CookMike Cook Posts: 829
    edited 2006-03-06 16:26
    This is how I do it with the DS18s20, all readings are in ºC, in the process of writing a ºF routine.
    tLSB = OW_READ          ' read LSB byte of temperature from DS18s20
    tMSB = OW_READ          ' read MSB byte of temperature from DS18s20
     
    temp1 = tLSB >> 1       ' remove fraction
     
    IF tMSB.0 = 1 THEN      ' check for negative temperature ºC
      temp1 = temp1 | 128   ' OR 128 with the value for the 9th bit 
      temp1 = -temp1        ' two's compliment conversion
      TX_BYTE 45            ' set 'SIGN' of temperature to negative
    ELSE 
      TX_BYTE 43            ' set 'SIGN' of temperature to positive
    ENDIF
     
    TX_DEC3 temp1           ' send temperature value to terminal
     
    IF tLSB.0 = 1 THEN      ' Send fraction to terminal, 1 = .05 ºC
      TX_STRING ".5C"
    ELSE
      TX_STRING ".0C"
    ENDIF
     
    RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
    "OEM NMEA GPS Module" Now available on ebay for only $17.49
    http://www.allsurplus.net/Axiom/

  • John CoutureJohn Couture Posts: 370
    edited 2006-03-06 17:25
    Beau,

    Your routine of multiplying by 9 limits you to 28C when you use an 8 bit register (clever solution though)

    Jon,

    yours seems the easiest

    Mike Cook

    I actually do have a routine for bringing in the .5 but I wanted to keep it very simple (grin).

    Mike W.

    In the DS18S20 you download two bytes. The MSB (most significant byte) is the sign, the LSB is the degrees C (which includes the .5 deg bit). If the MSB = $FF then the reading is a neg number represented in two's complement. As for converting neg temps to deg F ... let met think about that for a couple of minutes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-03-06 17:51
    Jon Williams,

    Nice! ....I didn't think of just dealing with .8 and then adding for the 1.0 relationship.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-06 18:25
    I wish I could take credit -- I think Bean pointed that out to me when I was having trouble with the DS1620 in SX/B.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Mike CookMike Cook Posts: 829
    edited 2006-03-07 23:33
    Ok, this looks like it should work for temperatures +/- 63ºC to +/-ºF. Check my math, I've run it several times and can't seem to find anything wrong with it. All done in bytes.

    MAIN:
      sign = 0    
      for idx = 0 to 63
        degC = idx
        degF = degC * 4                     ' convert to Fahrenheit
        degF = degF / 5
        fraction = __REMAINDER
        degF = degF + degC
        fraction = fraction * 2             ' Double the fraction
        
        IF sign = 1 THEN
     
          IF degC = 0 THEN
            TX_BYTE Space
          ELSE
            TX_BYTE NEGATIVE 
          ENDIF   
          
          TX_DEC3 idx                      
          TX_STRING "  "
      
          IF degF >= 32 THEN
            degF = degF - 32
            TX_BYTE NEGATIVE
            TX_DEC3 degF
          ELSE
            INC degF
            degF = 32 - degF
     
            IF fraction = 0 THEN
              INC degF
            ELSE
              fraction = 10 - fraction 
            ENDIF
    
            TX_DEC3 degF
          ENDIF
    
        ELSE
          degF = degF + 32
          TX_DEC3 idx
          TX_STRING "  "
          TX_DEC3 degF
        ENDIF
     
        fraction = fraction + $30           ' add 30 HEX to it to make it ASCII
        TX_BYTE decimal                     ' print the decimal point
        TX_BYTE fraction                    ' print the fraction amount
        TX_STRING "F "                      ' print the units
        TX_STRING CrLf                      ' new line for the terminal
    
      NEXT
    
      
    Finish: 
      END         
    

    Attached is the SX/B program I used to test it. To go negative set the sign variable to 1

    <EDIT>

    I had an error in IF degF > 32 THEN should be IF degF >= 32 THEN fixed listing and uploaded test program

    ADDED: fraction amount from the degF = decF / 5 calculation

    CORRECTED: error in fraction between -ºC to +ºF

    </EDIT>

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
    "OEM NMEA GPS Module" Now available on ebay for only $17.49
    http://www.allsurplus.net/Axiom/

    Post Edited (Mike Cook) : 3/8/2006 11:22:37 PM GMT
  • Mike CookMike Cook Posts: 829
    edited 2006-03-14 16:24
    Attached is a test program written in SX/B to test the ºC to ºF conversion formula. Program works with byte values to output temperature in ºF in the range of: +146.3ºF to -82.3ºF, .9ºF resolution. This program tests a temperature conversion routines that I will be using for a future project. I'm posting it here to see if it might be helpful to others. If you find any problems with it or can think of a more efficient way of doing this then please post your comments.

    The temperature sensor I will be using is a Dallas 1-Wire DS18s20, for the final project. A quick look at the DS1620 looks like this device outputs temperature in the same format as the DS18S20 so it might be useable for that part also. Please note that the DS18s20 is a 1-Wire device and the DS1620 looks to be a SPI device, never used a DS1620!

    Data is output to a terminal program and then captured to a text file that was imported to Microsoft Excel. ºC is in column A and ºF is in column B. The temperature conversion formula is in column C (ºF = (ºC x 1.8) + 32). The check for Excel's PASS or FAIL formula =IF(B1=C1,"PASS","FAIL") is in column D.

    One abnormally that I ran across is Microsoft Excel fails -18ºC. Not sure why, but the converted value (SX/B = -.4ºF) and (Excel = -.4ºF) looks to be the same. Yet the Excel formula for the 'check' prints FAIL, I calling this a 'PASS' because the data looks to be the same (not a Excel guru).

    The following files are attached:

    C_TO_F__V3.SXB -- SX/B program
    sampledata1.txt -- capture of the ASCII output to terminal
    sampledaat1.xls -- Excel worksheet with captured data and check formula

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
    "OEM NMEA GPS Module" Now available on ebay for only $17.49
    http://www.allsurplus.net/Axiom/

Sign In or Register to comment.