This code works great for reading 5 sensors however I want to decrease the amount of time it takes to run through this subroutine by not trying to read a sensor if its not being used. My problem starts with the line "if sensor = 1 then" because it thinks I'm trying to compare the value stored in sensor which is not what I want it to do. I need it to compare the port associated with the number against a specific value. Any ideas on how to resolve this issue?
Code:
read_sensors:
for sensor = 0 to 4
temp_c = 0
temp_f = 0
temp_k = 0
dummy = 0
dq = 0
gosub ds_init
gosub read_temp
sign_c_array[sensor] = sign_c
sign_f_array[sensor] = sign_f
temp_c_array[sensor] = temp_c
temp_f_array[sensor] = temp_f
temp_k_array[sensor] = temp_k
next sensor
ds_init:
low sensor ' Set the data pin low to initialize
pauseus 500 ' Wait for more than 480us
input sensor ' Make data pin port an input
pauseus 100 ' Wait for more than 60us
if sensor = 1 then ' Is there a DS18B20 detected
debug cmd,line3,"Not Present" ' If not set flag indicating so
SensorMissing = 1
return
endif
pauseus 400 ' Wait for end of presence pulse
debug cmd,line3,"Present " ' Set flag indicating there is a
SensorMissing = 0 ' sensor present
return
read_temp:
if SensorMissing = 1 then ' If the sensor is not present load
temp_c = 32767 ' these values in place of the
temp_f = 62181 ' temperature indicating there is
temp_k = 60082 ' a problem with the sensor
sign_c = "+"
sign_f = "+"
return
endif
owout sensor,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 sensor,1,[$CC,$BE] ' Send read scratch pad command
owin sensor,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 get_crc ' Calculate the CRC of the data
gosub convert_temp ' Convert raw data into real temps
return
Bookmarks