EmbeddedRelated.com
Forums

how to access I/O pin of MSP430?

Started by sona...@yahoo.co.in July 12, 2010
Hello everyone,
i am new in MSP430. i am using MSP430FE427 MCU. i just want to toggle LED connected to I/O pin. according to pin number,i've set PxDIR,PxSEL,PxOUT registers. my question is can i access particular pin only means bitwise.For example P2.4 = 1

Beginning Microcontrollers with the MSP430

Use P2OUT &= ~BIT4 to set the pin low.
Use P2OUT |= BIT4 to set it high.
Use P2OUT ^= BIT4 to toggle it.

Be sure to include the header file that defines the bits.

--- In m..., sona.bera@... wrote:
>
> Hello everyone,
> i am new in MSP430. i am using MSP430FE427 MCU. i just want to toggle LED connected to I/O pin. according to pin number,i've set PxDIR,PxSEL,PxOUT registers. my question is can i access particular pin only means bitwise.For example P2.4 = 1
>

Hello,

You can do like this:
P2OUT |= BIT1 - turn BIT1 to high
P2OUT &= ~BIT1 - turn BIT1 to low

You can do this with multiple bits
P2OUT |= BIT1 + BIT4 + BIT5 (turn P2.1,.4,.5 on)

Also I have read you can do something like
P2OUT.BIT1 = 1 - turn on
P2OUT.BIT1 = 0 - turn off

But this way, I have not tried yet.

P/S: play with "|=" and "&= ~BITx" should help you a lot.

Regards,
Anh
--- In m..., sona.bera@... wrote:
>
> Hello everyone,
> i am new in MSP430. i am using MSP430FE427 MCU. i just want to toggle LED connected to I/O pin. according to pin number,i've set PxDIR,PxSEL,PxOUT registers. my question is can i access particular pin only means bitwise.For example P2.4 = 1
>

--- "Hoai Anh" wrote:
> ...
> Also I have read you can do something like
> P2OUT.BIT1 = 1 - turn on
> P2OUT.BIT1 = 0 - turn off
>
> But this way, I have not tried yet.
> ...

Yes, I have seen people use c-statements like this:

P2OUT_bit.P2OUT_1 = 1; //Turn on BIT1 of P2OUT
P2OUT_bit.P2OUT_1 = 0; //Turn off BIT1 of P2OUT

It works because the header file they use defines:

__no_init volatile union
{
unsigned char P2OUT;

struct
{
unsigned char P2OUT_0 : 1;
unsigned char P2OUT_1 : 1;
unsigned char P2OUT_2 : 1;
unsigned char P2OUT_3 : 1;
unsigned char P2OUT_4 : 1;
unsigned char P2OUT_5 : 1;
unsigned char P2OUT_6 : 1;
unsigned char P2OUT_7 : 1;
} P2OUT_bit;
} @ 0x0029;

enum {
P2OUT_0 = 0x0001,
P2OUT_1 = 0x0002,
P2OUT_2 = 0x0004,
P2OUT_3 = 0x0008,
P2OUT_4 = 0x0010,
P2OUT_5 = 0x0020,
P2OUT_6 = 0x0040,
P2OUT_7 = 0x0080,
};

I suppose one could also use c-statements like this:

CLAP_ON; //Turn on BIT1 of P2OUT
CLAP_OFF; //Turn off BIT1 of P2OUT

Provided that the header file includes lines like:

#ifdef XMAS_TV_AD
#define CLAP_ON P2OUT |= BIT1
#define CLAP_OFF P2OUT &= ~BIT1
#endif

Many c-programmers want to show that their c-statements are simple and easy to read at the expense of complex and hard to follow header files.

O_C_Y - right on. I hate having to plow through header defines to find out what a statement is really doing.
--- In m..., "old_cow_yellow" wrote:
>
> --- "Hoai Anh" wrote:
> > ...
> > Also I have read you can do something like
> > P2OUT.BIT1 = 1 - turn on
> > P2OUT.BIT1 = 0 - turn off
> >
> > But this way, I have not tried yet.
> > ...
>
> Yes, I have seen people use c-statements like this:
>
> P2OUT_bit.P2OUT_1 = 1; //Turn on BIT1 of P2OUT
> P2OUT_bit.P2OUT_1 = 0; //Turn off BIT1 of P2OUT
>
> It works because the header file they use defines:
>
> __no_init volatile union
> {
> unsigned char P2OUT;
>
> struct
> {
> unsigned char P2OUT_0 : 1;
> unsigned char P2OUT_1 : 1;
> unsigned char P2OUT_2 : 1;
> unsigned char P2OUT_3 : 1;
> unsigned char P2OUT_4 : 1;
> unsigned char P2OUT_5 : 1;
> unsigned char P2OUT_6 : 1;
> unsigned char P2OUT_7 : 1;
> } P2OUT_bit;
> } @ 0x0029;
>
> enum {
> P2OUT_0 = 0x0001,
> P2OUT_1 = 0x0002,
> P2OUT_2 = 0x0004,
> P2OUT_3 = 0x0008,
> P2OUT_4 = 0x0010,
> P2OUT_5 = 0x0020,
> P2OUT_6 = 0x0040,
> P2OUT_7 = 0x0080,
> };
>
> I suppose one could also use c-statements like this:
>
> CLAP_ON; //Turn on BIT1 of P2OUT
> CLAP_OFF; //Turn off BIT1 of P2OUT
>
> Provided that the header file includes lines like:
>
> #ifdef XMAS_TV_AD
> #define CLAP_ON P2OUT |= BIT1
> #define CLAP_OFF P2OUT &= ~BIT1
> #endif
>
> Many c-programmers want to show that their c-statements are simple and easy to read at the expense of complex and hard to follow header files.
>