43oh

# Setting pins??!

## Recommended Posts

Since I've gotten my launchpad I have really wanted to start developing stuff with it. However I have hit a problem: no matter what tutorials I have looked at I cannot figure out how to set the pins. For the blinking LED it lists it as:

`P1DIR |= 0x01;`

What exactly is 0x01 and how do I figure out what to input for different pins?

Thanks alot!

##### Share on other sites

0x01 is the hexadecimal representation of ..uh.. the number 1.

The line logical ORs the directional register of port 1 with this bitmask. The LSB, Bit 0, is set to 1 the rest is set to zero. This configures Port 1.0 as output and the other bits are unaffected.

```X | 0 = X
X | 1 = 1

so if P1DIR was
7 6 5 4 3 2 1 0 Bit #
1 1 0 0 1 1 0 0 Bit value before P1DIR |= 0x01
0 0 0 0 0 0 0 1 0x01 bitmask
1 1 0 0 1 1 0 1 Bit value after P1DIR |= 0x01```

Work through this http://www.43oh.com/2010/08/10-beginner ... -counting/ and you become an expert.

##### Share on other sites

So basically if I want to use the pins that begin with 1 I would use P1DIR, and if I want to use the pins with 2 I use P2DIR. For the decimals I use 0x01, 0x02, 0x03, and so on? So if i wanted to use pin 2.4 I would use this?

`P2DIR = 0x04;`

##### Share on other sites

You are correct on the P1DIR and P2DIR.. however your value to use in the register is incorrect.

Basically the register is a bit mask for ALL the pins on the port.

These following lines all do the same and set P2.4 to be output...

```P2DIR = 0x10;
P2DIR = 16;
P2DIR = BIT4;
```

P2.4 is the 5th pin along (numbering starts at 0).. The mask in binary is therefore 00010000. In decimal this is 16 which is 10 in hexadecimal and BIT4 is defined in the device header file as one of these values. Personally I find the BIT4 varient easier to understand.

The mask being built up this way makes more sense when you need more than one pin for output. For example if pins P2.0 and P2.4 were needed for output.. the mask in binary would be.... 00010001. In decimal this is 17 and in hex is 0x11. So setting P2.0 and P2.4 for output could be done with any one of these following lines..

```P2DIR = 0x11;
P2DIR = 17;
P2DIR = BIT0 + BIT4;
```

From looking at this you can see that using the BIT0 and BIT4 variant is much easier to understand.

Hope this helps...

Dan

##### Share on other sites

That makes a huge amount of sense now. Thank you!!

##### Share on other sites

So I tried to change the out pin in the LED blinking program to 1.6, but when I run it the LED doesn't flash. It only works if I set it to 1.0.

```/*
* main.c
*/
#include
void main(void) {
int i;

WDTCTL = WDTPW + WDTHOLD;
P1DIR = BIT6;
P1OUT = 0x00;

for (; {
P1OUT ^= 0x01;

for (i=0; i<0xFFFF; i++) {
} // delay
} // infinite loop

}
```

##### Share on other sites

Try this:

```/*
* main.c
*/
#include
void main(void) {
int i;

WDTCTL = WDTPW + WDTHOLD;
P1DIR = BIT6;
P1OUT = 0x00;

for (; {
P1OUT ^= BIT6;

for (i=0; i<0xFFFF; i++) {
} // delay
} // infinite loop

}
```

##### Share on other sites

That fixed it. Thanks!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
• Blog

• #### Activity

×
• Create New...