View Full Version : ºC -> ºF Conversion
CocaColaKid
- 19th January 2005, 18:36
I've hit a road block in converting temperature from celcius to fahrenheit. I know:
ºF = (ºC*18)/10+32
This works fine for positve temperatures but not for negative. I discovered:
ºF = 32-((-ºC*18/10+32)-32)
This formula works fine until I get to the negative numbers in ºF. In ºC there is a bit set indicating that the number presented is a negative.
Any ideas would be greatly appreciated.
mister_e
- 19th January 2005, 19:07
what is the range of your positive and negative Temperature reading before converting ???
i mean positive : from x to y
negative : from a to b
I don't know what is your hardware setting but you can probably test the borrow bit and do some math with. Maybe the range will tell a better solution in this case.
CocaColaKid
- 19th January 2005, 19:17
I'm using the DS18S20 which is a celcius chip with a range or -50C - 125C.
Luciano
- 19th January 2005, 19:56
Hi!
See page 13 of the PDF.
From the Nuts and Volts magazine:
There’s A New Stamp In Town - Part 1
Column #68, December 2000 by Jon Williams:
PDF file:
http://www.parallax.com/dl/docs/cols/nv/vol2/col/nv68.pdf
Basic stamp code:
http://www.parallax.com/html_pages/downloads/nvcolumns/Nuts_Volts_Downloads_V2.asp
Best regards,
Luciano
* * *
In general using the calculator:
Convert -15 Celsius to Fahrenheit:
-15 * 9/5 = -135/5 = -27
-27 + 32 = 5 F
Convert -10 Celsius to Fahrenheit:
-10 * 9/5 = -90/5 = -18
-18 + 32 = 14F
Convert -273 Celsius to Fahrenheit:
-273 * 9/5 = -2457/5 = -491.4
-491.4 + 32 = -459.4F
languer
- 20th January 2005, 04:37
The following Maxim appnote shows some c-code to convert from the sign-extended two's complemented value:
temp_msb = get[1]; // Sign byte + lsbit
temp_lsb = get[0]; // Temp data plus lsb
if (temp_msb <= 0x80){temp_lsb = (temp_lsb/2);} // shift to get whole degree
temp_msb = temp_msb & 0x80; // mask all but the sign bit
if (temp_msb >= 0x80) {temp_lsb = (~temp_lsb)+1;} // twos complement
if (temp_msb >= 0x80) {temp_lsb = (temp_lsb/2);}// shift to get whole degree
if (temp_msb >= 0x80) {temp_lsb = ((-1)*temp_lsb);} // add sign bit
temp_c = temp_lsb; // ready for conversion to Fahrenheit
temp_f = (((int)temp_c)* 9)/5 + 32;
You may want to check this as well, http://www.rentron.com/PicBasic/one-wire2.htm, it's for PBP.
Ingvar
- 20th January 2005, 10:37
Since the DS1802 gives you temperature in half degrees, it would be easier to use the formula F = (C*9/10)+32. For negative numbers you need to convert the negative temperature to positive and reverse the formula F = 32-((ABS C)*9/10). Spotting negative numbers is easily done by looking at the highest bit. If it's set the number is negative.
TempC VAR WORD
TempF VAR WORD
IF TempC.15 THEN
TempF = 32-((ABS TempC)*9/10)
ELSE
TempF = (TempC*9/10)+32
ENDIF
You could use the ** operator to save the division. This operator makes a multiplication and an "invisible" division by 65536. 0.9 * 65536 = 58982.4, since we can only use integers we round that to 58983. Our calculation will look like......
IF TempC.15 THEN
TempF = 32-((ABS TempC)**58983)
ELSE
TempF = (TempC**58983)+32
ENDIF
/Ingvar
Darrel Taylor
- 15th March 2005, 09:42
For the EASIEST way to convert temperatures.
Try my Temperature Conversion (http://www.pbpgroup.com/modules/wfsection/article.php?articleid=18) routines for PBP.
It's an INCLUDE file that converts between Celsius, Fahrenheit and Kelvin.
HTH,
Darrel
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.