PDA

View Full Version : Port Addressing Issue



CocaColaKid
- 2nd November 2005, 14:56
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?


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

rhino
- 2nd November 2005, 15:03
read_sensors:
for sensor = 0 to 4
Can you use a different variable name here? Just a quick thought.

CocaColaKid
- 2nd November 2005, 20:06
The variable name will not matter. What I need to be is be able to step through portb.0 to portb.4 which works fine just using 0 to 4. The problem is that when "sensor" is equal to 0 the if then statement thinks that sensor is 0 and not 1 even though the port is being pulled high. As such when I do it for 2,3 and 4 the same thing. It looks at the number and not the state of the port.

rhino
- 2nd November 2005, 20:54
I see... don't think my code analogy will work, but what you are after is reading the state of the "portB.sensor" input, where sensor is the bit #... right?

CocaColaKid
- 2nd November 2005, 21:27
Bingo! That is exactly what I want

hmr
- 3rd November 2005, 12:10
Hi Cocacolakid

Try this:

PortB.0 [sensor]

If you need more info, read section 4.11 on the manual.

Hope this help.

Hans

CocaColaKid
- 3rd November 2005, 13:11
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.


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

rhino
- 3rd November 2005, 19:15
Glad to see you found a solution. I still think there should be a way to do it similar to what I was thinking, and Hans's idea. Any thoughts from the guru's?