Log in

View Full Version : Loading data into ONLY 4 bits of a port - is it possible?



Bigalscorpio
- 19th October 2012, 23:35
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

Charlie
- 20th October 2012, 11:58
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.

mackrackit
- 20th October 2012, 15:34
This post may help too.
http://www.picbasic.co.uk/forum/showthread.php?t=2611&p=13888#post13888

Demon
- 21st October 2012, 01:34
...

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
:)

Demon
- 21st October 2012, 01:41
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

Art
- 26th October 2012, 12:46
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.

Mike, K8LH
- 26th October 2012, 14:27
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;


;
; 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