Bits, Bytes Words and Arrays


+ Reply to Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,358

    Default 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

  2. #2
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    Gday Melanie
    I just read this post and found it quite interesting.
    On 6mar2004 i posted on the Picbasic Pro forum, re Accessing the PICs ports in subroutines.
    Particularly the ability to access ports not covered by the std 0..15 references.
    I never found anything on this in the manual, but it meant i could put direct port addressing into subroutines, by always addressing the required port relative to porta.0 .
    Maybe you could modify this post to include access to ports by the referential aliasing method as well?
    Just a thought.
    Andrew

  3. #3
    Join Date
    Dec 2004
    Location
    USA
    Posts
    36


    Did you find this post helpful? Yes | No

    Question words into byte array

    Found this interesting bit of ancient history. It covers all the options except the one I'm looking for. I need to put some words into a byte array.

    settings VAR byte[1]

    counter1.byte1 VAR settings[0]
    counter1.byte0 VAR settings[1]

    -does not appear to work

    Klaus

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    6. How can I reference a WORD in a BYTE ARRAY?

    Let's say that you have a WORD located at bytes 3 and 4 of a BYTE array, and Byte 3 is the LowByte. You can use the EXT (external) modifier to assign it to a WORD variable.


    MyByteArray VAR BYTE[10]

    MyWord VAR WORD EXT
    @MyWord = _MyByteArray + 3

    DT

  5. #5
    Join Date
    Dec 2004
    Location
    USA
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel,

    Your example will put the low byte into the array. How do I get the high byte in?

    MyByteArray VAR BYTE[10]
    MyWord.byte0 VAR MyByteArray[3]

    or

    MyByteArray VAR BYTE[10]
    MyWord.byte1 VAR MyByteArray[3]

    will work as well.

    I can read in either the high or low byte, but not both of them.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    It creates an Aliased WORD variable, inside the BYTE array.

    When you assign a value to the WORD variable, it puts both BYTEs in the array in a single operation.

    MyWord = $1234

    will make MyByteArray look like this

    [00, 00, 00, 34, 12, 00, 00, 00, 00, 00]

    HTH
    DT

Similar Threads

  1. LP Instant Interrupts Error
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 7th December 2009, 09:13
  2. how to store a very large amount of data
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 2nd April 2008, 06:05

Members who have read this thread : 5

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts