toggling bits

dougyy

New Member
Short on bits today I tried using flag5 on a 28x1. "Togglebit flag5" won't make it through the syntax editor (MacAXEpad, v1.0.3) while "Toggle flag5" doesn't do anything. I ended up using "let flag5 = flag5 +1" but it looks bad and am not sure what happens with the overflow, although it seems to work.
 

jim1248

Member
you could use 'not flag5' tried in PE simulator and seems to have the same effect as 'flag5 = flag5 + 1" and it won't cause an overflow
 

Technical

Technical Support
Staff member
'Toggle flag5' will actually do something - it will toggle output 1 if flag5 = 1 and toggle output 0 if flag5 = 0. Not what you intended!

The best syntax is as wa55 says
flag5 = flag5 xor 1

It doesn't matter if you are using AXEpad or ProgEdit - they use exactly the same compiler so if it works in one it will work in the other!
 

dougyy

New Member
'Toggle flag5' will actually do something - it will toggle output 1 if flag5 = 1 and toggle output 0 if flag5 = 0. Not what you intended!

The best syntax is as wa55 says
flag5 = flag5 xor 1
Thanks,
flag5 = flag5 xor 1 gets through the syntax check, will test on the project later today. Am confused a bit on output 1, since I'm using that as my pwm direction output. Am I switching that output each time flag5 is switched? Manual 2 on system variables talks about using system variables/flags as being safe if that system hardware is not used. Thought flags 5-6 would be ok since I'm not using either hserial or hi2c.

After posting yesterday also tried using one of the higher variables (w12) with the the bit commands (clearbit, togglebit, setbit). After going over the manual and experimenting modifying the bits worked well, but I couldn't figure out how to check a single bit status in a conditional statement. What kind of syntax do I need for: if (w12, 6) = 1 then .... ? While this won't be needed if flags 5-6 are available it would still be nice to know how to do it.

Off to the workbench now for more debuging.
 

westaust55

Moderator
Directly, bits can only be checked for the first few bytes b0, b1, and maybe b2, b3 (exact number depends upon the PICAXE chip. See PICAXE Manual 2 page 10.

If you want to set a bit in any other variable then you need to use an OR command

like LET b5 = b5 OR %00100000 will set bit 5 in byte variable b5

to test if a bit is set you can use the AND command

b6 = b5 AND %00100000
then you can test by
IF b6 = 0 then . . . bit5 in b5 was 0
IF b6 <> 0 then bit5 in b5 was 1
or
IF b6 =%00100000 then bit5 in b5 was 1

Far easier to use the bits in the lower byte/word variables for bit manipulation in many cases
 

hippy

Ex-Staff (retired)
I couldn't figure out how to check a single bit status in a conditional statement. What kind of syntax do I need for: if (w12, 6) = 1 then .... ?
The syntax is

IF <var> BIT <bitNumber> SET THEN ...
IF <var> BIT <bitNumber> CLEAR THEN ...

Thus to check if bit 6 of variable 'w12' is set you would use -

IF w12 BIT 6 SET THEN ...

This is further described on page 84 of PICAXE Manual 2.
 

westaust55

Moderator
Ah yes, and I should remember that :eek:
as I reported the bug in PE 5.2.9 with that command when the elseif was used - and that is now fixed in PE V5.2.10

And as from post 1 you have a 28x1 all is well since that test structure only works for X1 and X2 parts
 
Last edited:

dougyy

New Member
This is further described on page 84 of PICAXE Manual 2.
Dang it, I read that yesterday and still didn't pick up on it. Will put an example in my manual to remind me, Thanks for the help, I'll post the code when done so everyone can have a good chuckle. :)
 
Top