1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Variable expansion with i/o ports?

Discussion in 'Oshonsoft' started by eTech, Jan 20, 2013.

  1. eTech

    eTech Active Member

    Joined:
    Apr 25, 2012
    Messages:
    561
    Likes:
    43
    Is there a way to use variable expansion (in PIC Basic) with i/o ports?

    In other words, is there a way to do something like this:

    Dim a as byte

    a = 1

    If PORTB.(a) = 1 then
    'do something
    Endif

    eT
     
  2. ronsimpson

    ronsimpson Well-Known Member Most Helpful Member

    Joined:
    Apr 17, 2007
    Messages:
    7,259
    Likes:
    961
    Location:
    Loveland, CO USA
    ONLINE
    I think it will work. I have not used pic basic recently. The answer probably is on the PIC Basic forum. Do you get an error when you try it?
     
  3. eTech

    eTech Active Member

    Joined:
    Apr 25, 2012
    Messages:
    561
    Likes:
    43
    yes.

    Error:
    "There has to be at least one variable in the test expression"

    I'm trying to write a procedure I can pass a value to flash an LED. The variable represents the port number.

    So now i'm trying this approach:

    Symbol led_1 = PORTC.1

    Call flash_led(led_1)

    Proc flash_led(ledno As Bit)
    If ledno = 0 Then
    High ledno
    WaitUs 100
    Low ledno
    WaitUs 100
    Else
    Low ledno
    WaitUs 100
    High ledno
    WaitUs 100
    Endif
    End Proc
     
    Last edited: Jan 20, 2013
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Mike_2545

    Mike_2545 Super Moderator

    Joined:
    Jan 4, 2009
    Messages:
    1,057
    Likes:
    29
    Location:
    Toledo, Ohio

    You need to :
    Include "bs2defs.bas"
     
  6. eTech

    eTech Active Member

    Joined:
    Apr 25, 2012
    Messages:
    561
    Likes:
    43
    Will this work with the Oshonsoft Basic?
     
  7. Mike_2545

    Mike_2545 Super Moderator

    Joined:
    Jan 4, 2009
    Messages:
    1,057
    Likes:
    29
    Location:
    Toledo, Ohio
    Pic Basic Pro
     
  8. eTech

    eTech Active Member

    Joined:
    Apr 25, 2012
    Messages:
    561
    Likes:
    43
    OK..I guess Oshonsoft Basic isn't as flexible as other versions.

    I can't do this:

    Dim i As Byte
    For i = 0 To 7 'loop for 8 counts
    PORTB = %00000000 'make all pins low
    PORTB.0 = 1 'make pin number i high <----compiler errors "Constant Arg cannot be used"
    WaitMs 500 'pause for half a second
    Next i

    I need to be able to reference I/O pins with a variable.

    Guess I'll look for a different compiler...

    Thanks

    eT
     
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    This demo works on Oshonsoft

    Code (text):
    Dim i As Byte
    Dim p As Byte

    TRISB = 0x00

    loop1:

    PORTB.0 = 1
    WaitMs 10''''''''''''''''''''added
    For i = 0 To 7  'loop for 8 counts
    PORTB = ShiftLeft(PORTB, 1)
    WaitMs 10
    Next i

    Goto loop1
     
    Last edited: Jan 21, 2013
  10. eTech

    eTech Active Member

    Joined:
    Apr 25, 2012
    Messages:
    561
    Likes:
    43
    OK...I'm not making the connection... :)

    I need to be able to reference an I/O pin using a variable as the pin identifier.

    I was hoping to be able to do this:

    If PORTB.(i) = 0 Then
    High PORTB.(i)
    Endif

    Is there a way to this?

    eT
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    You only need to write a simple procedure to manipulate a port..

    I just wrote a function to change any pin without altering any others

    Code (text):

    main:
        PORTB = writeport(LATB, 3, 1)
        PORTC = writeport(LATC, 2, 0)
        PORTB = writeport(LATB, 6, 1)
        PORTC = writeport(LATC, 1, 0)
    Goto main
    End                                              

    Function writeport(port As Byte, pin As Byte, condition As Byte) As Byte
    Dim idx As Byte
    Dim buf As Byte
        idx = LookUp(1, 2, 4, 8, 16, 32, 64, 128), pin  'POWER OF 2
        buf = port  'READ PORT ( LATCH )
        idx = Not idx  'INVERT ALL BUT PIN
        buf = buf And idx  'AND TO CLEAR BIT
        If condition = 0 Then  'IF PIN NEEDS CLEARING FIN HERE
                writeport = buf  'RETURN WITH ONLY THAT PIN CLEARED
                Exit
        Endif
        idx = Not idx  'OTHERWISE WE NEED TO
        buf = buf Or idx  'SET JUST THAT PIN
        writeport = buf ' ALL DONE
    End Function                
     
    Last edited: Jan 21, 2013

Share This Page