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




Bookmarks