BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – TOGGLE
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 455
TOGGLE
BS1 BS2 BS2e BS2sx BS2p BS2pe BS2px
TOGGLE Pin
Function
Invert the state of an output pin.
Pin is a variable/constant/expression (0 – 15) that specifies which
I/O pin to switch logic state. This pin will be placed into output
mode.
Quick Facts
Table 5.122: TOGGLE Quick
Facts.
BS1 All BS2 Models
Affected
Register
PINS OUTS
Related
Commands
HIGH and LOW
Explanation
TOGGLE sets a pin to output mode and inverts the output state of the pin,
changing 0 to 1 and 1 to 0.
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 ' make P2 an input
PIN2 = 0 ' make P2 output driver low
DEBUG PIN2 ' show P2 state (1 due to pull-up)
TOGGLE 2 ' toggle P2
DEBUG PIN2 ' show P2 state (1 again)
- or -
DIR2 = 0 ' make P2 an input
OUT2 = 0 ' make P2 output driver low
DEBUG ? IN2 ' show P2 state (1 due to pull-up)
TOGGLE 2 ' toggle P2
DEBUG ? IN2 ' show P2 state (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 is that TOGGLE works on the OUTS register (PINS on the BS1),
which may not match the pin’s state when the pin is initially an input. To
NOTE: Expressions are not allowed as
arguments on the BS1. The range of
the Pin argument on the BS1 is 0 – 7.
1
A
ll
2
1
1
All
2