Searching \ for '[PIC]: bit shifting in C' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=c
Search entire site for: 'bit shifting in C'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: bit shifting in C'
2002\09\18@174935 by Bob Japundza

flavicon
face
Hi All,
I have a silly question.  I am writing some code for an 877 to control an array of 16 led's.  Here's a snippet of my code, which also shows what pins the led's are connected to:
// Right 8 LED's, from center: RA0, RA1, RA2, RB0, RB1, RB2, RB3, RB4
// Left 8 LED's, from center: RD0, RD1, RD2, RD3, RD4, RD5, RD6, RD7
 //handle right segment
PORTA = bRightSegment << 3 | PORTA;
PORTB = bRightSegment >> 5 | PORTB;
 //handle left segment
PORTD = bLeftSegment << 8;
bRightSegment and bLeftSegment are byte variables.  So, if I want to turn three LED's on the right segment, I'd set bRightSegment to be 0xE0, two would be 0xC0, etc. Since I want the remaining bits in PORTA and PORTB to not change (they are used for other functions) am I doing this correctly?  Basically, I just want to change the state of the first three bits of porta and the first five bits of portb, without doing it bit-by-bit.
thanks
Bob

--
http://www.piclist.com hint: To leave the PICList
spam_OUTpiclist-unsubscribe-requestTakeThisOuTspammitvma.mit.edu


2002\09\18@183951 by Dipperstein, Michael

face picon face
{Quote hidden}

If I understand what you're trying to do this won't work.

PORTA = bRightSegment << 3 | PORTA;

means shift bRightSegment 3 to the left, bitwise OR it with PORTA, and assign
the value to PORTA.

Let abcdefgh be the eight bits of bRightSegment.  bRightSegment << 3 = defgh000.
The bitwise OR with PORTA may now change all but RA2, RA1, and RA0 which are the
bits you actually want to change.

Also, if you're trying to change a PORTA bit from 1 to 0, there you can't do
that with an OR.  One technique is to AND the value with mask that sets the bits
low, then OR in the value you want.  e.g. PORTA = (PORTA & F8) | (bRightSegment
>> 5)

Because of the way you define your LEDs, the code to set port bits is rather
complex.  RA0 is the least significant bit of PORTA, but you want to use the
most significant bit in byte to store its value.  Similarly RA1 is the second
least significant bit of PORTA, and you want to use the second most significant
bit in a byte to change its value.

If you have the luxury, you should probably change the way your bits and/or LEDs
are defined.  Otherwise you will probably need write some code to reverse the
bit orders.

-Mike

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestspamKILLspammitvma.mit.edu


2002\09\18@185030 by Bob Japundza

flavicon
face
Thanks, Mike, that makes sense to me.  I didn't think I was doing it correctly...

{Quote hidden}

--
http://www.piclist.com hint: To leave the PICList
KILLspampiclist-unsubscribe-requestKILLspamspammitvma.mit.edu


2002\09\19@114737 by llile

flavicon
face
Bob Japundza wrote:

>bRightSegment and bLeftSegment are byte variables.  So, if I want to turn
three LED's on the right segment, I'd set bRightSegment to be 0xE0, two
would be 0xC0, etc. Since I want the remaining bits in PORTA and PORTB to
not change (they are used for other functions) am I doing this correctly?
Basically, I just want to change the state of the first three bits of
porta and the first five bits of portb, without doing it bit-by-bit.



I've got just the thing for you, Bob.  I had a similar situation where I
needed to left-shift some bits in a port, but leave two in the middle
alone.  I did it by creating a mirror byte (NEVER NEVER do left- or right-
shift operations on a ports because you risk read-write-modify bug, do
them on a mirror byte then mov that into the port)

I'll post what I did maybe it will give you some ideas:


--Lawrence

// Makes LEDs on portC zip from left to right, chaser light effect, only
uses RC0,1,2,5,6,7 leaves RC3 and RC4 alone
void Flashy(){
int preflash;        // mirror byte
int mirrorBtemp;
int counter1;
int counter0;

       tempflash = 48;  // counter
       preflash = 0;


       do{

               restart_wdt();


               preflash <<= 1;         // illuminate 1 more LED to the
left


               if(!bit_test(preflash, 6)){     // if no overflow
                       preflash |= 0x01;                       //
re-illuminate LED#1
               } // else de-illuminate lower LEDs

               if(preflash == 0B11000000){     // roll over after 6 bits
                       preflash = 0;
               }


               maskflash = preflash & 0B00111000;              // parse
the mirror byte into a pattern matching actual LEDS
               maskflash <<= 2;

               mirrorC =  preflash & 0B0000111;

               mirrorC |= maskflash;

               mirrorC &= 0B11100111;

               portC = mirrorC;


       }while (--tempflash < 48);

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\09\19@115759 by Bob Japundza

flavicon
face
I was thinking about using a temp variable and decided it was unnecessary until you mentioned the read-write-modify bug.  Could you explain that further?

thanks,
Bob

> {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...