You're excited. Your palms are sweaty and your mind is racing with ideas of 8x8x8 RGB LED cubes, of LCD displays saying "Hello World", of home made robots which chase the cats and fire ping pong balls at them. The day has come, there's no stopping you now - your TI MSP430 Lauchpad has arrived in the post and you're ready to take over the world. What next?
You may be disappointed to learn that it's not as simple to use as the Arduino (I was, and I don't even have an Arduino). The support is there, the information is there, but it just doesn't seem as accessable as the Arduino. So the dreams are crushed, plans for RoboCat are shelved, and the long journey towards microcontroller proficiency begins. Read on for the first step - flashing the chip.
There are a few options available for working with the Launchpad, but I'll just be discussing Code Composer Studio (CCS), available from the TI website (or on the CD supplied with your Launchpad). It does have some limitations, but they're not going to get in the way so until they pose a problem, I'm happy to ignore them. If you haven't already, install CCS now.
With CCS installed, we can now get on with creating our program. Use the keyboard shortcut "alt+shift+N" to bring up the new project menu. Click on CCS Project, enter a project name (HelloWorld should do for now), click next. Now ensure Project Type is set to MSP430 as in the screen below, and click next.
The next page asks if you want to set any project dependencies. Just make sure all boxes are unchecked, and click next. The following screen then allows you to choose your microcontroller.
I'm using the MSP430G2211 that came bundled with the Launchpad, so I've selected that, and I've put a big red oval around it above. Other than that, I'm leaving all of the other settings as default and clicking on finish.
Hopefully now you should have something like the screenshot below staring back at you. If the pane at the left hand side of the window is missing, a quick click on "View, C/C++ Projects" should sort things out. From here you need to right click on your project in the left hand pane, and select "New, Source File". A pop up window asks you to name your file. Being the ever adventurous sort, I'm going to call it "main.c".
So now everything is set up, we just need some code to put on the chip. I'm a fan of blinking LEDs, so that's what I'm going to make my Launchpad do - alternately flash the red and green LEDs. The code is below.
#include <msp430g2211.h> //This is the header file for your chip.
void main(void) {
WDTCTL = WDTPW + WDTHOLD; //Hold the WDT (WatchDog Timer)
P1DIR |= (BIT0 + BIT6); //Set P1.0 and P1.6 as outputs
unsigned int i=0; //Create a variable int called i, set it to 0
while(1){ //Loop forever
i++; //Add one to 'i' ever time the loop starts
if (i<10000){
P1OUT |= BIT0; //Set P1.0 high
P1OUT &= ~BIT6; //Set P1.6 low
}
if (i>10000){
P1OUT |= BIT6; //Set P1.6 high
P1OUT &= ~BIT0; //Set P1.0 low
}
if (i==20000){ //When 10000 cycles have completed, reset 'i' back to 0.
i=0;
}
}
}
Now we're onto the final step, uploading our code to the chip. Click on the little 'bug' icon towards the top left of the screen (circled in the image below).
If all is as it should be, CCS will now compile the code, send it to the chip on the launchpad and put CCS in 'debug' mode. The final step is to click to green icon which looks like a play button (circled in the image below), to set your code running on the MCU. So go ahead, click it!
That's it! Code has been written to the Launchpad, and is now merrily running, a testament to your god-like status as a programmer. Bask in the glory, but don't let the fame go to your head, there's still a long way to go before RoboCat becomes a reality.
Thanks for taking a look at my first ever blog post, and please feel free to comment and correct me where necessary. In the next instalment, we'll move away from the launchpad and put our chip in a breadboard powered by two AA cells, giving us the freedom to carry our project with us wherever we go. What larks we'll have Pip!





No comments:
Post a Comment