Understanding P1DIR |= 0x01

Skip to first unread message


Dec 23, 2010, 11:15:35 PM12/23/10
to TI Launchpad
Hi guys, I don't understand how the registers are linked or mapped in
the c include file.

I see a line of code like this:

P1DIR |= 0x01;

I see this variable P1DIR that suppose "map" to a register inside the
MSP430, but I understand how this happening. Where is this mapping
being defined, how it mapped to P1DIR and not some other register
inside the controller?

The corresponding line of code in the include file is:

extern volatile unsigned char address P1DIR

where does it say that P1DIR is the register inside the MCU?

Paolo Di Prodi

Dec 24, 2010, 5:43:31 AM12/24/10
to ti-lau...@googlegroups.com
The msp430 compiler-linker will take care of it.
P1DIR is a macro which the compiler will identify.

Paolo Di Prodi
Super Sayan
Robomotic LTD
Company Code: SC389778

André Hänsel

Dec 24, 2010, 5:37:50 AM12/24/10
to ti-lau...@googlegroups.com
It's the "address" that tells the compiler where to access the register and
you can find the addresses in the Family User's Guide. Look again at your
include file, it's probably some macro where the address is put in. For
example, in the MSP430x2xx family, the adress for P1DIR is 0x22.

> -----Ursprüngliche Nachricht-----
> Von: ti-lau...@googlegroups.com [mailto:ti-lau...@googlegroups.com]
> Im Auftrag von Sk3ptic
> Gesendet: Freitag, 24. Dezember 2010 05:16
> An: TI Launchpad
> Betreff: Understanding P1DIR |= 0x01


Dec 25, 2010, 12:55:26 AM12/25/10
to TI Launchpad
To expand further for those listening in:

The address is a byte in memory where each bit in that byte sets the
direction for a different pin of port 1. You set these bits in your
code though they may have a default power on value.

The locations of these bytes are usually different for different chips
or part numbers so a header file is made for convenience and each full
of these macros so you don't need to know the actual memory location
for every chip. This is especially useful when using code written for
one chip on another chip, you only change the header file called and
not a ton of numbers in the code. There is a P2DIR also but only two
bits are valid for 14-pin launchpad chips.

The spec sheet for the specific chip should define which registers are
available in each chip and where they are as André already did. I
also love PDFs because I can search for something like p1dir or pxdir
and find about everywhere it is relevant.


Dec 28, 2010, 9:33:08 PM12/28/10
to TI Launchpad
Thanks for the replies guys but I still don't see where the mapping
from P1DIR to 22h is happening.

I know that the IO is memory mapped and the register P1DIR is at
location 22h. There is no where in the include file where P1DIR is
defined to 022h, the include file has the following:

extern volatile unsigned char address P1DIR // and that is it

it defines it as an extern variable.

It must define it in the linker script which starts as following

SFR : origin = 0x0000, length = 0x0010
PERIPHERALS_8BIT : origin = 0x0010, length = 0x00F0
PERIPHERALS_16BIT : origin = 0x0100, length = 0x0100
RAM : origin = 0x0200, length = 0x0080


From that I guess it must some how know that P1DIR is at 22h for this
particular chip.


Dec 29, 2010, 1:33:18 AM12/29/10
to TI Launchpad
I do not know which file is "the" include file you are refereeing to.
There are hundreds of include files, each with hundreds of lines.

I got one called <msp430g2211.h>. Line number 331 says:
#define P1DIR_ (0x0022u) /* port 1 direction */



Jan 3, 2011, 5:41:44 PM1/3/11
to TI Launchpad
I'm using the include file that came with Code Composer Studio 4.0,
which are you using? What complier and what GUI are you using?

On Dec 28 2010, 10:33 pm, Old_Cow_Yellow

Paolo Di Prodi

Jan 3, 2011, 5:46:47 PM1/3/11
to ti-lau...@googlegroups.com
You should have told us before you was using the CCS.
That include file is in the mspgcc libraries.
you will probably find something similar for the CCS which I used few times.

On 3 January 2011 22:41, Sk3ptic <sk3...@gmail.com> wrote:
I'm using the include file that came with Code Composer Studio 4.0,
which are you using? What complier and what GUI are you using?

On Dec 28 2010, 10:33 pm, Old_Cow_Yellow
<lichen.wang.acti...@gmail.com> wrote:
> I do not know which file is "the" include file you are refereeing to.
> There are hundreds of include files, each with hundreds of lines.
> I got one called <msp430g2211.h>. Line number 331 says:
> #define P1DIR_  (0x0022u)  /* port 1 direction */
> --OCY


