Port masking and ports in pic

ccompressionpicport

i have data at portb on microprocessor (pic), i need to compress three bits rb3,rb4, and rb5, the other pins are various IO on port b, i wish to compress that info held in those three pins on port b, ignoring all others on port b, and present it as two bits on port A, pins RA3, RA4.

I just dont know how to apply bitmasks etc in this problem, i trust that is even the correct approach?

Best Answer

If you want to get a byte with RB3, RB4 and RB5 in the lower 3 bits (i.e. RB5 is the LSB), do this:

unsigned char uByte = ((RB3 << 2) | (RB4 << 1) | RB5);

Then do your compression, so assuming the lower two bits of uByte need to go to RA3 and RA4 (i.e. RA4 is the LSB), and set the PORTA bits like this:

RA3 = ((uByte >> 1) & 0x1);
RA4 = (uByte & 0x1);