Converting temperature K to C maths not working

tecnition_ted

New Member
Hi all, I am trying to convert temperature K into temperature C, The formula is straight forward from what I understand K - 273.15 = C. I know the Picaxe cannot use decimal places so I tried these ways without success

Picaxe 08M2

Code:
setfreq m16
pause 8000

;Maths 1 to change K into C, W12 is where the temperature K is stored 
W12 = W12 *100
W12 = W12 -27315
W12 = W12  /100
serout c.0,N2400,("Temperature ",#W12,"C",CR,LF)


;Maths 2 to change K into C, W12 is where the temperature K is stored 
W12 = W12 -273
W12 = W12 * 100
W12 = W12 -15
W12 = W12 /100
serout c.0,N2400,("Temperature ",#W12,"C",CR,LF)
 

AllyCat

Senior Member
Hi,

Are you trying to convert NEGATIVE Celcius temperatures? PICaxe doesn't support negative values natively.

"Maths 1" is almost correct, if you put in 300K, you get a result of 26 C. The units digit is out, because of the "pointless" multiplication and division (since you are only displaying an integer result), so the "26.85" is being truncated to 26. Therefore, if you only need positive results then Maths1 is correct, provided that you also show the decimal (fractonal) part (or ignore the * and / 100).

For negative results, PICaxe can handle "Twos Complement" (negative) numbers for Addition, Subtraction and (sometimes) Multiplication, but NOT division. So an integer calculation (i.e. ignoring the 0.15 degrees) is not too difficult, BUT you must display the result correctly, i.e. it is the programmer's responsibility to include the negative sign, and to convert the (two's complement) number to a positive value to show on the display.

If you want to display negative C temperatures WITH a decimal point, then there's quite a lot to learn. ;)

Cheers, Alan.
 

geoff07

Senior Member
You can always work in units of 1/10 or 1/100 degree and just insert the ./, later.

to process (< 0) readtemp12 values into 1/10 degree:
readtemp12 DB18B20_pin, W_received_temp
W_received_temp = W_received_temp * 25/40 'convert raw DB18B20 readings to 1/10 deg
bintoascii W_received_temp,B_thousands,B_hundreds,B_tens,B_units,B_dec 'extract the digits
 
Top