Simple Farenheit to Celsius conversion

fernando_g

Senior Member
This simple degrees_F to degrees_C conversion for a 0 to 140F range, which should be plenty for the intended purpose, which is measuring a home's internal temperature.
Because of the PICAXE's integer math, there are some inevitable truncation errors However, for general purpose indoor temp measurements, it should pose no issues as over the stated range the error is either -0.2, +0.65C uncorrected, or -0.45, +0.5C when rounded off....

Additionally, since the degree F is roughly 1/2 of a degree C, decimals require to be displayed to not lose further resolution. However, because of the truncation errors, these decimals may give you a false sense of increased accuracy. As such, you can choose to round off the decimal portion to either 0 or 5. Comment or uncomment the appropriate lines in the program to do so. This will also yield lower errors at high temps as indicated previously. See attached plot for an error comparison.

The conversion algorithm known to HVAC professionals for many years (my late grandfather being one of those) is as follows:

1) Substract 32 from F value (above freezing) or F value from 32 (below freezing).
2) Add 10% (exact value is actually 11.11111%)
3) Divide by two
4) OPTIONAL Since one Farenheit is roughly 1/2 Celsius, roundoff
the decimal_C to either 0 or 5 to ameliorate truncation errors at high temps.

Code:
'simple degrees_F to degrees_C conversion for a 0 to 140F range
'Because of the PICAXE's integer math, there are some truncation errors but result will be 
'within +/-0.5C over the whole range. Good enough for general purpose indoor temp measurements..
'
'Interestingly, the largest error occurs exactly at 32F, and thus
'the program will set the Celsius value at exactly 0.0C.
'
'the  conversion algorithm is;
' 1) Substract 32 from F value (above freezing) or F value from 32 (below freezing).
' 2) Add 10% (should actually be 11.11111%) 
' 3) Divide by two
' 4) OPTIONAL Since one Farenheit is roughly 1/2 Celsius, roundoff 
'the decimal_C to either 0 or 5 to ameliorate truncation errors at high temps.

symbol deg_F = b0
symbol deg_C_whole = b1
symbol tempor1 = b2
symbol tempor2 = w2 'b4,b5
symbol deg_C_dec = b3
symbol polarity = b6

main:
readadc 0, deg_F  'to load a temperature value for testing purposes on simulator
'in a real program,this value may have come from some previous conversion.

select case deg_F	'for Celsius polarity, determine if it is above, below or at freezing.
case 0 to 31	'Below freezing, negative sign
let tempor1 = 32 - deg_F 'conversion step 1)
let polarity = "-"
case 32		'At freezing, no sign, and also correct truncation error
let deg_C_whole = 0
let deg_C_dec = 0
let polarity = " "
goto display_here
else
let tempor1 = deg_F - 32 'Above freezing, positive sign
let polarity = "+"
endselect

let tempor2 = tempor1 *10  'Conversion step 2), multiply by 10 to minimize truncation
let tempor2 = tempor1 + tempor2  'adding the two temporary values results in adding 10%
let tempor2 = tempor2 /2  'Conversion step 3)

let deg_C_whole = tempor2/10 
let tempor1 = tempor2 // 10

'Optional step 4). the following routine is to round-off and correct the decimal portion 
'of the deg_C; please comment the following 6 lines if no round-off is desired.
if tempor1 >= 5 then
let deg_C_whole = deg_C_whole +1
let deg_C_dec = 0
else 
let deg_C_dec = 5
endif

'the following line should be only made active (un-commented) if the 
'previous 6 have been commented and viceversa.
'let deg_C_dec = tempor1

'for example purposes, display both F and converted C.
display_here:
serout 0, n2400, (254,1)
serout 0, n2400, (254,128,#deg_F, "F, ",polarity, #deg_C_whole,".",#deg_C_dec, "C")
pause 1000
goto main
 

Attachments

Last edited:
Top