Quick Routine to Convert deg C to deg F
John Couture
Posts: 370
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
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
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/
100 farenheit was his body temperature.
degF = degC * 9
degF = degF / 5
degF = degF + 32
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
"OEM NMEA GPS Module" Now available on ebay for only $17.49
http://www.allsurplus.net/Axiom/
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
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 Williams
Applications Engineer, Parallax
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
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/