0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ So you need to compress 3 bits to 2 bits? You mean you want to represent 7 different combinations in two bits? I guess you mean you have a truth table somewhere, or a logical relationship? \$\endgroup\$ – Roger Rowland Mar 13 '15 at 8:04
0
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ thanks Roger, will have to study whats going on there, i will try to implement that. \$\endgroup\$ – StevieB Mar 13 '15 at 8:20
  • \$\begingroup\$ think i need to make RB5 the MSB? that would be normal convention,,, or am i missing a clever programming trick here, ? \$\endgroup\$ – StevieB Mar 13 '15 at 8:24
  • \$\begingroup\$ @StevieB There's no clever trick, I'm just shifting the bits to some required order. Just replace RB3/4/5 in the order you need (MSB to LSB in the lower 3 bits of the byte). If you want to shift all the way to bit 7 of uByte, adjust the shift amounts - e.g. ((RB5 << 7) | (RB4 << 6) | (RB3 << 5)). \$\endgroup\$ – Roger Rowland Mar 13 '15 at 8:25
  • \$\begingroup\$ @Downvoter - please explain what you think is wrong with my answer so I can improve it. \$\endgroup\$ – Roger Rowland Mar 24 '15 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.