Splitting up bytes question


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    88

    Default Splitting up bytes question

    Hello all,

    Not sure how to word this, but I’ll do my best. When writing a number to eeprom (such as the number 15), does it get stored in the 8 bits as 00001111? If so, can you break up the low and high bytes to enter two values of numbers ranging from 0-15?

    What I’m trying to do is record into eeprom two variables. Either of these numbers can have a value ranging from 0-15. As an example, I’d like to tell the chip to write 4 to the low byte side and write 5 to the high byte side. Doing so, the 8 bits would look like 01010100 (0101 for the number 5 and 0100 for the number 4). The data stored can then be read back by reading the low side, placing that value into one variable, and reading the high side and placing that value into a second variable.

    I’d like to store two sets of values per memory location to double my space. Is this possible?

    Thanks,
    Tony

  2. #2
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Use hexadecimal notation??

    For your example, store 54 hex at the byte location.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  3. #3
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    PBP does not have tools to make working with nibbles easier, but you can certainly do it yourself by shifting right and left.

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    ERMEGM,

    nibble1 var byte
    nibble2 var byte
    both var byte

    nibble1 = 7
    nibble2 = 10

    'to store the 2 nibbles:
    both = ((nibble1 & $0F) << 4)
    both = both | (nibble2 & $0F)
    'both now = $7A

    'to retrieve the nibbles
    nibble1 = both >> 4
    nibble2 = (both & $0F)
    Dave Purola,
    N8NTA
    EN82fn

  5. #5
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Quote Originally Posted by rmteo View Post
    Use hexadecimal notation??

    For your example, store 54 hex at the byte location.
    In my example, it would work until you get to 11 on the first number and say 2 on the second number. How would it store with 112? It could mean 11 and 2 or 1 and 12.

    Quote Originally Posted by Charlie View Post
    PBP does not have tools to make working with nibbles easier, but you can certainly do it yourself by shifting right and left.
    Getting back to my first question, if I use the code: WRITE 15, does it get stored as 1111 or 15? Just trying to see if I have to use if..then commands to translate the bits to numbers and vice-versa. The numbers will be used by the gosub command, so if 112 get's stored it must mean 11 and 2 and not 1 and 12.

    Dave,

    I will have to look into yours. Half of it makes sense, but I'm trying to interpret what the commands actually mean. Once I understand that, the rest will be easy.

    I guess I need the above question answered first, and then I can try to implement the above suggestions by having a clearer understanding.

    Thanks,
    Tony

  6. #6
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    With hexadecimal notation, 11 is B so the byte value to store is B2. Simple isn't it?
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Hi Tony,
    Each location or adress in the EEPROM is a byte, a byte is 8 bits. If you do WRITE 0, 15 you will write the value 15 to adress 0 of the EEPROM. If you read the value back into a variable the variable will contain the value 15, if you decide to "print" the value in binary you'll get 00001111, if you decide to print it in hex you'll get E - it's all the same just different ways for us humans to express it.

    If I understand your example with the numbers 11 and 2 correct you want 11 to be stored in the high nibble and 2 in the low nibble, resulting in the full byte containing 10110010, if we express that number in decimal form it's 178. One way to go from your two numbers into a single byte could be:
    Code:
    FullByte = Number_1 * 16 + Number_2
    See, 11*16+2=178.
    (Multiplying by 16 can be replaced with four left-shifts so FullByte = (Number_1 << 4) + Number_2 which is kind of what Dave is doing.)

    To get the value back into your two discrete variables you can do something like
    Code:
    Number_1 = FullByte >> 4  ' This will take the FullByte and shift it four steps to the left, the low nibble will be "pushed" out
    Number_2 = FullByte & %00001111   ' This will mask the high nibble.
    /Henrik.

  8. #8
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    All the answers to the original question are correct - thanks Henrik for spelling out the details better than the rest or us - you just have to choose the one that suits your style of thinking best!

    To expand on my shift right and left, you would first write the high nibble, then shift left 4 times, then write the low nibble.
    To recover you would shift right 4 times to get the high nibble, and to get the low nibble, shift left 4 times, then shift right 4 times. (Clear as mud?)
    Basically, make the bits you don't want fall off the closest end, then make sure the ones you want are in the low nibble position.

    Some other compilers have tools for directly accessing nibbles, but the need is pretty rare.

  9. #9
    Join Date
    Jan 2010
    Posts
    88


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Quote Originally Posted by HenrikOlsson View Post
    Hi Tony,
    Each location or adress in the EEPROM is a byte, a byte is 8 bits. If you do WRITE 0, 15 you will write the value 15 to adress 0 of the EEPROM. If you read the value back into a variable the variable will contain the value 15, if you decide to "print" the value in binary you'll get 00001111, if you decide to print it in hex you'll get E - it's all the same just different ways for us humans to express it.

    If I understand your example with the numbers 11 and 2 correct you want 11 to be stored in the high nibble and 2 in the low nibble, resulting in the full byte containing 10110010, if we express that number in decimal form it's 178. One way to go from your two numbers into a single byte could be:
    Code:
    FullByte = Number_1 * 16 + Number_2
    See, 11*16+2=178.
    (Multiplying by 16 can be replaced with four left-shifts so FullByte = (Number_1 << 4) + Number_2 which is kind of what Dave is doing.)

    To get the value back into your two discrete variables you can do something like
    Code:
    Number_1 = FullByte >> 4  ' This will take the FullByte and shift it four steps to the left, the low nibble will be "pushed" out
    Number_2 = FullByte & %00001111   ' This will mask the high nibble.
    /Henrik.
    Hi Henrik,

    Thanks for the reply and explanation. What I'm trying to do is store two separate values. Each value can be incremented separately, that's why I need to be able to store digits separately, so they can't be misread. What I am seeking to do is store both values in one byte of eeprom. I am starting to understand how to shift them in and out, what I would like clarification on is when you say they are pushed out, does the value still stay in that byte of eeprom, or are the values shifted out like a shift register? I need to be able to go back to reference those numbers again and a again. If they are "shifted out" then they are gone, right?

    It's easy to understand when you know the answer. This is my first time working with shifting, so I'm kind of curious how creative I can be to use these features. I am just trying to make the best use of space I can in eeprom.

    The best analogy I can think of when trying to explain what I'm doing is like when working with a chart. You have so many blank entries on a page (eeprom available bytes). When you go to write your pairs of number in the blank spaces, if you enter one number on one blank line and the second on line two, you fill up the sheet twice as fast.

    If you squeeze both numbers in on one line, you double the amount of data you can store, however you have to make some kind of separator to make sure the numbers are not merged together. We would normally put a space or a dash or slash when we write it, but for the pic we shift. This is what I've gathered so far from the replies. Does that sound about right?

    Thanks for all the input guys.

    Tony

  10. #10
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  11. #11
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: Splitting up bytes question

    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

Similar Threads

  1. splitting output from a counter
    By astouffer in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th September 2009, 21:21
  2. Please help: Splitting HROUT
    By luminas in forum Serial
    Replies: 9
    Last Post: - 1st June 2008, 14:52
  3. Splitting numbers and recombining them(EEPROM Error)
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd June 2007, 06:40
  4. Splitting a byte?
    By Deadeye in forum General
    Replies: 3
    Last Post: - 29th May 2005, 21:58

Members who have read this thread : 0

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