quickest easiest way to toggle bit on 20M2?

Hemi345

Senior Member
What's the easiest way to toggle a bit on M series chips? Gotta be something more efficient than this:
Code:
if bit0 = 1 then
  bit0 = 0
else 
  bit0 = 1
end if
Thanks
 

hippy

Technical Support
Staff member
Any of the following and their equivalents will invert a bit variable -

bit0 = bit0 ^ 1 : bit0 = bit0 Xor 1
bit0 = bit0 + 1 : inc bit0
bit0 = bit0 - 1 : dec bit0

I would tend to favour the first because that is most clearly a bitwise operation. There is also -

bit0 = not bit0

Which is probably the most logical way for dealing with boolean bit values, but will not have the smallest code.
 

Willie...

New Member
A method that I found, and have used for years, is like this:

let b1=1-b1

However, I just now looked at the PICaxe manual, and the TOGGLE command works in all chips.

Syntax:
TOGGLE pin,pin,pin...
- Pin is a variable/constant which specifies the i/o pin to use.
Function:
Make pin output and toggle state.
Information:
The high command inverts an output (high if currently low and vice versa)
On microcontrollers with configurable input/output pins (e.g. PICAXE-08) this
command also automatically configures the pin as an output.
Example:
main:
toggle B.7 ; toggle output 7
pause 1000 ; wait 1 second
goto main ; loop back to start
 

Circuit

Senior Member
A method that I found, and have used for years, is like this:

let b1=1-b1

However, I just now looked at the PICaxe manual, and the TOGGLE command works in all chips.
I think you are confusing TOGGLE with TOGGLEBIT. The former only works with pins, not bits; the latter works only on X2 chips.
 
Top