# Energia - program a TI MSP430 using Arduino sketches

November 5, 20131 comment

## TI MSP430 Launchpad

I started tinkering with microcontroller a couple of years ago with an Arduino Uno.  I had a little experience with C, so programming in the Arduino environment has been relatively easy and straightforward for me.  My code is not necessarily elegant or efficient, but I can usually figure out how to make an Arduino do what I want it to do eventually.  A lot of credit to the Arduino userbase, as it is very easy to figure most things out with a quick Google search when you get stuck.

Not long after I started tinkering with the Arduino, I also ordered a couple of TI MSP-430 Launchpads.  At the time, this development board was only $4.30. For that price, you get a Launchpad board, two MSP430 DIP MCU's, a USB cable and a couple of Launchpad stickers. Oh, shipping is also included for that price. I paid$30 for my first Arduino, so this seemed like a steal.

It seemed great until I realized it wasn't nearly as easy to program as the Arduino.  The hardware was cheap, but Code Composer Studio seemed complex compared to the Arduino IDE.  Also, the code for the MSP430 seemed very cryptic compared to an Arduino sketch.  Let's compare:

### "Official" TI code to make an LED blink on MSP430

#include

unsigned int i = 0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;

P1DIR |= 0x01;

for (;;)
{
P1OUT ^= 0x01;
for(i=0; i< 20000; i++);
}
}

### Here's the Arduino version:

int led = 13;

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}


For the person that has taken an introductory programming class in high school or college, the Arduino code is immediately understandable.  The novice can take a look at the code and tell what each part does, even without commenting.  The MSP430 code is not as clear.

The best case scenario would be to just learn how to program the MSP430.  There are several good books and tutorials on the subject available on the web.  For the hobbiest, it's typically easier just to stick with the Arduino.

If it is more difficult to program the MSP430, then why would a hobbiest even consider trying to use it?  The main reason is cost.  Currently priced at $9.99, the Launchpad is still a fantastic deal. Not only does it come with a dev board, but it also comes with 2 microcontrollers. The last Launchpad I bought came with an MSP430G2553 and an MSP430G2231. Although the MSP430 chips have limited Flash and RAM versus the ATMEGA 328 in the Arduino, the 2553 is more than enough for most simple projects. You can buy additional MSP430 microcontrollers for a buck or two, pop them in your Launchpad, upload your code, and insert them in their destination circuit. Alternatively, you can even use the Launchpad as an ISP to program or edit the code on an MSP430 that is already in-circuit. Although these same kinds of options are available with the Arduino, the Arduino boards typically use SMD microcontrollers and if you buy an Atmel DIP micro, you will have to install a bootloader first. Programming the MSP-430 is much simpler, as I'll demonstrate shortly. One thing to keep in mind with the MSP430 - it is a 3.3 volt device. If you interface with a device that is running at 5V TTL, you will need to use level converters or resistors to avoid damaging your microcontroller. Don't abuse it, but Ti also makes a modest number of their microcontrollers available through their sample program. It's been awhile since I got samples, but if I remember correctly you can order up to 10 microcontrollers on one sample order. Don't abuse it. :) ## Energia Energia is a fork of the Arduino IDE created by a few members of the MSP430 community at 43oh.com. Using Energia, you can program the MSP-430 using Arduino sketches! All of the basic commands work fine, and most common libaries have been ported to work with Energia. There are a few times I have had trouble finding libraries for devices, but more are being ported all the time. The list of supported TI Launchpads has grown. Energia currently supports the MSP 430g 2231,2452, and 2553. Energia also supports the Launchpad MSP430F series, which give the user a maximum of 128K of program space and 8K of RAM. Finally, there is support for the Stellaris Launchpad, which features an 80 MHz ARM Cortex-M4F chip. The Stellaris Launchpad has since been replaced by the Tiva-C Launchpad. The performance/$ of these TI dev boards vs. Arduino Uno, Mega and Due is really no contest.

Energia can be downloaded for free at Energia.nu - versions are available for Windows, OSX and Linux.  There is excellent support for Energia at the 43oh Forum.  On Windows, installation is exactly the same as the Arduino IDE.  Download the zip file, then drag and drop to the destination folder.

Also available at Energia.nu are these cool pinout diagrams.  When addressing the i/o pins of your MSP430 micro, simply use the pin number on the board.  The diagram gives the input/output capability of each i/o pin.

## Hello Pumpkin

This past Halloween, I built a simple LED-blinky jack-o-lantern using an MSP-430 Launchpad and a few LED's.  This project is basically a Hello World type of project.  You can reference the video above to see how easy it was to build and code the project using Energia.  Below is the sketch used to program the MSP430 Launchpad.

void setup()
{
// put your setup code here, to run once:
for(int i=2;i<8;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
}

void loop()
{
// put your main code here, to run repeatedly:
allOn();
delay(4000);
allOff();
delay(1000);
eyesOn();
delay(1000);
noseOn();
delay(1000);
mouthOn();
delay(1000);
allOn();
delay(1000);
for(int i=0;i<2;i++)
{
eyesFlicker();
}
}

void allOn()
{
for(int i=2;i<8;i++){
digitalWrite(i,HIGH);
}
}

void allOff()
{
for(int i=2;i<8;i++){
digitalWrite(i,LOW);
}
}

void eyesOn()
{
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}

void eyesOff()
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
}

void noseOn()
{
digitalWrite(4,HIGH);
}

void noseOff()
{
digitalWrite(4,LOW);
}

void mouthOn()
{
for(int i=5;i<8;i++)
{
digitalWrite(i,HIGH);
}
}

void mouthOff()
{
for(int i=5;i<8;i++)
{
digitalWrite(i,LOW);
}
}

void eyesFlicker()
{
for(int i=1;i<70;i++)
{
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
delay(i);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
delay(i);
}
for(int i=70;i>0;i--)
{
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
delay(i);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
delay(i);
}
}


Next time:  We'll build a little more complex project using the Launchpad MSP-430, and I'll show how easy it is to move the micro to a breadboard and also program multiple MSP-430's using the Launchpad as an ISP.

[ - ]
Comment by January 19, 2015
Free energy monitor for PV panels and wind turbine - live data: http://free-energy-monitor.com/index.php/energy/live_data?tabela=data_log

To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.

Registering will allow you to participate to the forums on ALL the related sites and give you access to all pdf downloads.