Code:
DEFINE LOADER_USED 1DEFINE OSC 16
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR 1
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H
@ __CONFIG _CONFIG2L, _BOR_ON_2L & _PWRT_ON_2L & _BORV_45_2L
@ __CONFIG _CONFIG2H, _WDT_ON_2H
@ __CONFIG _CONFIG4L, _STVR_ON_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
data_bit var bit ' Bit shift variable if not using lookup table
sensor_error var bit ' Sensor CRC error indicator flag
sign_c var byte ' +/- Sign for celcius temperature
sign_f var byte ' +/- Sign for fahrenheit temperature
dummy var word ' Dummy variable for DIV32 function
x var byte ' Loop counter
i var byte ' Loop counter
byte_in var byte ' Temporary holder for byte received from DS18B20
data_byte var byte ' Byte to perform CRC calculations on
dq var byte[9] ' 9 Byte Arrray for DS18B20
crc_calc var byte ' Calculated CRC value
neg_bit var raw_temp.Bit11 ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
raw_temp var word ' Raw Temperature
temp_c var word ' Temperature in ºC
temp_f var word ' Temperature in ºF
temp_k var word
sign_c_array var byte[5]
sign_f_array var byte[5]
temp_c_array var word[5]
temp_f_array var word[5]
temp_k_array var word[5]
crc_error var byte ' Loop counter for CRC errors
a var byte
clear
main:
for a = 0 to 4
temp_c = 0
temp_f = 0
temp_k = 0
dummy = 0
dq = 0
gosub read_temp
sign_c_array[a] = sign_c
sign_f_array[a] = sign_f
temp_c_array[a] = temp_c
temp_f_array[a] = temp_f
temp_k_array[a] = temp_k
next a
gosub display
goto main
read_temp:
owout a,1,[$CC,$44] ' Send start temperature conversion command
low portb.5 ' Turn on transistor between rail and data pin
pause 750 ' Allow enough time to process Tconv
high portb.5 ' Turn off transistor between rail and data pin
owout a,1,[$CC,$BE] ' Send read scratch pad command
owin a,0,[STR dq\9] ' Read all 9 bytes and store them in dq array
raw_temp.Byte0 = dq[0] ' Isolate raw temperature from the rest
raw_temp.Byte1 = dq[1]
gosub convert_temp ' Convert raw data into real temperatures
return
convert_temp:
if neg_bit = 1 then below_zero ' If below 0ºC then goto different subroutine
sign_c = "+" ' Display + symbol for negative temp
sign_f = "+" ' Display + symbol for positive temperature
dummy = 625 * raw_temp ' Multiply to load internal register with 32-bit value
temp_c = DIV32 100 ' Divide internal register by 10 to calculate precision ºC
dummy = 0
dummy = 1125 * raw_temp ' Multiply to load internal register with 32-bit value
temp_f = DIV32 100 ' Make it manageable
temp_f = temp_f + 3200 ' Make it into ºF
gosub kelvin
return
below_zero:
sign_c = "-" ' Display - symbol for negative temperature
sign_f = "+" ' Display + symbol for positive temperature
dummy = 0
raw_temp.byte0 = raw_temp.byte0 ^ 255
raw_temp.Byte1 = raw_temp.byte1 ^ 255
dummy = 625 * raw_temp + 1 ' Multiply inversion to load internal register with 32-bit value
temp_c = DIV32 100 ' Divide internal register by 100 to calculate precision ºC
temp_f = (temp_c + 5000) * 900 ' Multiply to load interal register with 32-bit value
temp_f = DIV32 500 ' Divide internal register by 500
if raw_temp >= 285 then ' Check if temperature is + or - ºF
temp_f = temp_f - 12200 ' Process if temperature is to be negative
sign_f = "-" ' Display a - symbol for negative temperature
else
temp_f = 12200 - temp_f ' Process if temperature is to be positive
sign_f = "+" ' Display + symbol for a positive temperature
endif
gosub kelvin
return
display:
hserout [sign_c_array[0],dec3 temp_c_array[0]/100,".",dec2 temp_c_array[0]//100,186,"C ",sign_f_array[0],dec3 temp_f_array[0]/100,".",dec2 temp_f_array[0]//100,186,"F +",dec3 temp_k_array[0]/100,".",dec2 temp_k_array[0]//100," K",10]
hserout [sign_c_array[1],dec3 temp_c_array[1]/100,".",dec2 temp_c_array[0]//100,186,"C ",sign_f_array[1],dec3 temp_f_array[1]/100,".",dec2 temp_f_array[1]//100,186,"F +",dec3 temp_k_array[1]/100,".",dec2 temp_k_array[1]//100," K",10]
hserout [sign_c_array[2],dec3 temp_c_array[2]/100,".",dec2 temp_c_array[0]//100,186,"C ",sign_f_array[2],dec3 temp_f_array[2]/100,".",dec2 temp_f_array[2]//100,186,"F +",dec3 temp_k_array[2]/100,".",dec2 temp_k_array[2]//100," K",10]
hserout [sign_c_array[3],dec3 temp_c_array[3]/100,".",dec2 temp_c_array[0]//100,186,"C ",sign_f_array[3],dec3 temp_f_array[3]/100,".",dec2 temp_f_array[3]//100,186,"F +",dec3 temp_k_array[3]/100,".",dec2 temp_k_array[3]//100," K",10]
hserout [sign_c_array[4],dec3 temp_c_array[4]/100,".",dec2 temp_c_array[0]//100,186,"C ",sign_f_array[4],dec3 temp_f_array[4]/100,".",dec2 temp_f_array[4]//100,186,"F +",dec3 temp_k_array[4]/100,".",dec2 temp_k_array[4]//100," K",10,10]
return
kelvin:
if sign_c = "-" then
temp_k = 27315 - temp_C
else
temp_k = temp_c + 27315
endif
return
END
Bookmarks