This tutorial has to be seen as the most easy code that is possible to do with a MSP430 LaunchPad and then totally adapted for beginners.
Indeed, we are going to blink together the red and the green LEDs by explaining what we are doing with the famous P1DIR and P1OUT registers.
Ready?
So, let's do it!
In the MSP430 Launchpad there are a few microcontrollers possible to use.
For example, if you bought the LaunchPad recently, it could be a MSP430G2553, or older a MSP430G2221.
In this turorial we will use the MSP430G2553.
Anyway, the code below works for every microcontroller.
So, the first thing to know is that to blink a LED, we have to tell a special registry what we would like to do.
In our case, we would like to blink the red (pin 0) and the green (pin 6) LEDs.
You can check it directly on your board, at the bottom left, there are our two LEDs with P1.0 and P1.6 written respectively above the red and the green jumpers of the LEDs.
OK, now that we have understood which LED is on pin 0 and 6, we are going to see which registry will help us to blink those LEDs.
This registry is P1DIR and it's a word of 8 bits.
It means that each bit of this word can be set to 0 or 1 and each bit is linked to a pin.
So:
It's now easy to talk with the P1DIR and tell it which of pin we would like to set as ouputs.
We use ouputs because a LED as to be set like this to blink.
We have to use port 1 for our LEDs, so it will be the P1 output.
As our LEDs are on pin 0 and pin 6 we can say that we will be using the P1.0 and P1.6 pins.
And so, in our word of 8 bits, we have to set the bits we want to set as output (hence 1).
We have then to set P1DIR like this:
P1.0 = first.
p1.6 = seventh.
We can see that the first bit (on the right) is set to 1 because we have to set the pin 0.
And then the sixth bit (from the right) is actually the seventh on the list.
At this point, the registry knows that pin 0 and pin 6 will be set at 1 and are expected to send data from the microcontroller to those pins.
The P1DIR registry knows which pins will be used as output.
But it doesn't know when to send data to them.
We have then to use the P1OUT regitry to do that.
Indeed, this registry is specially designed to tell the microcontroller when send data to a pin.
Those pins are the same as those used previously with the P1DIR registry, that is P0 and P6.
It's time to see the code.
Notice that BIT0 and BIT6 are defined respectively to hexadecimal numbers in the msp430g2553.h file:
In binary:
So BIT0 + BIT6 is equal to:
00000001
| 01000000
----------------
= 01000001
#include <msp430g2553.h> // change it with your microcontroller void main() { WDTCTL = WDTPW | WDTHOLD; // don't touch it, it's for the watchdog stuff P1DIR |= (BIT0 | BIT6); // set P1DIR with P0 and P6 to high (1) for (;;) { P1OUT ^= (BIT0 | BIT6); // toggle the P0 and P6 of P1OUT with 1 and 0 __delay_cycles(250000); // 250000 microseconds between each cycle } }
In this tutorial you created your first example with the MSP430 LaunchPad and see how to use the P1DIR and P1OUT registers.
You are now able to blink the green and the red LEDs like a professional.
Well done, you made it.
Comments
Otto Hunt (not verified)
Thursday, August 7, 2014 - 12:41am
Permalink
Why "|=" and not just "=" ?
Why "|=" and not just "=" ?
Mi-K
Thursday, August 7, 2014 - 9:37pm
Permalink
Hello Otto Hunt,
Hello Otto Hunt,
I used "|=" instead of "=" because it's an usual way to program electronic microcontroller.
When you use "|=" it means that you are setting registers.
When you use "&=" it means that you are resetting registers.
I use "=" only when I program classic software in Java or PHP for example.
But in the tutorial's code, using "=" instead of "|=" is, indeed, the same.
Ambesaw. (not verified)
Monday, November 24, 2014 - 9:30am
Permalink
The primary reason to use |=
The primary reason to use |= is that it helps to make sure other bits of the registers are not affected by the assignment statement. Bitwise ORing enables not to change bits other than the target bit.
Mi-K
Monday, November 24, 2014 - 10:58pm
Permalink
Hello Ambesaw,
Hello Ambesaw,
You're absolutely right.
yash (not verified)
Wednesday, June 3, 2015 - 2:07pm
Permalink
you can also do like this
you can also do like this
P1DIR = BIT0 + BIT6;
while(1)
{ P1OUT ^= (BIT0 | BIT6); } // toggle the P0 and P6 of P1OUT with 1 and 0
__delay_cycles(250000);
Pat (not verified)
Tuesday, October 27, 2015 - 5:54am
Permalink
Okay, so I get why you do
Okay, so I get why you do this:
P1OUT ^= (BIT0 | BIT6);
It will keep toggling Pin0 and Pin6 with 1 and 0 in order to make the LED's blink.
But why do we have 2 Bitwise Or's?
P1DIR |= (BIT0 | BIT6);
I understand that Bit0 | Bit6 sets P1DIR to 01000001.
But what does the |= do? Does it do the actual assigning of P1DIR?
Thanks in advance.
Mi-K
Wednesday, October 28, 2015 - 9:19pm
Permalink
Hello Pat,
Hello Pat,
Yes "P1DIR |= (BIT0 | BIT6);" sets the P1DIR to 01000001.
Setting P1DIR allows electricity to circulate through BIT 0 and BIT 6.
It's like in a house, you can switch on lights ONLY if there is electricity in your electric house circuit.
In our case, we say that we want electric current ONLY inside BIT ZERO and BIT SIX.
And after we decide with P1OUT to switch ON and OFF the BIT 0 and BIT 6.
For the "|=", it means that we change the value of BIT 0 and BIT 6 to "1".
Because:
So with this bitwise OR we set the corresponding bit to 1 whatever was the value before the assignment.
Anonymous (not verified)
Sunday, November 1, 2015 - 3:43am
Permalink
Hi Mi-K. Thank you very much
Hi Mi-K. Thank you very much for your answers; they helped a lot :) I do have a few more questions, though.
-Is P1OUT originally 00000000 ?
-And instead of doing P1OUT ^= (BIT0 | BIT6);
can we do P1OUT ~= (BIT0 | BIT6);
Will the complement inside a for loop work by continuously toggling Pin0 and Pin6 with 1 and 0 in order to make the LED's blink?
Mi-K
Sunday, November 1, 2015 - 2:58pm
Permalink
Hello,
Hello,
1) We can never know if P1OUT is set to 0x00 or not.
So to be totally sure of that, best practice is to reset it before doing anything with it.
Same for P1DIR, I recommend to set it to 0x00 before anything else.
For example:
2) The following line isn’t correct:
Because the "~=" operator doesn’t exist in C language.
3) The loop I made in the tutorial is infinite, so yes inside this "for loop" Pin0 and Pin6 continuously toggling.
SARATHK (not verified)
Saturday, November 5, 2016 - 6:22am
Permalink
How can we make both LEDs
How can we make both LEDs blink alternatively? I mean "red on when green off" and red off green on" so on? Thanks
Anonymous (not verified)
Tuesday, March 20, 2018 - 7:30pm
Permalink
for (;;) {
for (;;) {
// toggle bit 0 of P1
P1OUT ^= 0x01;
for (i = 0; i < 0x4000; i++);
P1OUT ^=0x40;
// delay for a while
// __delay_cycles(250000);
for (i = 0; i < 0x9000; i++);
Hello (not verified)
Wednesday, April 4, 2018 - 9:23am
Permalink
I put 00000001 instead of
I put 00000001 instead of bit1 but don't work
Can i ask what is the problem?
Mi-K
Wednesday, April 4, 2018 - 8:11pm
Permalink
You can't use binary code
You can't use binary code directly in your C language code, use either equivalent in hexadecimal or the macros like BIT1, BIT2, etc.
TheCollector (not verified)
Tuesday, August 6, 2019 - 6:13pm
Permalink
How do you program the LED to
How do you program the LED to go off when the button is pressed and stay on until the button is pressed again?
Reddy@007 (not verified)
Thursday, June 30, 2022 - 7:18pm
Permalink
What code for only blink the
What code for only blink the green led in launchpad
Add new comment