Jan 4, 2011, 11:16:35 AM1/4/11
to TI Launchpad
CCS also has hundreds (if not thousands) of include files. You looked
at the wrong one for where P1DIR is mapped to.

André Hänsel

Jan 4, 2011, 11:41:46 AM1/4/11
to ti-lau...@googlegroups.com
Can't you (Ctrl+) right-click on the symbol and jump to its definition?

> -----Ursprüngliche Nachricht-----
> Von: ti-lau...@googlegroups.com [mailto:ti-lau...@googlegroups.com]

> Im Auftrag von Old_Cow_Yellow
> Gesendet: Dienstag, 4. Januar 2011 17:17
> An: TI Launchpad
> Betreff: Re: Understanding P1DIR |= 0x01


Jan 6, 2011, 3:20:14 PM1/6/11
to TI Launchpad
OCY makes sense. When I set up a project in CCS I have to tell it a
chip or group of chips to compile for so who knows what all includes
come with that.

Kenneth Finnegan

Jan 10, 2011, 11:23:39 PM1/10/11
to ti-launchpad, sk3ptic
To actually answer your question, in CCS, the include folder contains both a header (x.h) and a linker script (x.cmd) for each core.  Citing a random linker script:
C:\Program Files (x86)\Texas Instruments\ccsv4\msp430\include\msp430g2231.cmd
Line 52: P1DIR  =  0x0022;

Along with addresses for every other register on the chip.

Kenneth Finnegan

Mallappa T

Oct 7, 2013, 3:44:23 AM10/7/13
to ti-lau...@googlegroups.com
Dear Sir,

             I want some help from you, I have few questions about MSP430 I/O pins.

1.what is the meaning of P1DIR = 0x0001 ; (in program i write like BIT1, BIT2)

2. How the BIT1 is equal to 0x0001 is it hexadecimal value or what if hexadecimal value and how come you know the 0X0001 is BIT1.

please help me for this.

Thank you.

Utku Norman

Oct 9, 2013, 5:04:07 PM10/9/13
to ti-lau...@googlegroups.com

BIT1 to BITF are defined in the header files to be constants.
For instance, in msp4g2553.h, the following definitions hold:


#define BIT0                (0x0001u)
#define BIT1                (0x0002u)
#define BIT2                (0x0004u)
#define BIT3                (0x0008u)
#define BIT4                (0x0010u)
#define BIT5                (0x0020u)
#define BIT6                (0x0040u)
#define BIT7                (0x0080u)
#define BIT8                (0x0100u)
#define BIT9                (0x0200u)
#define BITA                (0x0400u)
#define BITB                (0x0800u)
#define BITC                (0x1000u)
As the definition suggests, BIT0, 0x0001u, 0x001, 0b00000001 are equivalent.
While coding, you can use whichever you like.

P1DIR is the register where whether a pin of port 1 is an output or an input is set.
For example, in MSPG2553, P1DIR is an 8-bit register.
The zeroth bit is associated with the zeroth pin, first bit with first pin and so on.
Setting a bit of P1DIR register to 1 means the associated pin will work as an output.
Setting to 0 will lead to an input pin.

So, P1DIR = 0x0001;  is the same as P1DIR = 0x01; and P1DIR = BIT0;

They all mean that you set the bit-0 to 1 , that pin-0 is output.
Set all other bits to 0, that all other pins are input.

Remark: You should also check the datasheet of the specific model of MSP430. My explanation holds for P1SEL bits set to 0. Other configurations for built in peripherals may be used.

Good luck,


2013/10/7 Mallappa T <mallappa...@gmail.com>

You received this message because you are subscribed to the Google Groups "TI Launchpad" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-launchpad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

mark hunsberger

Oct 11, 2013, 8:36:07 AM10/11/13
to ti-lau...@googlegroups.com

Just in case you are still looking for an explanation, the "P1DIR |= 0X22u is just saying make bit 1 and bit 5 high ('1').  0x22u is just hexadecimal for 0%00100010b


mark hunsberger

Oct 11, 2013, 8:39:09 AM10/11/13
to ti-lau...@googlegroups.com

Oh.... My bad.... The #define statement sets P1dir_  like a pointer to the memory location of P1DIR

Reply all
Reply to author
0 new messages