Toggle bit vs inc bit

Using an 8M (vs. 9.2, PE 5.4.3), I wanted to toggle one of the b0 flags. Inc bit0 worked as expected, but when I tried: toggle bit0 , toggle 0 was what happened, while nothing happened to bit0. (And same with bit1 and op1.) As I have not yet entered the xM2 world, I don't know it the same will happen there. If this "anomaly" is intended, it might be an idea to let the compiler flag down toggle bitx. Otherwise .... Well, no big deal, just reporting..
 

nick12ab

Senior Member
The toggle command is for toggling an output pin state. As pin numbers are represented by constants (B.0 = 0, B.1 = 1, C.0 = 8 etc) when you try to toggle bit0 you actually toggle the pin referenced by bit0.
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.
You need to use the togglebit command instead, which does what you want to do.
Syntax:
TOGGLEBIT var, bit
- var is the target variable.
- bit is the target bit (0-7 for byte variables, 0-15 for word variables)
Function:
Toggle (invert) a specific bit in the variable.
EDIT: I beat Technical to it!
 

Technical

Technical Support
Staff member
Think about what you are actually using within the command, it's normal behaviour... as toggle is a command for outputs.

if bit0 is currently = 0 you are saying

toggle 0 (toggle output 0)

and if 1

toggle 1 (toggle output 1)

b0 = b0 xor %00000001 will do what you wanted.
 
Thanks a lot for the comments. nick12ab: Togglebit does not apply yo 8M.. Tecnical: I now realize that toggle bit0 simply isn't a syntax error, but a perfectly valid output command!.... Now, I did use inc bit0 instead of: b0 = b0 xor %00000001, and even tried : bit0=bit0 xor 1, which also worked well. All 3 of them took 3 bytes ea of program memory, but b0 = b0 xor %00000001 was marginally faster.
 
Top