1
\$\begingroup\$

I need to know how to check whether a byte contains a 1 for touch status in a MPR121 project, so if:
00000010 then pin 2 is touched
00000100 then 3 is touched, how can i know it?
A set bit means that a pin is touched it doesnt matter whether there are more 1s, I just want to know if there is any 1, because that means that a pin is touched not caring which is that particular pin

I tried
R1-byte container

CJNE R1,#0, hasone
hasone:
   ....

But I don’t know if this works since is comparing a hex with a number, I can do it comparing with every single solution but I don’t like the approach. If anyone can help. Thank you!

\$\endgroup\$
  • 4
    \$\begingroup\$ Do you need to know which bit is 1 or just that any is 1? \$\endgroup\$ – Eugene Sh. Mar 18 at 18:18
  • 4
    \$\begingroup\$ What result do you want in case more than one of the bits is 1? \$\endgroup\$ – The Photon Mar 18 at 18:26
  • 2
    \$\begingroup\$ Also, you might want to review the JZ and JNZ instructions. \$\endgroup\$ – The Photon Mar 18 at 18:35
  • 1
    \$\begingroup\$ This risks being closed as unclear what you are asking. If the register has any bits set to one the value the register will contain anything other than 0x00. If you want to know if bit zero is set then if ((R0 & 0x01) != 0) it is, for bit 2 if ((R0 & 0x02) != 0) it is, for bit 3 if ((R0 & 0x04) != 0) etc. \$\endgroup\$ – Warren Hill Mar 18 at 18:36
  • 2
    \$\begingroup\$ @ÇlirimMurati, then you're back to looking at the JZ or JNZ instruction. \$\endgroup\$ – The Photon Mar 18 at 20:15
1
\$\begingroup\$

If the byte is NOT 00000000, then at least one bit is non-zero. All you need to know is it is 00000000 or not.

\$\endgroup\$
0
\$\begingroup\$

To avoid all those if statements, you could use an 8 bit lookup table. The code will execute much faster than performing all the if-then-else statements. For the values you've indicated, 0x01, 0x02, etc the table below returns the number of the button pressed. But there are a lot of 0xUU in the table. 0xUU means that the value has not been defined by you yet, as the comments have indicated. You must decide what you want to return for those values and then update your lookup table. You might set them to 0xFF, for example, if you wanted to detect an error.

 char lookup[] = {
    0x00, 0x01, 0x02, 0xUU, 0x03, 0xUU, 0xUU, 0xUU,
    0x04, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0x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xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU,
    0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU, 0xUU
 }
 char result = lookup[R1];
\$\endgroup\$
  • \$\begingroup\$ I mentioned it has to be assembler 8051 \$\endgroup\$ – Çlirim Murati Mar 18 at 20:03
  • 4
    \$\begingroup\$ This isn't really a free consulting service, you might have to do some work yourself. \$\endgroup\$ – Cristobol Polychronopolis Mar 18 at 20: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.