Specifications
BASIC Stamp II
Parallax, Inc. • BASIC Stamp Programming Manual 1.8 • Page 337
2
Toggle
TOGGLE pin
Invert the state of a pin.
•
Pin
is a variable/constant (0–15) that specifies the I/O pin to use.
The state of the corresponding bit of the OUTS register is inverted
and the pin is put into output mode by writing a 1 the
corresponding bit of the DIRS register.
Explanation
Toggle inverts the state of an I/O pin, changing 0 to 1 and 1 to 0. When
the pin is intially in the output mode, Toggle has exactly the same ef-
fect as complementing the corresponding bit of the OUTS register. That
is, Toggle 7 is the same as OUT7 = ~OUT7 (where ~ is the logical NOT
operator).
When a pin is initially in the input mode, Toggle has two effects; it
inverts the output driver (OUTS bit) and changes the pin to output
mode by writing a 1 to the pin’s input/output direction bit (the corre-
sponding bit of the DIRS register).
In some situations Toggle may appear to have no effect on a pin’s state.
For example, suppose pin 2 is in input mode and pulled to +5V by a
10k resistor. Then the following code executes:
DIR2 = 0 ' Pin 2 in input mode.
OUT2 = 0 ' Pin 2 output driver low.
debug ? IN2 ' Show state of pin 2 (1 due to pullup).
TOGGLE 2 ' Toggle pin 2 (invert OUT2, put 1 in DIR2).
debug ? IN2 ' Show state of pin 2 (1 again).
The state of pin 2 doesn’t change—it’s high (due to the resistor) before
Toggle, and it’s high (due to the pin being output high) afterward. The
point of presenting this puzzle is to emphasize that Toggle works on
the OUTS register, which may not match the pin’s state when the pin is
initially an input.
If you want to guarantee that the state of the pin actually changes,
regardless of whether that pin starts as an input or output, just do this:










