I feel your pain!! the math was WAY over my head also... luck was in my favor and I stumbled across an INCLUDE file by Darrel Taylor. His routine will convert between C, F, Kelvin and possibly more!!!
Here is my temperature conversion... after reading a DS18B20 one wire temp sensor.
bear in mind that this routine is for a One Wire DS18b20 temp sensor... and the value is a two BYTE value that can be programmed for 9-12 bit resolution and the value is initially read in deg C
Code:
'=======TEMPERATURE subroutine==============================================
ShowTemp:
while PortA.3=0 : wend
PAUSE 500
OWOUT Comm_Pin, 1, [$CC, $4E, 0, 0, DS18B20_9bit] 'set resolution of sensor
Start_Convert:
OWOUT Comm_Pin, 1, [$CC, $44]' Skip ROM search & do temp conversion
Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!
OWOUT Comm_Pin, 1, [$CC, $BE]' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [Raw.LOWBYTE, Raw.HIGHBYTE]' Read two bytes / end comms
Convert_Temp:
Sign="+"
IF Cold_Bit = 1 THEN 'it's below zero Celsius
C=(ABS Raw>>4)*-10 'so shift the ABS value right and mult X -10
ELSE
C=(Raw>>4)*10 'else shift value right and mult X 10
ENDIF
@ CtoF _C, _F ; Convert Celsius to Fahrenheit
'converted value will be X 10, ie. 756=75.6 deg F
'so devide by 10 to get whole degrees
IF f.15=1 THEN Sign="-" 'if converted value is below zero F then sign is "-"
TempF = (ABS f)/10 'take tha ABS value and /10
IF f//10 >4 THEN TempF=TempF+1 'check remainder, if >4 then round up
IF TempF <10 THEN '1 digit value plus "deg F"
len=3
elseif TempF <100 THEN '2 digit value plus "deg F"
len=4
ELSE '3 digit value plus "deg F"
len=5
ENDIF
arraywrite msg,[Sign,DEC TempF,"*"] ' *= lookup for "deg F"
GOSUB Message ' display current temp
if intrpt=1 then SleepNow 'if here by interrupt then return to SleepNow
IF mode=0 THEN Motto 'if here from motto return to motto
GOTO Icon 'else return to icon
Here are most of the variables associated with the temp conversion routine.
Code:
'-------------[variables and constants for temperature routine]--------------
DS18B20_9bit CON 011111 ' set resolution on DS18B20 93.75ms, 0.5 C
Comm_Pin VAR PortA.4 ' One-wire Data-Pin "DQ" on PortA.4
Busy VAR BIT ' Busy Status-Bit
Raw VAR WORD ' RAW Temperature readings
TempF VAR WORD ' Temp in deg F
Cold_Bit VAR Raw.BIT11 ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Sign VAR BYTE ' +/- sign for temp display
Don't forget to add this line to your program...
INCLUDE "Temp_Convert_CtoFonly.pbp"
also you will need to delete the .txt extension off from the attached include file.
I hope this helps... ask questions if you need to.
possibly most if what you need is Darrels (as always excellent) include file.
Bookmarks