Port Variables act like Constants


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    219

    Default Port Variables act like Constants

    My project has both USB and RS485 com ports. I would like to switch between active com ports by using a hardware dip switch. This would save a lot of code and time.

    Code:
       DS1       VAR PortA.1   ;dip switch 
       TX_USB    VAR PortC.6
       TX_RS485  VAR PortC.5
       ComVar    VAR BYTE
        If  DS1=1 then
    	ComVar=TX_USB
    	Else
    	ComVar=TX_RS485
        EndIF
        Serout1 ComVar, 84 [“Transmitting “]
    OK, so we know this will not work. Melanie explained why in this post http://www.picbasic.co.uk/forum/showthread.php?t=11801 but stopped short of a solution to the problem, if there is one. I’ve found several posts on this subject, none of which find solutions.
    So how do I set a Port Variable within a Serout statement to switch between two ports?
    Code:
        If DS1=1 then
    	ComVar=6
    	Else
    	ComVar=5
        EndIF
        If ComVar=7 then
    	Serout2 PortC.0[ComVar], 84, [“  Com is USB”]
    	Else
    	Serout2 PortC.0[ComVar], 84, [“  Com is RS485”]
        EndIF
    This compiles but it doesn’t work either, Is it for the same reasons the first example failed? There must be some way to solve this problem ??? There must be a different approach that I just haven’t found. Comments please.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Port Variables act like Constants

    Hi MOUNTAIN747.. why not just use GOTO statements? I think it's easier to do...
    Code:
            DS1       VAR PortA.1   ;dip switch 
                     
            start:  
             If  DS1=1 then
    	   goto tx_usb
             Else
    	  goto tx_rs485
             EndIF
    
            tx_usb:
              Serout portc.6, 84 ["Transmitting USB"]
              goto start
       
            tx_rs485:
              Serout2 portc.5, 84 ["Transmitting RS485"]
              goto start
    Adding PAUSE's can help...

  3. #3
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Port Variables act like Constants

    Thank you for your reply Olan,
    The program has to print out 12 or more different messages when it uploads 30 days of stored data. I’ve run out of EEprom so the messages are at a high cost in code space. If I can use only one message for both USB, which is the primary port, and RS485, which is used only occasionally, it will save 1800 words of code space.

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


    Did you find this post helpful? Yes | No

    Default Re: Port Variables act like Constants

    Look in the .bas file for the chip you are using.
    If it's a 16F877A then open c:\pbp\16f877a.bas

    The PORTL and PORTH aliases are used for the PIN numbers (0-15)
    PORTL is 0-7 and PORTH is 8-15.
    Code:
    PORTL   VAR     PORTB
    PORTH   VAR     PORTC
    TRISL   VAR     TRISB
    TRISH   VAR     TRISC
    If PORTH is aliased to PORTC then you don't need to do anything.
    If it's not, change it to PORTC, and change TRISH to alias TRISC.

    Then you can use the PIN numbers to access the pins with a variable.
    Code:
        If DS1=1 then
    	ComVar=14   ; PORTC.6
    	Else
    	ComVar=13   ; PORTC.5
        EndIF
    
        Serout2 ComVar, 84, [“  Com is PIN - ”, DEC ComVar,13,10]
    DT

  5. #5
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Port Variables act like Constants

    Darrel,
    Thanks again, you make it look so easy! After two days of working with this I now know I was searching for the wrong topics. I haven’t found anything about the .bas file in the manual. I think this subject deserves a WiKi along side your other documents.

    By the way, to those who had a hand in creating the Wiki and those who maintain it, ITS GREAT!

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