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.

Define a bit in register

Other Parts Discussed in Thread: MSP430F1611

I am using MSP430F1611 for my purpose.
I want to define a particular bit in a register or a particular pin as an indicator.

For eg. I want to set pin6 of port2 as an indicator named LED for which I use:
#define LED   P2OUT_bit.P2OUT_6

but this returns an error and says it is not defined.

Could you please provide some alternatives?Anticipating a reply at the earliest!
Regards, 

 

  • Hi,


    You cannot access individual bits inside a register. You must define a structure.

    Best regards,

    AES

  • You can use:

    #define    LED 0x40

     

    later in your main :

    P2DIR |= 0xFF;

    P2OUT = LED;

    Regards

    Gastón

  • Hi Rahul,

    you can use a bitfield for dealing with variables.

    // own data type definition

    struct MYBITFIELD
      {
        unsigned char FLAG0 :1;
        unsigned char FLAG1 :1;
        unsigned char FLAG2 :1;
        unsigned char FLAG3 :1;
        unsigned char FLAG4 :1;
        unsigned char FLAG5 :1;
        unsigned char FLAG6 :1;
        unsigned char FLAG7 :1;
        unsigned char FLAG8 :1;
        unsigned char FLAG9 :1;
        unsigned char FLAG10 :1;
        unsigned char FLAG11 :1;
        unsigned char FLAG12 :1;
        unsigned char FLAG13 :1;
        unsigned char FLAG14 :1;
        unsigned char FLAG15 :1;
      };

    // define variable
    struct MYBITFIELD STATUS;    // define a global variable of type MYBITFIELD - variable name is STATUS

    // Symbolic names for STATUS flags
    // these symbolic names were used in the program when accessing STATUS bits
    #define bmybitone STATUS.FLAG0
    #define bmybittwo STATUS.FLAG1
    #define bmybitthree STATUS.FLAG2

     

    you can now use the bitfield i.e. like:
    STATUS.FLAG0 = 1; // set bit to one..

    bmybittwo = 1; // set bit one
    ..

    while(bmybitthree == 0)

    {

    }

    Bitfield were accessed like a structure --> you can use the programming style for accessing them your most comfortable with.

     

    In case of the MSP430 registers you can define macros like:

    // LED
    #define LED_ON      P2OUT |= BIT6       // output HIGH --> switch on LED

    #define LED_OFF     P2OUT &= ~BIT6      // output LOW --> switch off LED
    #define LED_TOGGLE  P2OUT ^= BIT6       // toggles P2.6

    Now, you can simply write:

    LED_ON; // switch LED on
    LED_TOGGLE; // toggle LED

     

    Kind regards
    aBUGSworstnightmare

  • Try this

    #define LED (((struct LED_Struct*)&P2OUT)->LED_6)
    struct LED_Struct
    {
      unsigned char reserve1 : 6;
      unsigned char LED_6    : 1;
      unsigned char reserve2 : 1;
    };

    Then you can use the macro LED just like any 1 bit variable as in

    LED = 1;

    or

    if(LED) { /* ... */ }

    You may need to invert the order in the structure depending on how your compiler places bit fields in structures but otherwise all should work fine.

    Jim Noxon

  • While bitfields are a nice play for the lazy programmer, they have a significant drawback ´: each bit set or clear results in an individual access to the register. You cannot bundle several into one.
    On a port pin, it might be useful, but on other registers, it will pump up code size and slow down execution.
    So these bitfields are not recommended and in most cases not defined.

  • True JMG, nothing comes without some tradeoffs.  One of the bigger issues with bit fields happens with ISR's where a flag is cleared using a read-modify-write operation which turns out to not be atomic thus providing a small window whereby an interrupt can be dropped inadvertently.

    However, there are also some real bennefits of doing this such as type validation at compile time etc.

    If you really want to provide the ability to update multiple fields in a register you can always use a union as in

    union utag
    {
      struct
      {
        unsigned char field1 : 3;
        unsigned char field2 : 2;
        unsigned char field3 : 2;
        unsigned char field4 : 1;
      }; fld;
      unsigned char reg;
    };

    In this case, you can access the full register if you want to update multiple fields simultaneously but you also have access to the individual bit fields.  There are usually many places where you only need to read or update a single field and, in these cases, bit field structures work no differently than a macro would.

    Jim Noxon

  • The MSP430 does have some defines that are done already. For instance, P1OUT |= BIT1;  will set an output pin high, P1OUT &= ~BIT1; will take it low.

    You could take the specific bit and also name it as LED, and then do the same thing.

    If you look at the PowerPac RTOS example on the IAR site, they have done some creative things with the LED...

     

    Regards,

    Todd Anderson

  • I just took some code and added a #define for LED:

    #define LED (BIT0)

    in my code, I changed my BIT0 reference to "LED."

     // Code to toggle P1.0 LED.
     P1OUT ^= LED;  // (was BIT0;)

    That is pretty quickly done, with no additional code.

    Hopefully that helps.

    Todd Anderson

**Attention** This is a public forum