Reassigning I/O within the program ?


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    24

    Question Reassigning I/O within the program ?

    I would like to reassign I/O pins to specific variables based on the state of an input pin. Can I do this? e.g.

    If Input = 1 then
    Variable1 = PORTB.1
    else
    Variable1 = PORTB.2
    endif

    But the above doesn't work. Is there another way? I would like to do this at the beginning of the program, just once.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    You could use PIN numbers instead of PORT.bit.


    Code:
    InputSelect  VAR PORTB.0
    myPIN        VAR BYTE
    
    Test:
        IF InputSelect = 1 THEN
            myPIN = 1            ; PORTB.1 selected
        ELSE
            myPIN = 2            ; PORTB.2 selected
        ENDIF
    
        HIGH myPIN
        PAUSE 500
        LOW myPIN
        PAUSE 500
    GOTO Test
    Or, a similar version.
    The Inputselect will always read 0 or 1. Add 1 to it to make 1 or 2, for PORTB.1 and PORTB.2.
    Code:
    InputSelect  VAR PORTB.0
    myPIN        VAR BYTE
    
    Test:
        myPIN = InputSelect + 1
    
        HIGH myPIN
        PAUSE 500
        LOW myPIN
        PAUSE 500
    GOTO Test
    The ports used depends on the chip you are using.
    Check the .bas file for the PIC you are using in the PBP folder. (ex. 16F877a.bas)

    PORTL and TRISL determine which PORT that pin numbers 0-7 go to.
    PORTH and TRISH determine which PORT that pin numbers 8-15 go to.
    <br><br>
    Last edited by Darrel Taylor; - 6th June 2010 at 01:23. Reason: Forgot PIN was a reserved word, changed to myPIN
    DT

  3. #3
    Join Date
    Nov 2007
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Yes, there are many ways to achieve this within the loop of the main program. I was trying avoid having to make pin decisions every time I wanted to collect data. I just want to reassign the pins at the beginning of the program, and never worry about it again (i.e. less code).

    Having thought about this for awhile now, I think the easiest way is to just make a subroutine for each data input (e.g. Get_Data) that makes the I/O pin decision each time I need the data. It still gobbles up a bunch of code, but is still relatively easy to follow the logic.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Del Tapparo View Post
    Yes, there are many ways to achieve this within the loop of the main program. I was trying avoid having to make pin decisions every time I wanted to collect data. I just want to reassign the pins at the beginning of the program, and never worry about it again (i.e. less code).
    I don't see a difference there.

    If you set the PIN variable at the top of the program instead of in the main loop, it's the same thing.

    Best regards,
    DT

  5. #5
    Join Date
    Nov 2007
    Posts
    24


    Did you find this post helpful? Yes | No

    Thumbs up

    I get it now ....

    In my case, all of the reassigned inputs use the PULSIN command. I simply assign a variable to the pin portion of the PULSIN command, separate from the resulting input data variable.

    Thanks Darrel! Much appreciated.

  6. #6
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    I don't like needing to check the compiler files for information, because changing PICs can be a hassle, especially when you forget about it.

    When I did it (years ago) I had to do something like this...
    myPIN = 2 ...set this in your IF statements
    low portb.0[myPIN] ...allows you to use any port you like, and changing PICs is easy.

Members who have read this thread : 0

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