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?
1 Answer 1
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
-
\$\begingroup\$ ah yes!!! I can't believe I didn't see that myself. Thanks. \$\endgroup\$BG100– BG10001/08/2011 00:38:05Commented Jan 8, 2011 at 0:38