I think you've got several problems going on here.
First, is the 3.16V output from the device when it's "not 109°F".
Until you resolve the hardware issue, it will never work in software.
Next, while the A/D resolution is 8-bits, and adval doesn't need to be a WORD, the variable used to convert it to Fahrenheit (Ftemp) DOES need to be a WORD.
Also, by using 8-bit A/D, it limits the resolution of the final reading to +/- 3.5°F (horrible).
If you use 10-bit A/D it reduces that to about +/- 0.8°F (better).
Now the formula,
> The LM335 is calibrated as 10mv/°K.
So you have to convert the A/D reading to a voltage before you can convert it to a temperature.
And the math needs to be done differently to avoid overflows and to handle negative values.
Code:
DEFINE ADC_BITS 10
adval VAR WORD
TempF VAR WORD
Sign VAR BIT
ADCIN 0, adval 'Read channel 0 - Temperature
pause 20
adcin 3, adval2 'Read channel 3 - Battery Voltage
TempF = adval * 4887 ' Convert to Voltage
TempF = DIV32 1000 ' in millivolts (°K*10)
TempF = TempF -2730 ' Convert °K to °C
Sign = TempF.15 ' Convert °C to °F
TempF = ABS(TempF) * 9 / 5
if TempF DIG 0 > 5 then ' Round up to whole digit over .5
TempF = TempF + 10
endif
TempF = TempF / 10
if Sign = 1 then TempF = -TempF ' restore sign
TempF = TempF + 32
That will give it the full -50 to +150°C range (-58 to +302°F)
Since there are negative numbers, you'll also need to change the Temperature output routine...
Code:
IF TempF.15 = 1 then
dig1 = "-"
gosub outdigit
endif
for i = 2 to 0 step -1 '
dig1 = ABS(TempF) dig i 'Subroutine to send out :Temperature
gosub outdigit '
next i
Tested on a 16F88.
HTH,
Bookmarks