This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

macro setbit(port,bit) in MSP430 launchpad

Hello every body,

i try the macro setbit(port,bit) ...

#define setbit(p,b) ((p)|=(b))
#define clearbit(p,b) ((p)&= ~(b))
#define testbit(p,b) ((p)&(b))
#define toglebit(p,b) ((p)=(p)^(b))

but i m a beginner and i think i must define first the funktion so that´s why i got the error "p and b not define" 

i need a help...

  • Aymen Ben said:
    i try the macro setbit(port,bit) ...

    Using these macros isn't recommended when programming a microcontroller. Each of them is a register access whcih may have side-effects. Testing for one bit may clear all others just because the register has been read.
    The MSP does not provide bit-level addressing, so accessing a bit of a register always means reading and writing a full byte.

    To provide a visible and obvious relation between what the code does and what effect it may have, you should simply use the normal arithmetics.
    This also has some advantages: you can combine access to more than one bit into one single instruction, which makes the code smaller and faster. It also works with register bitfields that span more than one bit (selections, e.g. clock selection, divider selection etc.)

    So for setbit(P2OUT, BIT0), you can just use
    P2OUT |= BIT0;
    or
    P2OUT |= (BIT0|BIT1); to combine two bit sets into one instruction.

**Attention** This is a public forum