5: BASIC Stamp Command Reference – TOGGLE
BASIC Stamp Programming Manual 2.0c
•
www.parallaxinc.com
•
Page 327
TOGGLE
BS1 BS2 BS2e BS2sx BS2p
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 set high. This pin will be placed into output mode.
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 ' Pin 2 in input mode.
PIN2 = 0 ' Pin 2 output driver low.
DEBUG ? PIN2 ' Show state of pin 2 (1 due to pullup).
TOGGLE 2 ' Toggle pin 2 (invert PIN2, put 1 in DIR2).
DEBUG ? PIN2 ' Show state of pin 2 (1 again).
--or--
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 is that TOGGLE works on the OUTS register, which may not match
the pin’s state when the pin is initially an input. To guarantee that the state
actually changes, regardless of the initial input or output mode, do this:
PIN2 = PIN2' Make output driver match pin state.
TOGGLE 2 ' Then toggle.
--or—
OUT2 = IN2 ' Make output driver match pin state.
TOGGLE 2 ' Then toggle.
1
2
e
2
sx
2
p
2
1
NOTE: Expressions are not
allowed as arguments on the BS1.
The range of the Pin argument on
the BS1 is 0 – 7.
1
2
e
2
sx
2
p
2
1
2
e
2
sx
2
p
2
Summary of Contents for BASIC Stamp 1
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...