How to read/write to P0..P15 simultaneously

Hello everyone. New BS user here. I have experience in basic programming and started using the basic stamp 2 recently. I have been experimenting around with it and wondered if I could output 1’s and 0’s from groups of pins simultaneously with one command line. I understand how to do that with OUT and the other set ups for input and output control. But I am interested in using a variable to represent the ports P15..P0 to set them hi or low in the command line instead of individual pin states.
For example:

Instead of this

DIRS = %0000000000001111 ' make pins 0 - 3 outputs
OUTS = %0000000000001111 ' make pins 0 - 3 high

I would like to have something like this:

A Variable called Ports to represent the ports “0000000000001111” (P15..P0)

To then use a command such as

DIRSports VAR Word ‘represents the target for the DIRS command.
OUTSports VAR Word ‘represents the target for the OUTS command.

DIRSports = 15 ‘decimal for 0000000000001111 output pin directions
OUTSports = 15 ‘decimal for 0000000000001111 output pin states

DIRS = DIRSports ‘make pins P3 – P0 outputs
OUTS = OUTSports ‘sets pins P3..P0 high at the same time

I understand from experimentation that using a variable with DIRS and OUTS as above doesn’t work. Hopefully maybe someone has found a clever way to do something like this. Thanks.


  • For most applications, you setup the I/O pin directions once during your program's initialization using DIRS or its subsets (DIRA,DIRB,DIRH, etc.) You then set the output pin states using OUTS and its subsets. When you can, you assign groups of I/O pins on 8 or 4 bit boundaries so you can do what you describe. For example, DIRA and OUTA represent I/O pins 0-3 and you can manipulate the settings for those pins without affecting the others. There is no way to create other groups of I/O pins. Look at the section of the Basic Stamp Manual on memory architecture.

    When it's absolutely necessary to assign I/O pin groups across 8 or 4 bit boundaries, you can use bit manipulation to leave other I/O pins unchanged like:

    DIRS = DIRS & %1111111001111111 ' make P7-8 inputs

    DIRS = DIRS | %0000000110000000 ' make P7-8 outputs
  • Update: Got my hands on a copy of StampWorks and found what I was trying to do. Amazing. The LED Counter (Ex03) was exactly what I wanted to do. Great resource. Having a load of fun with this little microcontroller! Thanks for the help!
Sign In or Register to comment.