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?

asked Jan 7, 2011 at 23:34
\$\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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.