Problems with bits

Manel_s4

Member
So I'm programming a LED cube and I have been reading the manuals previously. On the page 52 of the first manual (version recently actualized) explains how can I collect two bytes, with a word. So my LED CUBE is 3x3x3 and I multiplex the LED's in each floor, so I need 8 bits, so 2 bytes, 1 word for each floor. The problem is when I use de w2, the programm doesn't recognise bit40. What can I do?
 

nick12ab

Senior Member
The program doesn't recognise bit40 because even the new PICAXEs can only do up to bit31. What you can do is instead of wasting 7 bits of a word by using one word per floor you can just use the first 9 bytes for one floor, another 9 for the middle floor and another 9 for the last floor using bit0-bit8, bit9-17 and bit18-bit26 respectively then you have bit27-bit31 (5 bytes) spare for other things.

In addition bytes b0 and b1 (w0) are broken down into individual bit variables.
These bit variables can be used where you just require a single bit (0 or 1) storage
capability.
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)
You can use any word, byte or bit variable within any mathematical assignment
or command that supports variables. However take care that you do not
accidentally repeatedly use the same ‘byte’ or ‘bit’ variable that is being used as
part of a ‘word’ variable elsewhere.
 

Manel_s4

Member
Yes, I know it, but with one word for each floor I can work easier. My qüestion is if there is any other solution?
 

nick12ab

Senior Member
Another suggestion:

This might be too slow to work on a PICAXE, but you could copy the variable that can't be accessed with bit variables into another each time a bit is wanted, then use the & operator to drop all bits except the one you want to read, then check if the bit is set or not by comparing it with the appropriate value: 1,2,4,8,16,32,64,128 or use the bit shift operator >> and then the result can be used like any other bit.
 

Manel_s4

Member
Now, that I have modified the program, I have writen in this way:

Code:
symbol Data_Floor0 = b0 : bit8
symbol Data_Floor1 = bit9 : bit10 : bit11 : bit12 : bit13 : bit14 : bit15 : bit16 : bit17
symbol Data:Florr2 = bit18 : bit19 : bit20 : bit21 : bit22 : bit23 : bit24 : bit25 : bit26
I don't know if this is correct, I don't know how to link each bit with each. Is this the right way?
 

geoff07

Senior Member
No, in short. This won't compile, they aren't valid statements

bit0-bit7 ARE b0 (byte 0); similarly the bits up to 31 (b1, b2, b3) - they don't need to be assigned, they are the same thing.

review post 2, but don't assume these statements are Basic, they are just illustrations. And don't worry about minimising storage until you have worked out the program logic. If storage is an issue (unlikely I would guess) then you can review later when the shape of the project is clearer.

symbol Data_Floor0 = b0 'means byte 0 now also called Data_Floor0 and also called bit0-bit7
 

Manel_s4

Member
No, in short. This won't compile, they aren't valid statements

bit0-bit7 ARE b0 (byte 0); similarly the bits up to 31 (b1, b2, b3) - they don't need to be assigned, they are the same thing.

review post 2, but don't assume these statements are Basic, they are just illustrations. And don't worry about minimising storage until you have worked out the program logic. If storage is an issue (unlikely I would guess) then you can review later when the shape of the project is clearer.

symbol Data_Floor0 = b0 'means byte 0 now also called Data_Floor0 and also called bit0-bit7
And I should write this?

Code:
symbol Data_Floor0 = b0, bit8
symbol Data_Floor1 = bit9, bit10, bit11, bit12, bit13, bit14, bit15, bit16, bit17
symbol Data_Floor2 = bit18, bit19, bit20, bit21, bit22, bit23, bit24, bit25, bit26
 

geoff07

Senior Member
What you should write depends on what you are trying to do, which has me puzzled. Perhaps if you spelt out what info you need to store, and what is to be done with it, then people could suggest ways of doing it.

As I see it, you want a data structure that comprises 27 cells, representing a 3 x 3 x 3 matrix. For that I would use bytes, and then have a number of subroutines to act on the matrix according to whatever you want to achieve. I would probably use pointers in the unaddressed memory area; to do this I would use three bytes as coordinates each with values 0,1,2 and then calculate the pointer value according to the cell I wanted to act upon. If x, y and z are the coordinates each taking the value 0,1, or 2, then the pointer would be base + x + (3 x y) + (9 x z). So if x,y,z were 000 then the address would be base + 0, if 1,1,1 then base + 13, and if 2,2,2 then base + 26 with all other values in there as well. I hope I got that right, it is late here.

Then any subroutine can read the three coords and find the value in the relevant cell and do something with it. And if you use the area I mean it won't use up any variables apart from the three coords.

If you really run out of space you can revert to bits but with an M2 or X2 I doubt you would and code that avoids bit fiddling is easier to debug.
 

inglewoodpete

Senior Member
Why not use w1 to store data for layer 1, w2 to store data for layer 2, w3 to store data for layer 3?

And use w0 for manipulating the LEDs in any layer.

When you're working on (any) layer, first do w0 = wX, where wX is w1, w2 or w3. Once completing your work on that layer, copy the data back to the storage word: wX = w0

Since you are only using 9 bits in each word, you could use some of the spare bits to point to the layer, making it possible to do a lot of the work all layers in a single subroutine.
 

Manel_s4

Member
Why not use w1 to store data for layer 1, w2 to store data for layer 2, w3 to store data for layer 3?

And use w0 for manipulating the LEDs in any layer.

When you're working on (any) layer, first do w0 = wX, where wX is w1, w2 or w3. Once completing your work on that layer, copy the data back to the storage word: wX = w0

Since you are only using 9 bits in each word, you could use some of the spare bits to point to the layer, making it possible to do a lot of the work all layers in a single subroutine.
I think that I have understood it. I'll try it. Thanks!
 
Top