"W" & "B" working variables - a clarification please?

OLDmarty

Senior Member
Hi All,

I was reading the info about the W (word) and B(byte) working variables and pondered a few things....

It states "There are 14 or more general purpose byte variables. These byte variables are labelled b0, b1 etc"
So, is it 14 bytes? "or more"? does it depend on the chip type?

It then shows a map where W0 to W6 are made up up bytes paired together:

e.g.
W0 = b1,b0
W1 = b3,b2
W2 = b5,b4
W3 = b7,b6
W4 = b9,b8
W5 = b11,b10
W6 = b13,b12


So far, so good, but is there anything like W7 or beyond?

Then, the bytes are shown broken down to bit level.

e.g.
b0 = bit7: bit6: bit5: bit4: bit3: bit2: bit1: bit0
b1 = bit15: bit14: bit13: bit12: bit11: bit10: bit9: bit8

M2, X1 and X2 parts also support bit16-bit31 (b2-b3)


Do the bit names then continue through all bytes upto byte B13? (B13 being bits "bit104-"bit111")

Have i understood this correctly? that i can address 112 individual bits within all the variables from "Bit0" upto "Bit111" ?????


Thanx in advance, i hope i've understood this correctly, and/or it helps others understand it too ;-)
 

inglewoodpete

Senior Member
To fully understand the "register" variables requires close reading.

Rev-Ed have had to make decisions depending on the availability of sufficient RAM in the underlying PIC chips.

Just above the information that you have quoted (from Manual 2), you will see the number of bytes/words in each of the PICAXE chip models. X2s have 56 bytes (b0 to b55) or 27 words (w0 to w26) or combinations of this etc etc. That form of bit addressing is as per that same table. Bit names are limited to the first few bytes as per this table.

You can address any bit in any byte variable in the X2 chips using SetBit, ClearBit, and If (variable) Bit ....

Once you have a good understanding of what variables can be used for each purpose, you can allocate them to you project as required. I always leave bytes 0 and 1 free for bit addressing. I then allocate byte variables from b2 upwards and word variables from the last one downwards.
 

OLDmarty

Senior Member
To fully understand the "register" variables requires close reading.

Rev-Ed have had to make decisions depending on the availability of sufficient RAM in the underlying PIC chips.

Just above the information that you have quoted (from Manual 2), you will see the number of bytes/words in each of the PICAXE chip models. X2s have 56 bytes (b0 to b55) or 27 words (w0 to w26) or combinations of this etc etc. That form of bit addressing is as per that same table. Bit names are limited to the first few bytes as per this table.

You can address any bit in any byte variable in the X2 chips using SetBit, ClearBit, and If (variable) Bit ....

Once you have a good understanding of what variables can be used for each purpose, you can allocate them to you project as required. I always leave bytes 0 and 1 free for bit addressing. I then allocate byte variables from b2 upwards and word variables from the last one downwards.
Thanx for your help Pete

ok, i see now......
The table i was reading was in manual 1, but i now see in manual 2 what you mentioned.

Just confirming, we can ONLY access upto bit31 and nothing higher? (bitwise), but obviously we can a still access B0-B55 and/or W0-W27 for general storage etc

I was puzzled why it never referred to anything above bit31, as my example shows upto bit111.

I often hate reading, and only read what i 'want' to see lol ;-)
 

inglewoodpete

Senior Member
I often hate reading, and only read what i 'want' to see lol ;-)
Take the manuals to bed with you for some bedtime reading ;)

1. You'll learn something new every day
2. You'll get off to sleep much quicker
3. You'll dream about PICAXE. The possibilities are endless!
 

Technical

Technical Support
Staff member
If you don't want to read, select the chip you are interested in within the PE6 software and then simply look at the Code Explorer (right hand side of screen). It lists which RAM/variables are available for that particular chip.
 

lbenson

Senior Member
As IP states, any bit may be tested in the X2 picaxes, so what task is it that you want to perform in which you may want to test up to 56*8 or 448 bits?

I use IP's method of using "W" variables from the top down, so w27, w26, etc. (w13, w12 for M2 chips), and byte variables from the bottom up, except that I reserve all 4 variables which have named bits, so I begin using b4, b5, etc.
 

hippy

Technical Support
Staff member
One thing to remember is that byte and word variables can be moved to b0 though b3, w0 or w1, and the bit variables of those can be used. For example -

Let w0 = w2
If bit0 = 1 Then ...

That isn't quite so nice as 'If bit32 = 1 then...' but may often be tolerable.
 

AllyCat

Senior Member
Hi,

It's also possible to test any bit of any byte or any word variable, by ANDing or ORing with the binary power (1, 2, 4, 8... etc.) of the bit, and testing if = 0 (or <> 0 , or = 255 , or = 65535 , as appropriate). But unless you can "destroy" the contents of the variable (e.g. bx = bx AND 16 : IF bx <> 0 THEN ....) , you still need to use a "spare" variable, so it may as well be b0 (or w0) and test the bitx variable, as others have said above.

