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!
Explanation
Good to know
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.
This P1DIR registry
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.
- Low or 0 meaning that the pin is set as an input;
- High or 1 meaning that the pin is set as an ouput.
So:
- 0 (low) = input
- 1 (high) = output
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:
- 01 000001
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 LEDs
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:
- #define BIT0 (0x0001)
- #define BIT6 (0x0040)
In binary:
- BIT0 = 00000001
- BIT6 = 01 000000
So BIT0 + BIT6 is equal to:
0000000**1**
| 01 000000
= 01 000001
Code
#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
}
}
Conclusion
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.