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.

MSP430G2553: Confusion regarding setting a bit in a register

Part Number: MSP430G2553

This is a very basic question. The way we set bits in a register of msp430 is different from the commonly used method. Please go through the example given below :

Let's assume there is a register R and its n'th bit is named as X in datasheet.

The way we set the X bit (n'th bit) for most other MCUs (like AVR) is as follows :

R |= (1 << X) ;

This is same as: R |= (1 << n) ;

So, X = n

But for MSP430, I see it's done like this:

R |= X ;

This is NOT same as: R |= n ;

So, X != n

I am confused here. Does that mean the Register bit names are defined differently in msp430's header files?

  • > I am confused here. Does that mean the Register bit names are defined differently in msp430's header files?
    Differently from the AVR, yes. The AVR has instructions to set/clear a (single) bit by bit number, so (20 years ago when assembly was everything) that was how the bits were defined. Once avr-gcc became a Thing, the association became looser, and so the __BV(n): (1 << n) macro came about. (I think the optimizer still replaces "|=" on a single bit with e.g. SBI.)

    MSP430 instructions used bit masks, so e.g. BIT2: (1 << 2) was natural. Over 40 years of writing assembly, I'd say the AVR is actually in the minority.

    [Edit: I remembered wrong -- SBR does use a mask.]

  • Thank you so much for your explanation. I didn't expect an answer so early. Now I understand the way register bits are set here, thank you once again.

**Attention** This is a public forum