PIC Microcontoller Bit Math Method

Rotate some bits without affecting others

What if you need to shift a 5 bit number in, through and out of a byte using bit0 to bit4 and not affect bit5 - bit7 because, for example, bits 5-7 are running the column select (via a 3 to 8) for a matix LED and the lower 5 bits are connected to the colum.

If you had two 8 bit wide registers next to each other like the following. The 5 bit value in byte 2 is shifted left throughout byte 1 and out.

byte1 byte2
XXX00000 xxx11111 start state
XXX00001 xxx11110
XXX00011 xxx11100
XXX00111 xxx11000
XXX01111 xxx10000
XXX11111 xxx00000
XXX11110 xxx00000
XXX11100 xxx00000
XXX11000 xxx00000
XXX10000 xxx00000
XXX00000 xxx00000 end state

X = can not be changed.
x = not used

Dmitry Kiryashov [zews at AHA.RU] says:

1. You only need to shift single one register.
 rlf byte1,W ;get new bit from carry
 xorwf byte1,W ;get difference
 andlw B'00011111' ;0's will mask unchanged bits
 xorwf byte1,F ;update only required bits
2. You need to shift in such way through many registers.
 rlf byte1,W ;get new bit from carry
 andlw B'00111111' ;mask msb's but last
 addlw B'11100000' ;copy last to carry
 xorwf byte1,W
 andlw B'00011111' ;see explanation of
 xorwf byte1,F ;this in example above
;then
;
 rlf byte2,W
 andlw B'00111111'
 addlw B'11100000'
;
;and so on as many bytes as you need ;)


file: /Techref/microchip/math/bit/rotpart.htm, 2KB, , updated: 2003年4月21日 07:34, local time: 2025年9月4日 04:10,
40.74.122.252:LOG IN

©2025 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/techref/microchip/math/bit/rotpart.htm"> PIC Microcontoller Bit Math Method Rotate some bits without affecting others</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here:
if you want a response, please enter your email address:
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

Welcome to massmind.org!

Welcome to techref.massmind.org!

.

AltStyle によって変換されたページ (->オリジナル) /