Loading data into ONLY 4 bits of a port - is it possible?


Closed Thread
Results 1 to 7 of 7
  1. #1

    Red face Loading data into ONLY 4 bits of a port - is it possible?

    Hi to all,

    I am trying to use a port split between 2 variables.

    For example I want to load PORTC upper 4 bits with 4 bits from a variable WITHOUT altering the lower 4 bits of the port. I will also need to similarly load the lower 4 bits of the port with data from another variable again without changing the state of the upper 4 bits.

    I know I could do this bit by bit but It would really make things much simpler if I could do it by loading either lower or higher in one move. Is it possible?

    From the manual there is- anyvar = PORTB & $0f ‘ Isolate lower 4 bits of PORTB and place result into anyvar.

    Could that be reversed?

    Again baffled, Al

  2. #2
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Loading data into ONLY 4 bits of a port - is it possible?

    The standard way to do this is read-modify-write. To change lower 4 bits only, read all 8 bits, strip off lower 4 bits, add your new value, then write 8 bits back. To change upper 4 bits only is more complicated, but also doable (hint - use shift left / shift right). Still, this is unnecessarily complicated for what you want to do... just do it bit by bit. Place the code in a subroutine and call it to read/write your values for simplicity - use it like a new command.

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Loading data into ONLY 4 bits of a port - is it possible?

    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,601


    Did you find this post helpful? Yes | No

    Default Re: Loading data into ONLY 4 bits of a port - is it possible?

    Quote Originally Posted by Bigalscorpio View Post
    ...

    From the manual there is- anyvar = PORTB & $0f ‘ Isolate lower 4 bits of PORTB and place result into anyvar.

    Could that be reversed?

    Again baffled, Al

    What happens if you try with $f0?

    Robert

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,601


    Did you find this post helpful? Yes | No

    Default Re: Loading data into ONLY 4 bits of a port - is it possible?

    Just a note, I prefer to use binary instead of hex.

    %11110000 is the same as $f0, except it is easy for a guy like me to use %11100000 if I want to refer to top 3 pins only.

    There no way I can remember the hex equivalent without using a conversion tool.

    Robert

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Loading data into ONLY 4 bits of a port - is it possible?

    You could read portb to a word variable, multiply it, read the high byte into buffera,
    zero the high byte, multiply the word var again, and copy the new high byte to bufferb.
    This would probably end up more complicated in compiled code than just shifting it.

  7. #7
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: Loading data into ONLY 4 bits of a port - is it possible?

    Hi Al (and gang),

    Can you better explain what you're trying to do, please? For example, are you trying to take bits b3..b0 in "varA" and copy them to PORTX bits RX3..RX0 and then copy bits b3..b0 in "varB" to PORTX bits RX7..RX4? It so, can you rely on "varA" and "varB" containing only 4 bits of data and simply combine them and write a single byte to PORTX which contains all 8 bits of data?

    If you're doing something else, you could always move the 'source' bits into the proper 'target' bit position and then use XOR and AND instructions to copy them to the target. Here's an (assembler) example;

    Code:
    ;
    ;  copy "varA" b3..b0 to PORTX b7..b4
    ;
            swapf   varA,W          ; move b3..b0 to b7..b4
            xorwf   PORTX,W         ; differences, hi or lo
            andlw   b'11110000'     ; don't change target b3..b0
            xorwf   PORTX,F         ; apply new b7..b4 bits

Similar Threads

  1. Assign different port bits to an array?
    By vamtbrider in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th November 2010, 02:40
  2. loading data to EXt. eeprom
    By MINHLE in forum General
    Replies: 0
    Last Post: - 28th December 2009, 17:56
  3. Serin2 data bits
    By Luckyborg in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th February 2009, 22:10
  4. 16F88 7 data bits
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 17th February 2008, 01:05
  5. pre loading eeprom data
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th April 2004, 11:18

Members who have read this thread : 1

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