Port Addressing Issue


Closed Thread
Results 1 to 8 of 8
  1. #1

    Default Port Addressing Issue

    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

  2. #2
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Question

    read_sensors:
    for sensor = 0 to 4
    Can you use a different variable name here? Just a quick thought.
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  3. #3


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    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?
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Bingo! That is exactly what I want

  6. #6
    Join Date
    Nov 2004
    Location
    Fribourg, Switzerland
    Posts
    9


    Did you find this post helpful? Yes | No

    Default

    Hi Cocacolakid

    Try this:

    PortB.0 [sensor]

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

    Hope this help.

    Hans

  7. #7


    Did you find this post helpful? Yes | No

    Default

    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

  8. #8
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Smile

    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?
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

Similar Threads

  1. Change On Interrupt, PIC16F884
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 14th November 2008, 17:25
  2. port config.
    By tamertokgoz in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th July 2008, 12:25
  3. Code Issue - select case or 'if' issue - not sure why
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 7th October 2007, 08:52
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. STATUS re-curtain W
    By zugvogel1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th February 2005, 15:21

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts