Shop OBEX P1 Docs P2 Docs Learn Events
How can I use a binary bit pattern to set highs and lows on multiple pins? Easy — Parallax Forums

How can I use a binary bit pattern to set highs and lows on multiple pins? Easy

Brian218Brian218 Posts: 92
edited 2008-12-28 03:59 in General Discussion
Hi,

I usually use the propeller, but am also trying to learn SX/B for projects where the propeller would be overkill. I mention the propeller because I can easily do the following in SPIN, but find myself stuck when attempting to do the equivalent on the SX.

I scanned through som previous forum posts without finding what I'm looking for.

I'm trying to set the highs and lows of 3 output pins using a binary bit pattern.

Is there an easy way to do this? Any help will be greatly appreciated.
Thanks in advance.
Merry Christmas

Example:

the output pins are RB.0, RB.1 and RB.2
#1
Binary_pattern = %101
The result I want based on this bit pattern is:
RB.0 high
PB.1 low
PB.2 high

#2
binary_pattern = %011
desired result:
RB.0 low
RB.1 high
RB.2 high

Using Spin the equivalent would be:
#1
Bit_pattern := %101
OUTA[noparse][[/noparse]RB0..RB2] := Bit_pattern

#2
Bit_pattern := %011
OUTA[noparse][[/noparse]RB0..RB2] := Bit_pattern

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
This post is a work of art. Variations in spelling and grammar are intentional, artistic endeavors that add value to all of mankind.

Comments

  • ZootZoot Posts: 2,227
    edited 2008-12-26 02:44
    Use the full port (i.e. RB or a symbolic equivalent), e.g.

    MyPort PIN RB OUTPUT ' RB.0 - RB.7 are outputs, addressed as "MyPort"
    myByte VAR Byte
    
    MyPort = %0000_0001  ' set just RB.0 high, the rest low
    MyPort = %1000_1000  ' RB.3 and RB7 high, rest low
    
    'masking and setting just some pins with AND and OR ...
    
    myByte = MyPort ' copy to work byte
    myByte = myByte & $F0 ' mask off all 4 low nib bits
    myByte = myByte | %1010 ' or to set .3 and .1 high
    MyPort = myByte ' copy back to port
    
    



    You can also use GETBIT and PUTBIT which are pin bit operators in the forthcoming SX/B 2.0, but essentially those high level commands do the same as above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Brian218Brian218 Posts: 92
    edited 2008-12-26 04:16
    Zoot,

    Thanks for the quick reply!

    I'll study your example and try to figure it out.

    B-

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    This post is a work of art. Variations in spelling and grammar are intentional, artistic endeavors that add value to all of mankind.
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-26 14:42
    You can also do direct bit-to-bit copies when it's just a few pins; this is also helpful if they pins from source to destination are not aligned or contiguous.

    myPort.0 = myByte.0
    myPort.4 = myByte.1
    myPort.7 = myByte.2
    

    Post Edited (JonnyMac) : 12/26/2008 9:20:34 PM GMT
  • Brian218Brian218 Posts: 92
    edited 2008-12-28 03:59
    Thanks JonnyMac!

    I appreciate the info, every little bit helps <no pun intended>

    B-

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    This post is a work of art. Variations in spelling and grammar are intentional, artistic endeavors that add value to all of mankind.
Sign In or Register to comment.