Shop OBEX P1 Docs P2 Docs Learn Events
Reading a port — Parallax Forums

Reading a port

Curtis BrooksCurtis Brooks Posts: 38
edited 2004-10-18 18:28 in BASIC Stamp
Is there a way to read the current state of an entire port?

For instance, if I have some of the port pins high and some low is there a way to read the state of the port into a variable?

Comments

  • WorapohtWorapoht Posts: 17
    edited 2004-10-17 16:49
    try this procedure:
    first check port direction from DIRx (x is port name) and seperated case by
    if DIRx = 0, current port is defined as input, check input on INx
    if DIRx = 1, current port is defined as ouput, check current output state on OUTx
  • Curtis BrooksCurtis Brooks Posts: 38
    edited 2004-10-17 18:40
    Okay. So could I do something like this:

    setportA VAR OUTA
    setportB VAR OUTB

    setportA = %1111
    setportB = %0000

    DEBUG setportA
    DEBUG setportB
    END

    Will debug display the current state of the port this way?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-10-18 03:52
    Yes. INS reads all 16 pins; INL reads pins 0-7, INH reads pins 8 - 15; INA is pins 0 - 3; INB is pins 4 - 7; INC is pins 8 - 11; and IND is pins 12 - 15.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • WorapohtWorapoht Posts: 17
    edited 2004-10-18 15:38
    Yes, you can do. just a little changed on debug command

    setportA VAR OUTA
    setportB VAR OUTB

    setportA = %1111
    setportB = %0000

    ' I'd added modifier to show on debug term as two lines 4 binary digits format.
    DEBUG BIN4 setportA,CR
    DEBUG BIN4 setportB
    END
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-10-18 18:28
    I thought you wanted to READ the state of a port. All the solutions presented so far WRITE out a port.

    To READ the state of a port, you have to set the port as an INPUT (PIC and BS2 gotcha).

    So:
    ReadPortA VAR INA ' Create alias
    ReadPortB VAR INB ' Create another alias
    MyVal VAR BYTE

    INPUT PORTA ' Set PortA bits as INPUT. See also the 'DIRA' variable.
    MAIN:
    MyVal = ReadPortA
    DEBUG BIN8 MyVal, CR
    PAUSE (100) ' 100 mSec, so we're not FLOODED with data...
    GOTO MAIN
Sign In or Register to comment.