Category Archives: msp430

“volatile” can cost you!

In an earlier post, I mentioned how I am going to get back into embedded progarmmming using the fantastic launchpad from TI.

Well, I got started with a book and various resources online, using the open source msp430 gcc chain (since i don’t have access to a win machine for IAR tools, the only negative thing for the launchpad in my opinion) with my hello world program (blinking leds).

Though there are plenty of blinking led programs using the Timers on the lanchpad, I really wanted to write a simple program without any timers. Below is the sample code I started with:


#include <io.h>
#include <signal.h>

 /* Program toggles Pin 1.6 in MSP430 */

 void delay()
 {
    int i;
   for(i=0;i < 32000;i++);
}

void main(void)
 {
   // Stop the watchdog timer
   WDTCTL = WDTPW | WDTHOLD;
  // Set the direction of PIN1.6
   P1DIR = P1DIR | BIT6;
   P1OUT |= BIT6;
   // Toggle Pin 1.6
   for(;;) //forever
     {
       P1OUT ^=BIT6;
       delay();
     }
 }

The code seems simple enough! The delay function should provide for sufficient delay to see the blinking of the LED. However on running this code, the LED never blinks!

As a beginner in embedded programming, it took me an hour of debugging and searching to come up with this article . The key point is:

An automatic object declared in a function that calls setjmp and whose value is-changed between the call to setjmp and a corresponding call to longjmp

Changing my delay function to use volatile fixes the issue!


void delay()
 {
    volatile int i;
   for(i=0; i< 32000;i++);
}

You can compare the objdump between the two, using:

You can check out the objdmp using:

msp430-gcc -S -mmcu=msp430x2012 main.c
msp430-as main.s -mmcu=msp430x2012
msp430-objdump -D a.out

MSP430 launchpad!

TI launched  this crazy package of a microcontroller with all the bells and whistles that an arduino has for just $4.30. This includes a USB cable, the assembled board as well as the free IDE tools to get you started. There are a couple of catches though:

a) The free ide tools are only available for windows

b) There is no easy language for the board as there is one for arduino. (There is a graphical tool Grace available for win)

However, if you are willing to delve in to the embedded world and don’t mind getting dirty with C, this is an amazing deal.

Though there are no official tools available for other platforms, there are many wikis out there that outline the creation of the toolchain for linux and OSX.

I have personally managed to get this up and running in Linux Ubuntu (11.04) using the hackaday wiki and I recommend this wiki for OSX. Though, I have had some issues setting this up with rf2500 and permission. It seems that the solution is documented here,  I just haven’t had the time to set it up.

The basic code that I have uploaded works great!

Since, I am completely out of touch with core C Assembly programming, this micro controller provides a great opportunity to learn this architecture at a meagre cost of just $4.50.