Example

  TOGGLE BitVar

Function
Invert the state of the specified bit.

Explanation
TOGGLE inverts the state of the specified bit, changing 0 to 1 and 1 to 0. If BitVar is an SX I/O pin, it sets a pin to output mode and inverts the pin's state.

In some situations TOGGLE may appear to have no effect on a pin's state. For example, suppose RA.3 is in input mode and pulled to +5V by a 10 kΩ resistor. Then the following code executes:

Main:
  INPUT RA.3                                    ' make RA.3 an input
  RA.3 = 0                                      ' set output driver to 0
  TOGGLE RA.3                                   ' toggle pin state

The state of RA.3 doesn't change; it's high (due to the pull-up resistor) before TOGGLE, and it’s high (due to the pin being output high) afterward. The point is that TOGGLE works on associated port register bit, 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:

Main:
  INPUT RA.3                                    ' make RA.3 an input
  RA.3 = RA.3                                   ' get state at pin
  TOGGLE RA.3                                   ' toggle pin state

Related instructions: HIGH, LOW, and OUTPUT