You can address any bit in any byte variable in the X2 chips using SetBit, ClearBit, and If (variable) Bit ....
Using b0 or w0 directly is faster and more compact (in downloaded code size) than the above, because it appears that the PE may simply replace these commands with a "macro", something like : PUSH w0 : w0 = variable : IF bitx = 1 THEN ... : POP w0 ... . The additional PUSH and POPs being needed in case the program-writer is using w0.

Cheers, Alan.
 

OLDmarty

Senior Member
ok, this might sound like a silly question, but if all 56 'byte' variables (28 'word' variables) have bits that are accessable to set, clear etc, then "why" do the first 32 bits have their own unique access names?

e.g.
If i can set/clear W0,15 (aka bit15) and just as easily set/clear W25,15 then what is the reason for needing the terms "bit0 to bit31"???? <- is this just a legacy method being held on to?




On a similar note regarding Bytes/words access....

Can i make an output pin (with an LED attached) follow the staus of say W25,0

Would i use"
"OutpinD.0 = W25,0"

Or would it be more like:

"if W25,0 = 1 then
OutputpinD.0 = 1"

etc etc...


Thanks again for everyone's comments, always good to bounce around some different ideas & scenarios to improve skills.
 

hippy

Technical Support
Staff member
"why" do the first 32 bits have their own unique access names?
Simply to be more convenient for the programmer.


Can i make an output pin (with an LED attached) follow the status of say W25,0
Yes. In fact you could just use "outpinD.0 = w25" for that, but that's an optimisation which only applies because you chose bit 0 rather than any other.

The generic way to do it is as follows where %xxxxxxxxxxxxxxxx would be a bit mask with just one "1" bit set, the rest "0" -

outpinD.0 = w25 And %xxxxxxxxxxxxxxxx Max 1

Or -

outpinD.0 = w25 / %xxxxxxxxxxxxxxxx

Or, for X2 PICAXE devices, when 'n' is the bit number 0 through 15 -

outpinD.0 = w25 >> n
 
Last edited:

techElder

Well-known member
Or, more conveniently, something like this could work (not tested for your program) ...

Code:
symbol my16Bit0 = %0000000000000001
symbol my16Bit1 = %0000000000000010
symbol my16Bit2 = %0000000000000100
symbol my16Bit3 = %0000000000001000
symbol my16Bit4 = %0000000000010000

outpinD.0 = w25 And my16Bit0 Max 1
outpinD.0 = w25 And my16Bit1 Max 1
outpinD.0 = w25 And my16Bit2 Max 1
outpinD.0 = w25 And my16Bit3 Max 1
outpinD.0 = w25 And my16Bit4 Max 1
 

OLDmarty

Senior Member
Simply to be more convenient for the programmer.
you could just use "outpinD.0 = w25" for that, but that's an optimisation which only applies because you chose bit 0 rather than any other.
I didn't know that, so if i used say bit7 it wouldn't work?

"outpinD.7 =w25"

or should it now be
"outpinD.7 = W25,7"

or, are you saying that as long as the bit number is allocated in the 'outpin' command, then it will automatically copy that same(equivelant) bit number in the variable (W25) being read from?
 

hippy

Technical Support
Staff member
or, are you saying that as long as the bit number is allocated in the 'outpin' command, then it will automatically copy that same(equivelant) bit number in the variable (W25) being read from?
No. You create a bit value on the right hand side, and the left hand side, the variable being assigned to, simply gets set to that bit value. You can use any "outpinX.Y" on the left hand side. For w25 bit 7 ...

Code:
outpinD.0 = w25 And %0000000010000000 Max 1
Though it would might be better to express the mask in hexadecimal than binary or to use the SYMBOL trick Texasclodhopper suggests above -

Code:
Symbol my16Bit7 = %0000000010000000

outpinD.0 = w25 And my16Bit7 Max 1
I'd perhaps take that one step further and go with ...

Code:
#Define GetBit7(var) var And %10000000 Max 1

outpinD.0 = GetBit7(w27)
 

inglewoodpete

Senior Member
I'd perhaps take that one step further and go with ...

Code:
#Define GetBit7(var) var And %10000000 Max 1

outpinD.0 = GetBit7(w27)
Taking that (another) step further, passing 2 parameters into the macro code (untested):
Code:
#Define GetBit(var, mask) var And mask Max 1

outpinD.0 = GetBit(w27, %10000000)    'or outpinD.0 = GetBit(w27, 128) or outpinD.0 = GetBit(w27, $80)
Marty may get what he wants yet :)
 

lbenson

Senior Member
Top