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?

up vote 6 down vote accepted

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

  • ah yes!!! I can't believe I didn't see that myself. Thanks. – BG100 Jan 8 '11 at 0:38

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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