Shop OBEX P1 Docs P2 Docs Learn Events
reading ouputs — Parallax Forums

reading ouputs

ArchiverArchiver Posts: 46,084
edited 2003-01-09 17:53 in General Discussion
Hi All,

I have spent numerous hours trying to work out why the following code
does not dispaly the correct state of each output (off/on or
closed/open, or 1 or 0)

The code is designed to toggle outputs based on a keypad input. This
part works fine. I just can not seem to dispaly the current status of
a given output.

If I individually assign each output (i.e. out2, out6 etc), that
works. However, tagging a VAR onto out2 (i.e. out0(result) or outs
(result), does not seem to work.

Any suggestions would be greatly appreciated.

'{$STAMP BS2p}
'{$PORT COM1}

dirs=%1111111111111111 '0=input >> 1=output
bps con 240 '9600 Baudrate
dly con 20 'Global pause delay..
auxio 'use aux i/o
Rx con 13 'Serin (receive) pin 13

invar var byte 'key input varible
result var byte 'decoded key
pause 500
HIGH 11 'Set the pin high (Power LED on).

gosub reset 'reset lcd

loop:

auxio
serin Rx,bps,[noparse][[/noparse]invar]
if invar=13 then tgl 'if key 13 is pressed then toggle
output
if invar=8 then reset
invar=invar-48
if invar<=10 then keyscan
invar = invar-7

Keyscan:
LOOKUP invar, [noparse][[/noparse]48,55,56,57,52,53,54,49,50,51,65,66,67,68,35,42],result
mainio
if outs(result) = 0 then displayo
goto displayc

tgl:
debug "tgl",cr
mainio
toggle(result) 'Toggle output
if outs(result) = 0 then displayo 'read status of output, then
dislay
goto displayc
goto loop

displayc: 'display relay closed
serout 0,bps,[noparse][[/noparse]254,192]
pause dly
serout 0,bps,[noparse][[/noparse]"RLY ", (result), " CLOSED"] 'display status of
selected relay
pause 100
goto loop

displayo:
serout 0,bps,[noparse][[/noparse]254,192]
pause dly
serout 0,bps,[noparse][[/noparse]"RLY ", (result), " OPEN "]
pause 100
goto loop

reset:
debug "reset",cr
mainio
serout 0,bps,[noparse][[/noparse]1]
pause dly
serout 0,bps,[noparse][[/noparse]12]
pause dly
serout 0,bps, [noparse][[/noparse]"READY"]
goto loop




Thanks

ken

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-01-09 10:34
    Instead of 'outs(result)' (kind of a word variable)
    you probably should use 'outs.LOWBIT(result)'
    to access the individual bits of outs like a bit array.

    Hope this helps
    Adrian
  • ArchiverArchiver Posts: 46,084
    edited 2003-01-09 14:17
    You can deal with bits in any variable as an array element if you do it like
    this:

    status = Outs.LowBit(idx)

    Where idx is 0 - 15 corresponding to your output bit. Since you're using a
    BS2p40, you must remember which set of pins you're dealing with. If you want
    to know about I/O pin 15 then this works:

    MAINIO
    status = Outs.LowBit(15)

    If you're outputs are on the AUX group, you need to do this:

    AUXIO
    status = Outs.LowBit(15)

    If you're mixing pins on both groups, you can write a little routine that
    works like this:

    Get_Status:
    IOTERM (ioPin 16) ' ioPin = 0 - 31
    status = Outs.LowBit(ioPin)
    RETURN

    I hope this helps.

    -- Jon Williams
    -- Parallax





    In a message dated 1/9/2003 3:36:56 AM Central Standard Time,
    jduldig@s... writes:

    > The code is designed to toggle outputs based on a keypad input. This
    > part works fine. I just can not seem to dispaly the current status of
    > a given output.
    >
    > If I individually assign each output (i.e. out2, out6 etc), that
    > works. However, tagging a VAR onto out2 (i.e. out0(result) or outs
    > (result), does not seem to work.



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-01-09 17:53
    As others have pointed out, you want to address the bits of one word,
    not successive words. Use this syntax:
    out0(result)
    is equivalent to
    outs.lowbit(result)
    is equivalent to
    outs.bit0(result)

    Here is another issue:


    >serin Rx,bps,[noparse][[/noparse]invar]
    >if invar=13 then tgl 'if key 13 is pressed then toggle
    >output
    >if invar=8 then reset
    >invar=invar-48
    >if invar<=10 then keyscan
    >invar = invar-7
    >
    >Keyscan:
    >LOOKUP invar, [noparse][[/noparse]48,55,56,57,52,53,54,49,50,51,65,66,67,68,35,42],result

    The "invar" will be 0,1,....., but the number output in "result" at
    this point will be too large for the following [noparse][[/noparse]even when changed to
    out0(result)].

    >mainio
    >if outs(result) = 0 then displayo
    >goto displayc

    out0(48) would refer to the 48th bit following out0, which is way up
    in the memory space above the i/o pin states.

    out0(48), out0(55),... and so on is not what you want. If you need
    to permute the lookup values, maybe:

    LOOKUP invar, [noparse][[/noparse]0,7,8,9,4,,...],result


    -- Tracy
Sign In or Register to comment.