After sleeping on this I figured out an easy way to accomplish this task. I just added a subroutine that gets the port state according to the value of "sensor". I works like a charm.
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
gosub direction
if SensorState = 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
direction:
if sensor = 0 then
SensorState = portb.0
return
endif
if sensor = 1 then
SensorState = portb.1
return
endif
if sensor = 2 then
SensorState = portb.2
return
endif
if sensor = 3 then
SensorState = portb.3
return
endif
if sensor = 4 then
SensorState = portb.4
return
endif
read_temp:
if SensorMissing = 1 then
temp_c = 32767
temp_f = 62181
temp_k = 60082
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