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.

Setting or clearing port bits

Other Parts Discussed in Thread: MSP430G2955

I am using an MSP430G2955 and writing the code using CCS with the MSP430-GCC compiler.  In the past with GCC under linux I have used the following line of code with no problems.

        P4OUT &= ~BIT0+BIT1;

It would clear bits 0 and 1 and leave all others unchanged.  However while using the GCC that comes with CCS version 6.1.0.00104, this line of code is clearing all Port 4 output bits.  I changed the code to   

        P4OUT &= ~BIT0;.

        P4OUT &= ~BIT1;

It now does what I want it to do.  But my question is has something changed in GCC that caused this, was it intentional, and what other changes have been made.  

  • I cannot answer your question regarding the changes of GCC, but maybe the order of the operators causes a problem. Have you tried writing

    P4OUT &= ~(BIT0 + BIT1);

    I would recommend to use the OR-operator instead of adding the bits together:

    P4OUT &= ~(BIT0 | BIT1);

    But adding them works, of course.

    Dennis

  • Yes, the parentheses are required here. The bitwise NOT operator has higher precedence than both addition and bitwise OR. That means ~BIT0+BIT1 == (~BIT0)+BIT1, rather than ~(BIT0+BIT1). I suspect if this worked previously it was due to undefined/unspecified/implementation-defined behaviour.

  • In IAR the compiled code:

    P2OUT &= ~BIT0+BIT1; 
    93C2 0029          tst.b   &P2OUT
    43C2 0029          clr.b   &P2OUT
    
    P2OUT &= ~(BIT0+BIT1); 
    F0F2 00FC 0029     and.b   #0xFC,&P2OUT

    As you can see the first one makes no sense
    So always use (..) and also always use them in macro's too.
    I kind of wish that C had a andnot symbol with lower priority, so and + inverted (..) did not have to be used.

  • Thank you all for your help. It is greatly appreciated.

**Attention** This is a public forum