# Beginner with a few questions

Started by March 15, 2013
Hey everybody,
About two weeks ago I started making a serious effort to learn to use the MSP430 Launchpad (with MSP430F2012). I have no prior experience coding in C or C++, so I wanted to describe what I've learned, and get some feedback, just to make sure I understand everything correctly.

In the "blink your first LED" tutorial there is the following piece of code:
P1DIR |= 0x01;
It explains that this sets pin 1.0 in port 1 as an output.
If I understand this correctly, each "port" is a set of eight pins. The direction of each pin is stored in P1DIR as an eight bit number, here represented in hex as 0x01 or 00000001 in binary for pin 1.0. After doing some research I was able to put together a table:
Pin 1.0 Bit 0 0x01 00000001
Pin 1.1 Bit 1 0x02 00000010
Pin 1.2 Bit 2 0x04 00000100
Pin 1.3 Bit 3 0x08 00001000
Pin 1.4 Bit 4 0x10 00010000
Pin 1.5 Bit 5 0x20 00100000
Pin 1.6 Bit 6 0x40 01000000
Pin 1.7 Bit 7 0x80 10000000
After some experimenting, I found that to turn on multiple pins, I just have to add the hex values together:
Pin 1.0 and 1.1 would be 0x01 + 0x02 = 0x03 or 00000011
Pin 1.0 and 1.4 would be 0x01 + 0x10 = 0x11 or 00010001
Because I can't carry the ten, if the sum is greater than 10, I have to use letters A through F (0x04 + 0x08 = 0x0C). If I wanted to set all the pins (of port 1) as outputs, it would be 0xFF or 11111111.
How am I doing so far?

I checked msp430f2012.h (version 1.5), and it says:
#define P1DIR_ (0x0022u) /* Port 1 Direction */
DEFC( P1DIR , P1DIR_)
#define P2DIR_ (0x002Au) /* Port 2 Direction */
DEFC( P2DIR , P2DIR_)
This means that pin 1.1 and 1.5 of port 1 and pins 1.1, 1.3, and 1.5 of port 2 are set as outputs when the MSP430 first powers up, right? If yes, why are they set as outputs?
The MSP430F2012 only has 14 pins and doesn't extend onto the port 2 section of the launchpad, does it even have a port 2?
VCC, GND, RST, TEST, XOUT, and XIN are not part of any port, correct?

So if I put all of this together, the line:
P1DIR |= 0x01;
can be rewritten as:
00100010 OR 00000001;
and it is doing the following:
00100010 OR
00000001 00100011
and then it stores the result (00100011) as P1DIR, right?

Thank you in advance to anyone who reads through all of that!
Andrew

Good start pal.

All PORT1 pins are not set as outputs at reset. What you're seeing is the address of the P1DIR register. Each register has an address in the MCU. To understand the default state of any register you need to reference the family guide. You will see something like rw-0 for each bit in each register which would mean in this instance the bit is readable, writeable and is 0 at power up.

VCC and GND are obviously power pins. RESET and TEST are used to program the device (they have other functions but don't worry about that for now) and XIN AND XOUT are used for the crystal by default. If I'm not mistaken they can also be configured to be regular IO and are defined at P2.6 and P2.7.

Hope that helps

Hani
Sent from my iPhone

On 16/03/2013, at 9:29 AM, "Andrew" wrote:

> Hey everybody,
> About two weeks ago I started making a serious effort to learn to use the MSP430 Launchpad (with MSP430F2012). I have no prior experience coding in C or C++, so I wanted to describe what I've learned, and get some feedback, just to make sure I understand everything correctly.
>
> In the "blink your first LED" tutorial there is the following piece of code:
> P1DIR |= 0x01;
> It explains that this sets pin 1.0 in port 1 as an output.
> If I understand this correctly, each "port" is a set of eight pins. The direction of each pin is stored in P1DIR as an eight bit number, here represented in hex as 0x01 or 00000001 in binary for pin 1.0. After doing some research I was able to put together a table:
> Pin 1.0 Bit 0 0x01 00000001
> Pin 1.1 Bit 1 0x02 00000010
> Pin 1.2 Bit 2 0x04 00000100
> Pin 1.3 Bit 3 0x08 00001000
> Pin 1.4 Bit 4 0x10 00010000
> Pin 1.5 Bit 5 0x20 00100000
> Pin 1.6 Bit 6 0x40 01000000
> Pin 1.7 Bit 7 0x80 10000000
> After some experimenting, I found that to turn on multiple pins, I just have to add the hex values together:
> Pin 1.0 and 1.1 would be 0x01 + 0x02 = 0x03 or 00000011
> Pin 1.0 and 1.4 would be 0x01 + 0x10 = 0x11 or 00010001
> Because I can't carry the ten, if the sum is greater than 10, I have to use letters A through F (0x04 + 0x08 = 0x0C). If I wanted to set all the pins (of port 1) as outputs, it would be 0xFF or 11111111.
> How am I doing so far?
>
> I checked msp430f2012.h (version 1.5), and it says:
> #define P1DIR_ (0x0022u) /* Port 1 Direction */
> DEFC( P1DIR , P1DIR_)
> #define P2DIR_ (0x002Au) /* Port 2 Direction */
> DEFC( P2DIR , P2DIR_)
> This means that pin 1.1 and 1.5 of port 1 and pins 1.1, 1.3, and 1.5 of port 2 are set as outputs when the MSP430 first powers up, right? If yes, why are they set as outputs?
> The MSP430F2012 only has 14 pins and doesn't extend onto the port 2 section of the launchpad, does it even have a port 2?
> VCC, GND, RST, TEST, XOUT, and XIN are not part of any port, correct?
>
> So if I put all of this together, the line:
> P1DIR |= 0x01;
> can be rewritten as:
> 00100010 OR 00000001;
> and it is doing the following:
> 00100010 OR
> 00000001 > 00100011
> and then it stores the result (00100011) as P1DIR, right?
>
> Thank you in advance to anyone who reads through all of that!
> Andrew

Andrew wrote:
> So if I put all of this together, the line:
> P1DIR |= 0x01;
> can be rewritten as:
> 00100010 OR 00000001;
> and it is doing the following:
> 00100010 OR
> 00000001 > 00100011
> and then it stores the result (00100011) as P1DIR, right?
Hi Andrew
No. P1DIR is an address, not a value. The memory contents of P1DIR are
ord. As if:
*P1DIR|= value;
P1DIR has been macroed to act as a a referenced address, not a value.
Look at the memory map for the device, you will find P1DIR is a register
at address 0x0022. what is in that register is what gets ord.

~~~~~
There are some basic defines in the headers too. so:

P1DIR|= BIT0; becomes more readable. Go further and:
#define LED1 BIT0
and
P1DIR|= LED1;

Now if you move the led to another pin you won't have to hunt through
your code for all the changes.

*Best, Dan.
*