Bits, Bytes Words and Arrays
1. How can I reference a BIT in a BYTE?
2. How can I reference a BIT in a WORD?
3. How can I reference a BIT in a BYTE ARRAY?
4. How can I reference a BIT in a WORD ARRAY?
5. How can I reference a BYTE in a WORD ARRAY?
We all know there’s eight Bits in a Byte, and sixteen Bits in a Word. Sometimes however we need to access a Bit in a Byte, or a Word, or in an Array by using an intermediate VARIABLE rather than a constant.
1. How can I reference a BIT in a BYTE?
We know that MyByte.0 references Bit 0 of MyByte, all the way up to MyByte.7 referencing Bit 7 of MyByte… but there’s another way of accessing the bits in a Byte (MyVar is a BYTE variable)…
MyByte.0(MyVar)
When MyVar is in the range 0-7, it will access Bits 0-7 of MyByte.
Example to display all eight BITS of a BYTE sequentially, Bit at a time…
For MyVar=0 to 7
MyBit=MyByte.0(MyVar)
LCDOut $FE,1,"Bit ",#MyVar,"=",#MyBit
Pause 2000
Next MyVar
2. How can I reference a BIT in a WORD?
We know that Myword.0 references Bit 0 of Myword, all the way up to MyWord.15 referencing Bit 15 of MyWord… but there’s another way of accessing the bits in a word (MyVar is a BYTE variable)…
MyWord.0(MyVar)
When MyVar is in the range 0-15, it will access Bits 0-15 of MyWord.
Example to display all sixteen BITS of a WORD sequentially, Bit at a time…
For MyVar=0 to 15
MyBit=MyWord.0(MyVar)
LCDOut $FE,1,"Bit ",#MyVar,"=",MyBit
Pause 2000
Next MyVar
3. How can I reference a BIT in a BYTE ARRAY?
Arrays are arranged in memory in BIT ORDER from the LEAST SIGNIFICANT BIT of the first Byte at the bottom end, to the MOST SIGNIFICANT BIT of the last Byte at the top end. Consider this…
MyByteArray var BYTE [40]
There are 40 Bytes in this array (0-39), each with eight Bits. That means this array has a total of 320 Bits (numbered 0-319 sequentially). All the Bits in this Byte Array can be referenced individually like so…
MyByteArray.0(MyVar)
Where MyVar in this instance is a WORD (because there’s more than 256 Bits in the array). When…
MyVar=0 it references Bit 0 of MyByteArray(0)
MyVar=7 it references Bit 7 of MyByteArray(0)
MyVar=8 it references Bit 0 of MyByteArray(1)
MyVar=15 it references Bit 7 of MyByteArray(1)
All the way, when…
MyVar=312 it references Bit 0 of MyByteArray(39)
MyVar=319 it references Bit 7 of MyByteArray(39)
If MyVar was a BYTE rather than a WORD, you will only be able to access Bits 0-255.
4. How can I reference a BIT in a WORD ARRAY?
Just like the Byte Arrays, Word Arrays are arranged in memory in BIT ORDER from the LEAST SIGNIFICANT BIT of the first Word at the bottom end, to the MOST SIGNIFICANT BIT of the last Word at the top end. Consider this…
MyWordArray var WORD [25]
There are 25 WORDS in this array (0-24), each with sixteen Bits. That means this array has a total of 400 Bits (numbered 0-399 sequentially). All the Bits in this Byte Array can be referenced individually like so…
MyWordArray.0(MyVar)
Where MyVar in this instance is a WORD (because there’s more than 256 Bits in the array). When…
MyVar=0 it references Bit 0 of MyWordArray(0)
MyVar=15 it references Bit 15 of MyWordArray(0)
MyVar=16 it references Bit 0 of MyWordArray(1)
MyVar=31 it references Bit 15 of MyWordArray(1)
All the way, when…
MyVar=384 it references Bit 0 of MyWordArray(24)
MyVar=399 it references Bit 15 of MyWordArray(24)
If MyVar was a BYTE rather than a WORD, you will only be able to access Bits 0-255.
5. How can I reference a BYTE in a WORD ARRAY?
As can be previously seen, all the bits in an array are stored sequentially in memory. It follows that the BYTES in a WORD ARRAY also follow sequentially. This means we can easily extract any BYTE we want to out of a WORD Array in a similar manner. Consider again…
MyWordArray var WORD [25]
There are actually 50 Bytes in this array (0-49). The first byte (0) being the LOWBYTE of MywordArray(0), and the last byte (49) being the HIGHBYTE of MyWordArray(24). Using a similar manner to Bit access, we can access all fifty Bytes of this Word array like so…
MyWordArray.Lowbyte(MyVar)
Where MyVar is in the range 0-49 for my previously defined example Array of 25 Words.
This is really handy, because we can use this method of loading or unloading Word Arrays from EEPROM for example very easily.
Note that HIGHBYTE cannot be used in this instance, because it cannot access the LowByte of the first array element… ie..
MyWordArray.HighByte(0)=MywordArray.LowByte(1)
all the way to…
MyWordArray.HighByte(48)=MywordArray.LowByte(49)
More dangerously, MyWordArray.HighByte(49) will actually perform an unauthorised access on an unknown byte OUTSIDE the MyWordArray structure.
Melanie
Re: Bits, Bytes Words and Arrays
This is working in Pic Basic:
For MyVar=0 to 15
MyBit=MyWord.0(MyVar)
LCDOut $FE,1,"Bit ",#MyVar,"=",MyBit
Pause 2000
Next MyVar
But I can not get to work in Pic Basic Pro
I need to read/write some bits from/to a word variable and this method was very useful.
Any ideas?
Many thanks,
Ricard.
Re: Bits, Bytes Words and Arrays
Hi,
It works perfectly fine here. I had to add a # in the LCDOUT statementin order to have it actually display "1" or "0" but apart from that it works as I'd exect it to.
Code:
MyVAR VAR BYTE
MyWord VAR WORD
MyBIT VAR BIT
MyWORD = %1010101010101010
For MyVar = 0 to 15
MyBit = MyWord.0(MyVar)
LCDOut $FE, 1, "Bit " ,#MyVar, "=", #MyBit
Pause 2000
Next MyVar
/Henrik.
Re: Bits, Bytes Words and Arrays
Henrik,
Thanks for your help. I not tried exaktly this (i have no LCD)
I tried this:
MR var word: H var byte
for H=0 to 15
MR.0[H]=1 'set bit H of MR
next H
This works fine in PicBasic but when compiling in PicBasicPro this bits never are set. Other things in my code (about 8k) works perfect. I have only problems when adressing bits form words or bytes
Maybe I have a library instalation problem?
Re: Bits, Bytes Words and Arrays
Hi,
That is weird.... This code:
Code:
MR var word: H var byte
MR = 0
HSEROUT ["First, all zeros:",13]
HSEROUT ["MR = ", BIN16 MR,13,13]
MR=65535
HSEROUT ["Then, all ones:",13]
HSEROUT ["MR = ", BIN16 MR,13,13]
for H=0 to 15
MR.0[H]=0 'reset bit H of MR
next H
HSEROUT ["Next, all zeros again:",13]
HSEROUT ["MR = ",BIN16 MR,13,13]
for H=0 to 15
MR.0[H]=1 'set bit H of MR
next H
HSEROUT ["And finally, all ones:",13]
HSEROUT ["MR = ", BIN16 MR,13,13]
Pause 10
END
Produces the following result on the serial terminal:
Code:
First, all zeros:
MR = 0000000000000000
Then, all ones:
MR = 1111111111111111
Next, all zeros again:
MR = 0000000000000000
And finally, all ones:
MR = 1111111111111111
The FOR-NEXT loop is copy-pasted from your post above. How do you know it's not setting the bit? Do you have serial line to which you can send the data as above? Is there any chanse something else in the program is resetting/changing MR before you have chanse to see it? What version of PBP? (I'm using 2.60)
/Henrik.
Re: Bits, Bytes Words and Arrays
Thanks,
I tried to run the isolated code and it works. :S
I'm sorry. I tested only the complete program before.
Sure the problem is in the code between. the weird is it works compiled with PicBasic and not when compiled with PicBasicPro.
I will check possible causes...
Thanks again.
Ricard.
Re: Bits, Bytes Words and Arrays
Hello,
I have more information.
I started commenting all the lines between this code, resulting the same. MR value is 0.
Next, I continued commenting all the lines outside this code. Result the same. MR value is 0.
Finally, I decided to comment step by step my array variables.
After 2 o 3 arrays commented I get MR=65535.
I repeated this process commenting other variables randomly with the same result. After 2 o 3 steps MR=65535.
My code is about 8k over a Pic16F877. Very fair but fits.
My conclusion is PicBasicPro compiler needs more memory for variables than PicBasic.
The weird is why the compiler gives no errors...
Many thanks,
Ricard.
Re: Bits, Bytes Words and Arrays
ok... I've built an 8x8x8 LED cube using 18F452, I have encountered a problem with my array
each of the 512 leds as 1 bit assigned to it from leddata array
0 = bottom corner
511 = the top corner of the cube
leddata VAR BYTE[64]
litled VAR WORD
litled = 511
leddata.0(lilted) = 1
the above 2 lines lights up an led half way down the cube (I think it would correspond to 255)
but if I write this:
leddata.0(511) = 1
the correct LED lights up at the top corner of the cube
Any ideas?
Re: Bits, Bytes Words and Arrays
You have an error in the variable litled.
leddata.0(lilted) = 1
It appears as lilted.
Ioannis
Re: Bits, Bytes Words and Arrays
awww that was an autocorrect on here, not in my code
changed litled to 'lit' still no change,
I've changed my code now to address each led using X,Y,Z
and it is working ok
xl = 7
zl = 7
yl = 7
GOSUB ledon
ledon:
ledonbyte = xl + (yl*8)
ledtemp = leddata[ledonbyte]
ledtemp.0(zl) = 1
leddata[ledonbyte] = ledtemp
RETURN
Re: Bits, Bytes Words and Arrays
Hi,
I had a similar issue a couple of years (5) ago. Like you I found that indexing using a constant works but indexing using a variable doesn't (for bits beyond 255). And you, just like me back then, didn't bother to check the manual...
Quote:
7.6.4 Accessing Arrays as Multiple Variable-Types
It is possible for a single array to be declared with multiple names and for each name to be assigned a different variable type. Note that offsets for BITs are limited to a maximum value of 255.
So, I Think Melanies post where she indicates that using a WORD as the pointer for indexing bits in a BYTE Array really isn't correct.
Here's my thread.
/Henrik.
Re: Bits, Bytes Words and Arrays
Thanks Henik
solved that one
1 Attachment(s)
Re: Bits, Bytes Words and Arrays
The other option is a bit of asm code.
note the code would be slightly different for a pic18