#Picaxe 08M2
#No_Data
Symbol TempWord0 = W0 '// reserve as reusable temp variable
Symbol TestVar = W10
Do
inc TestVar '// Increment by 1
TempWord0 = TestVar
If bit1 = 1 then
Sertxd ("Bit1 is SET",13,10)
Else
Sertxd ("Bit1 is Clear",13,10)
End if
pause 1000
Loop
#Picaxe 40X2
symbol test = w10
if test BIT 1 SET then does not work, the word SET is green
also tried
if test & $0001 <> 0 then also not accepted.
I would guess it is perhaps because a value of 1 sets bit 0, not bit 1. It all worked when I tried it, in simulation in PE6 and on a real chip.#Picaxe 40X2
symbol test = w10
if test BIT 1 SET then does not work, the word SET is green
also tried
if test & $0001 <> 0 then also not accepted.
#Picaxe 28X2
#Terminal 9600
Symbol test = w10
Pause 2000
SerTxd("Starting...", CR, LF )
Do
test = 0
If test Bit 1 Set Then
SerTxd( "Set FAIL when bit = 0", CR, LF )
Else
SerTxd( "Set Okay when bit = 0", CR, LF )
End If
test = 2
If test Bit 1 Set Then
SerTxd( "Set Okay when bit = 1", CR, LF )
Else
SerTxd( "Set FAIL when bit = 1", CR, LF )
End If
test = 0
If test Bit 1 Clear Then
SerTxd( "Clr Okay when bit = 0", CR, LF )
Else
SerTxd( "Clr FAIL when bit = 0", CR, LF )
End If
test = 2
If test Bit 1 Clear Then
SerTxd( "Clr FAIL when bit = 1", CR, LF )
Else
SerTxd( "Clr Okay when bit = 1", CR, LF )
End If
Pause 5000
SerTxd( "---", CR, LF )
Loop
Yes, that command is something I missed in the manual for quite some time and I was delighted when I discovered it; it is there in the manual; page 107 manual 2;Best I can tell there is no simple read access to individual bits of variables other than B0,B1,B2,B2 and/or W0 and W1.
Goey
As Circuit suggests it can be a useful construct for some in terms of readability.Hi,
It may be a "useful" command, but it occupies significantly more codespace (and is probably slower) than the "normal" syntax. For example:
IF w10 BIT 4 SET THEN : ENDIF uses 16 bytes of program space, whilst:
w0 = w10 : IF bit4 = 0 THEN : ENDIF uses only 9 bytes (and remains compatible with M2 code). And it's only 6 bytes if you declare w0 as the variable at the start.
Cheers, Alan.
Can you repeat your solution with actual code which runs in the simulator?I don't get the question. W10 is b20 and b21 so why not if b20 and bit value ie 1,2,4,8 then whatever, can't see the issue except tex's reply. What am I missing?
From an earlier post it appears you are using a PICAXE 40X2 and PE6 definitely accepts it for a 40X2.The manual indicates that if var BIT 1 SET then should work, but the editor would not accept it.
Can you repeat your solution with actual code which runs in the simulator?
w10=1
b0=b20 and 255
b1=b21 and 255
sertxd ("w10 low=",#b0," w10 high=",#b1,cr,lf)
if b20=1 then sertxd ("bit 0 w10 set",cr,lf):end if
w10 = w10 + 2
b0=b20 and 255
b1=b21 and 255
sertxd ("w10 low=",#b0," w10 high=",#b1,cr,lf)
if b20=1 then sertxd ("bit 0 w10 set",cr,lf):end if
w10 low=1 w10 high=0
bit 0 w10 set
w10 low=3 w10 high=0
That's identical to w0 = w10 AND $FFFF wihich is then the same as w0 = w10 , but perhaps you like writing slow, inefficient and confusing code.b0=b20 and 255
b1=b21 and 255
Using 'AND 1,2,4,8' etc. is a very limited way of testing bits.I don't get the question. W10 is b20 and b21 so why not if b20 and bit value ie 1,2,4,8 then whatever, can't see the issue except tex's reply. What am I missing?
[color=Navy]#picaxe [/color][color=Black]28x2[/color]
[color=Navy]#no_table
#no_data[/color]
[color=Blue]symbol [/color][color=Purple]MyBitNum [/color][color=DarkCyan]= [/color][color=Purple]b4
w10 [/color][color=DarkCyan]= [/color][color=Navy]%1111000010101010[/color]
[color=Blue]for [/color][color=Purple]MyBitNum [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]15
[/color][color=Blue]if [/color][color=Purple]w10 [/color][color=DarkCyan]BIT [/color][color=Purple]MyBitNum [/color][color=DarkCyan]SET [/color][color=Blue]then
sertxd([/color][color=Red]"1"[/color][color=Blue])
else
sertxd([/color][color=Red]"0"[/color][color=Blue])
endif
next[/color]
It would help members if you could post the exact code that you are trying to use. You only mention your code in discussion, saying that it doesn't work. Since we know that the correct syntax DOES work, it would help us to help you.Thanks to every one who replied. The manual indicates that if var BIT 1 SET then should work, but the editor would not accept it. Decided to go with:
let w0 = col1
if BIT1 <> 0 then
low row1
else
high row1
endif etc.
let w0 = col2 etc.
"Doesn't work if both bits 0 and 1 are on." use and 3. I still can't see what the problem with finding the value of w10. I'm obviously missing the point. Sorry
Te issue is that this only works if there is only one bit set in the variable.I don't get the question. W10 is b20 and b21 so why not if b20 and bit value ie 1,2,4,8 then whatever, can't see the issue except tex's reply. What am I missing?
Complete Code to Test:I think I have found an even better solution
let pinB.n = BITn etc
should be very fast and use little code space.
#Picaxe 40x2
Symbol TestVar = W0
DIRSB = %00000010
Do
Inc TestVar '//Increment
Let PinB.1 = Bit1
Pause 1000
Loop
The common way to test bits is to AND out only the bits of interest and then check for the desired value, so a two step process such as"If more than one bit is set then simply comparing the variable with 1, 2, 4, 8 etc will not work. The individual bits need to be checked." So if you want to check if bit 0 and bit 6 set then anding the var with 65 won't work?