Combining 4 nibble to 16bits


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2004
    Location
    brighton
    Posts
    149

    Default Combining 4 nibble to 16bits

    Hi All
    I am looking to get some advise of how i can combine four nibble (4bits x 4)
    to give me 16bits so that i can shiftout the 16bits at once.
    The device i am reading can only end 4bits at a time so i would have to receive the data four times and store this into 4 variables Nibble1-Nibble2
    i would be receiving the data on PortD.0-PortD.3
    The tricky bit for me i that i want Nibble1 to corresponds to Msb posistion
    of my 16bits so for example if my values are as below

    nibble1=F
    nibble2=E
    nibble3=D
    nibble4=C
    TRISD=$FF

    Assuming at this point those values are in the var would this be ok
    ShiftOut DataP,Clk,MsbFirst,[nibble1\4,nibble2\4,nibble3\4,nibble4\4] 'Send 16bits out

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Perhaps something like this:
    Code:
    Result var word
    
    'Nibble 1 is the 4 most significant bits, Nibble4 the 4 least significant.
    'Most signinficant bit in nibble 1 is most significant bit in result.
    Result = (Nibble1 * 4096) + (Nibble2*256) + (Nibble3 * 16) + Nibble4
    Nibble1----Nibble2---Nibble3--Nibble4
    0 0 1 0 - 1 1 0 0 - 0 0 0 1 - 1 1 1 0 = 11294(dec)

    (2*4096)+(12*256)+(1*16)+14 = 11294

    /Henrik Olsson.
    Last edited by HenrikOlsson; - 11th November 2006 at 16:10.

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Hi Isaac,

    It would be more helpful if you could explain what you need in more detail.

    As much as I understood, reading the port pins individually could work for you.

    Nibbles are BITs, not bytes.

    nibble1 = PORTD.0
    nibble2 = PORTD.1
    nibble3 = PORTD.2
    nibble4 = PORTD.3

    Then you can take these four bits and use as you like.


    Or am I on the wrong track here?


    --------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    From what you have explained, here is something that should work:

    Code:
    Full_Word var Word
    Low_Byte  var Full_Word.lowbyte
    High_Byte Var Full_Word.Highbyte
    
    'First Nibble
    High_Byte = (PortD & %00001111)
    High_Byte = High_Byte << 4
    'Second Nibble
    High_Byte = High_Byte + (PortD & %00001111)
    'Third Nibble
    Low_Byte = (PortD & %00001111)
    Low_Byte = Low_Byte << 4
    'Forth Nibble
    Low_Byte = Low_Byte + (PortD & %00001111)
    'Full_Word now has the 16 bit result of 4 Nibbles
    HTH,
    Steve

    [i]EDIT: just changed port, since I miss read original post and used B instead of D
    Last edited by SteveB; - 12th November 2006 at 00:28.

  5. #5
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Cool

    Sorry sayzer

    if i didn't give enough explaination so here it is

    I have 4 variables of 4bits each
    Nibble1,Nibble2,Nibble3 and Nibble4
    Lets assume that they contain the following values

    nibble1=F which is binary 1111
    nibble2=E which is binary 1110
    nibble3=D which is binary 1101
    nibble4=C which is binary 1100

    i want to combine them to form a 16bit word variable say result
    to make result=FEDC or 1111111011011100

    Then i can use the shiftout command to send the value of result
    as

    ShiftOut DataP,Clk,MsbFirst,[result\16]

    instead of as below as i presently do it

    ShiftOut DataP,Clk,MsbFirst,[nibble1\4,nibble2\4,nibble3\4,nibble4\4]

    Those are read in on PORTD.0 - PORTD3 one at a time as the device
    i am reading is only capable of send 4bits at a time.
    i think SteveB method might work though


    Hope this is clear
    Isaac

  6. #6
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default

    Thanks Henrik

    i would give it a go just used my calculator and it comes out perfect

    Isaac

  7. #7
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    No improvement over Henrik's version except about half the code space

    Code:
    ;Result = (Nibble1 * 4096) + (Nibble2*256) + (Nibble3 * 16) + Nibble4
    
    Result.highbyte =(Nibble1<<4)+Nibble2
    Result.LowByte =(Nibble3<<4)+Nibble4
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i'm not sure you need to send it that way. I'm pretty sure it will work anyway if you send the whole word variable... no?

    Same thing for reading from... in one shot.

    Well i think. You never told what device you're using so...
    Last edited by mister_e; - 11th November 2006 at 21:48.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. 4 pin 4 x 4 keypad interface using pic basic pro
    By dunlao_john in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th January 2009, 05:21
  2. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  3. 16f88 - pin RA4 as analog input
    By savnik in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 15th December 2006, 13:55
  4. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 19:03
  5. having problems with Hantronix 20x4 lcd
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 22nd December 2005, 12:22

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