1
\$\begingroup\$

I'm writing firmware for a PIC16F877A in assembler, and I want to set some constants at the top of my source code to indicate which port is used for what, so that if needed I can easily change all the i/o pins around by changing the constant values and recompiling.

Like this:

O_LEDS   EQU      PORTA
CONSTANT O_RED    = RA1
CONSTANT O_GREEN  = RA2
CONSTANT O_BLUE   = RA3

In my code, I need a bitmask to represent the ports I'm using. My question is, how can I write an expression using assembler directives to calculate the bitmask?

Using my example above:

O_RED = 1
O_GREEN = 2
O_BLUE = 3

and the bitmask I want is:

movlw b'00001110'

If there was a to the power of operator, I could do something like this:

movlw (2 ^ O_RED) | (2 ^ O_GREEN) | (2 ^ O_BLUE)

but ^ in MPASM is a Bitwise exclusive OR, not to the power of.

Can anyone think of another way to do this?

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

I haven't used MPASM before, but does this work?

movlw (1 << O_RED) | (1 << O_GREEN) | (1 << O_BLUE)

Shifting to the left doubles a number.

1 << x == 2x

\$\endgroup\$
1
  • \$\begingroup\$ ah yes!!! I can't believe I didn't see that myself. Thanks. \$\endgroup\$
    – BG100
    Commented Jan 8, 2011 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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