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.

How to check whether a flag is set or clear

Other Parts Discussed in Thread: MSP430G2231

Hi,

  I am new to MSP430 & CCS .I  have earlier worked on PIC & AVR devices.

I would like to know how can I Check whether a particular bit is SET or CLear using C languge .in CCS v4.

My Controller is MSP430G2231.

 

  • Example: Check if bit 0 is set.  Using the #include <msp430.h> will pull in a definition of BIT0 as:

    #define BIT0 (1 << 0)

    if (var & BIT0)
    {
      // Do something if Bit 0 is set

    }
    else
    {
      // Do something else if Bit 0 is not set (ie. clear)
    }

     

  • By the way, if you would like to see some code examples for the particular series of MSP430 you are targeting, go to the following link below.

    http://focus.ti.com/mcu/docs/mcusplash.tsp?contentId=128826

  • Ratanlal Yadav said:
    how can I Check whether a particular bit is SET or CLear using C languge .in CCS v4.

    Just like you do it in C language. That's why it is C language and not MSP language.

    In C, checking bits in a bitfield is done as Brandon described. No matter whether it is C for MSP430 or for PC or MAC or whatever.

    That the PICs have one-bit-registers (bit-addressable ports), is somewhat outside the scope of the C language.

    However, if you want, you can simulate something like this with C:

    typedef union port {
      volatile char reg_p;
      volatile struct {
        char __p0:1,
          __p1:1,
          __p2:1,
          __p3:1,
          __p4:1,
          __p5:1,
          __p6:1,
          __p7:1;
      } __pin;
    };

    #define pin0    __pin.__p0
    #define pin1    __pin.__p1
    #define pin2    __pin.__p2
    #define pin3    __pin.__p3
    #define pin4    __pin.__p4
    #define pin5    __pin.__p5
    #define pin6    __pin.__p6
    #define pin7    __pin.__p7

    #define inport (port)P1IN
    #define outport (port)P1OUT

    Then you can do

    outport.pin5 = 1

    or

    if (inport.pin3)

  • Jens-Michael Gross said:

    #define inport (port)P1IN
    #define outport (port)P1OUT

    Then you can do

    outport.pin5 = 1

    or

    if (inport.pin3)

     

    #define inport (port)P1IN
    #define outport (port)P1OUT

    I am not getting what is  P1IN & P1OUT & what is the use of it . will you please make it clear  for me?

  • P1IN and P1OUT are defined in the header file mentioned before and are defined to be the address of the P1IN (Data In) register and P1OUT (Data Out) register of Port 1.

    You can find these in the device datasheet in terms of their specific address and in the family user's guide in the Digital I/O section.

**Attention** This is a public forum