Programming

Introducing the MSP430 Development Software

We will use three pieces of software during this workshop:

  • Notepad++ is a plain text editor, which we will be using to type C code into a file.
  • MSPGCC is a C compiler for the Texas Intruments MSP430 family of microcontrollers.
  • MSP430Flasher is another piece of software which we will use to transfer a compiled program from the PC to the MSP430 microcontroller where it will be stored in flash memory.

All of this software is completely free, so if you want to continue experimenting with your robot after the workshop you can copy them onto your own PC. If you want to copy them from the PC you’re working on during the workshop, you’ll need a USB key with about 140 MB of space free.

Download the RoboSlam folder

Download the file RoboSlam.zip and extract it somewhere convenient (e.g. on your desktop). This folder contains Notepad++, the MSPGCC C compiler, MSP430Flasher and the build scripts you will need. It’s also where you will store your own programs.

Now you’re ready to begin writing programs for your robot.

Example 1: Blink an LED on and off

This example program is called “Blink” because it blinks the green LED which is connected to pin P2.0 on the MSP430. Begin by creating a new folder called “blink” inside the RoboSlam folder you extracted from the zip file in the previous step. For example, on my PC the folder is “C:\Users\Ted\Desktop\RoboSlam\blink\”.

Now, open Notepad++ which you will use to edit your C code. You will find a shortcut to start Notepad++ in the RoboSlam folder.

Type in (or copy and paste) the code below and save it as a file called “main.c” in your “blink” folder. For example, on my PC this file is “C:\Users\Ted\Desktop\RoboSlam\blink\main.c”.

//
// RoboSlam Example: Blink an LED
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>

int main( void )
{
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    
    // Set P2.0-2 as outputs and P2.3-7 as inputs
    // Set P1.0-7 as inputs
    P2DIR = 0b00000111;	
    P1DIR = 0b00000000;
    
    // Infinite loop
    while(1)
    {
        P2OUT = 0b00000100;      // LED on - set P2.2 high
        __delay_cycles(1000000); // 1 second delay
        P2OUT = 0b00000000;      // LED off - set P2.2 low
        __delay_cycles(1000000); // 1 second delay
    }
    
    return 0;
}

In the RoboSlam folder, you will find a file called “build_for_g2553.bat”. Copy this file into your “blink” folder.

Connect your Launchpad to the PC using the mini USB cable. Don’t worry if you see a message saying you need Administrator rights to install it – just click “Cancel”.

Once the build file is in your blink folder and the Launchpad is connected, you’re ready to build you program, which means to convert the C code you typed into binary machine code that the microcontroller understands. Double click “build_for_g2553” to start the build process. A command window will open and you’ll see some messages scrolling past. After a few seconds, the new program should be downloaded onto the microcontroller and you should see the LED flashing.

If it doesn’t work right away, or if you see errors appearing on the screen, don’t panic! It’s probably something simple – just ask one of the facilitators to take a quick look.

You are now ready to try out some more complex programs. The following sections provide a series of basic example programs.

Example 2: Controlling the motors

This program repeats the following steps over and over again: drive forward for 2 seconds, turn left for 1 second, drive forward for 2 seconds, turn right for 1 second. This makes the robot drive in a zigzag pattern.

Create a new folder called “motors” (for example, “C:\Users\Ted\Desktop\RoboSlam\motors\”), then type in (or copy and paste) the code below and save it as a file called “main.c” in that folder. You’ll also need to copy the file “build_for_g2553.bat” into the “motors” folder.

//
// RoboSlam Example: Drives around in a square
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>

int main( void )
{
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    
    // Set P2.0-2 as outputs and P2.3-7 as inputs
    // Set P1.0-7 as inputs
    P2DIR = 0b00000111;	
    P1DIR = 0b00000000;
    
    while(1)
    {
        P2OUT = 0b00000011;      // both motors forwards, LED off
        __delay_cycles(2000000); // 2 second delay
        P2OUT = 0b00000101;      // one motor forwards, one motor stop, LED on
        __delay_cycles(1000000); // 1 second delay
        P2OUT = 0b00000011;      // both motors forwards, LED off
        __delay_cycles(2000000); // 2 second delay
        P2OUT = 0b00000110;      // one motor stop, one motor forwards, LED on
        __delay_cycles(1000000); // 1 second delay
    }
    
    return 0;
}

Example 3: Responding to the colour sensor

This program demonstrates how the voltage – either high (3V) or low (0V) – on a digital input pin is read as 1 or 0 by the program. In this case, the digital input is pin P1.5 which is connected to the infrared colour sensor.

  • When the colour sensor is close to a black surface, the output voltage of the sensor is low, so the program reads P1.5 as 0 and sets P2.2 to 0, which turns off the LED.
  • When the colour sensor is close to a white surface, the output voltage of the sensor is high, so the program reads P1.5 as 1 and sets P2.2 to 1, which switches on the LED.

Create a folder called “colour” (for example, “C:\Users\Ted\Desktop\RoboSlam\colour\”), then type in (or copy and paste) the code below and save it as a file called “main.c” in that folder. You’ll also need to copy the file “build_for_g2553.bat” into the “colour” folder.

//
// RoboSlam Example: Test colour sensor
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>

int main( void )
{
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    
    // Set P2.0-2 as outputs and P2.3-7 as inputs
    // Set P1.0-7 as inputs
    P2DIR = 0b00000111;	
    P1DIR = 0b00000000;
    
    while(1)
    {
        // Light LED when white detected
        if (P1IN & BIT5) P2OUT = 0b00000100; // LED on
        else P2OUT = 0b00000000; // LED off
    }
    
    return 0;
}

Example 4: Using the colour sensor to navigate

As in the previous example, this program reads the voltage from the colour sensor as either 1 or 0 on pin P1.5. In this case though, when white is detected (i.e. pin P1.5 = 1) the robot stops and turns until the sensor is back on black before driving forwards again.

Create a folder called “navigate” (for example, “C:\Users\Ted\Desktop\RoboSlam\navigate\”), then type in (or copy and paste) the code below and save it as a file called “main.c” in that folder. You’ll also need to copy the file “build_for_g2553.bat” into the “navigate” folder.

//
// RoboSlam Example: Navigate by ground colour
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>

int main( void )
{
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    
    // Set P2.0-2 as outputs and P2.3-7 as inputs
    // Set P1.0-7 as inputs
    P2DIR = 0b00000111;	
    P1DIR = 0b00000000;
    
    while(1)
    {
        // Check if white is detected
        if (P1IN & BIT5)
        {
            // turn by running just one motor, LED on
            P2OUT = 0b00000101;
        }
        else
        {
            // both motors on, LED off
            P2OUT = 0b00000011;
        }
    }
    
    return 0;
}

Build Scripts

For reference, these are the same build scripts that are stored in the RoboSlam folder.

Here’s the one for the MSP430G2452, which should be saved as “build_for_g2452.bat” in the same folder as your C program file “main.c”:

@setlocal
@PATH=..\mspgcc-20120406-p20120911\bin\;..\MSP430Flasher_1.2.1\

@REM Compile the C code
msp430-gcc -mmcu=msp430g2452 main.c
@if errorlevel 1 goto end

@REM Convert compiled program to hex format
msp430-objcopy -O ihex a.out a.hex
@if errorlevel 1 goto end

@REM Copy hex file onto microcontroller
msp430flasher -n MSP430G2452 -w a.hex

:end
@pause

Here’s the one for the MSP430G2553, which should be saved as “build_for_g2553.bat” in the same folder as your C program file “main.c”:

@setlocal
@PATH=..\mspgcc-20120406-p20120911\bin\;..\MSP430Flasher_1.2.1\

@REM Compile the C code
msp430-gcc -mmcu=msp430g2553 main.c
@if errorlevel 1 goto end

@REM Convert compiled program to hex format
msp430-objcopy -O ihex a.out a.hex
@if errorlevel 1 goto end

@REM Copy hex file onto microcontroller
msp430flasher -n MSP430G2553 -w a.hex

:end
@pause

19 Responses to Programming

  1. Jim says:

    Can you provide instructions on how to create the build_for_g2553.bat batch file. I’m not familiar with the compiler program. Thank you!

    Like

    • batchloaf says:

      Hi Jim,

      To create the batch file, all you need to do is copy and paste the content from the final section of this webpage into Notepad and save it as “build_for_g2553.bat”. However, for it to work correctly, you need to have the compiler and other software structured the same way we do in our RoboSlam folder.

      We haven’t posted a zip file of the full RoboSlam folder on the website yet (we’ve just brought it to the workshops we’ve run, or given it to the facilitators directly). It’s about 130MB (although it squashes down a lot when we zip it) and although it only includes free software, we haven’t looked into the full details of distributing everything in it as one combined package.

      Anyway, by far the easiest thing would be for me to send you the zip file of the RoboSlam folder, which includes all the software you need and you can just extract it and copy the RoboSlam folder onto each participant’s PC. I’d send it right now, but my internet is running at a crawl, so it will have to wait until the morning when I’m back in the office. Maybe I can put it up on Google drive then and email you the link? The zip file is about 40MB to download as far as I recall.

      Once you have the RoboSlam folder extracted from the zip file, it’s really easy to compile the example programs on each PC. No installation as such is required – you just copy the RoboSlam folder onto (for example) the desktop of each participant’s PC and they’re ready to go!

      Ted

      Like

    • batchloaf says:

      Hi Jim,

      Actually, I managed to upload the zip file to google drive via my phone, so it’s there for you to download right now. Here’s the link:

      RoboSlam_14-10-2013.zip

      Just download it and extract the RoboSlam folder (which is all it contains) somewhere convenient. I have mine on my desktop. Everything you need (compiler, build scripts, text editor, etc) is in that folder, so you just need to copy it onto each participant’s PC and you’re ready to go. For each of the example programs (and other programs you might write yourself later on), just create a new folder inside the RoboSlam folder and copy the build script file (e.g. “build_for_g2553.bat”) into your new folder alongside your C code file (“main.c”).

      Let me know if you have any problems.

      Ted

      Like

  2. Alok says:

    i tried to interface multiple analog sensors for reading , but i am able to get the adc reading of only the first one ,,

    You got any code snippet which could do this task ?

    Like

    • batchloaf says:

      Hi Alok,

      In the current RoboSlam design, we are not using the ADC at all. The optical colour sensor is read as a digital input (the analog voltage from the sensor module is close to 3V for white and close to 0V for black, so it works fine for digital input).

      Are you building a RoboSlam robot or just using the MSP430 for something else? Either way, if you tell me which pins you want to use as analog inputs and exactly which MSP430 you’re using, I’ll try to post a simple example of analog reading from multiple inputs.

      Ted

      Like

  3. Jim says:

    Hi All. The kids are enjoying learning a little bit about programming microcontrollers. I don’t have a background in programming, so it’s been a good experience for me, too.

    One question:

    When I look at these lines of the code:

    P2OUT = 0b00000011; // motors forwards, LED off
    __delay_cycles(2000000); // 2 second delay
    P2OUT = 0b00000101; // one motor forwards, one reverse, LED on

    I’m focusing on the last 3 numbers on the P2OUT line. I was assuming that the first number controlled the LED (0 is off, 1 is on) .

    I assumed the second and third numbers controlled the 2 motors. I assumed that 0 was off, and 1 was on. But, according to the remarks, the “101” turns the LED on, and makes one motor go forward and the other motor in reverse. I assumed that it would be one motor going forward, the other stopped.

    We played around with the numbers, but haven’t seen the pattern.

    I guess my question is: How to the ones and zeroes work?

    I know you’re busy…if you have a chance, I’s appreciate your input. Thank you!

    Jim

    Like

    • batchloaf says:

      Hi Jim,

      Gosh, yes! Well spotted. That comment is completely wrong! Each motor can only go forwards or stop. There was a previous design which allowed bidirectional control of the motors and that’s where the comment originated, but it should have been updated. Anyway, thanks, I’ve fixed it now.

      Ok, so to explain all the 1s and 0s:

      You’re quite right that the last three digits of each of those numbers control the two motors and the LED.

      • The third last digit controls the LED. 1 switches it on. 0 switches it off.
      • The second last digit controls one motor. 1 switches it on. 0 switches it off.
      • The last digit controls the other motor. 1 switches it on. 0 switches it off.

      To explain in more detail, please allow me to go off on a tangent for a minute!

      You’ve probably heard that inside computers, everything is represented as 1s and 0s. What that means is that everything is represented as numbers in “binary” form. For example, when a piece of text is stored in computer memory, each letter is represented by a number (if you’re interested, you can see which numbers represent which letters here), and the whole piece of text is basically stored as a list of numbers. Similarly, when a photograph is stored in computer memory, it’s actually stored as a very long list of numbers. Each pixel in the image is represented by three numbers, one for how red it is, one for how blue it is, and one for how green it is. Anyway, whatever is being stored in computer memory has to somehow be represented as numbers, and within the computer those numbers are always represented in binary form, which means using only 1s and 0s.

      To explain binary numbers, let me begin by reviewing how decimal numbers work. Decimal numbers are the normal numbers we’re familiar with. For example, consider the number 5713 (five thousand seven hundred and thirteen).

      • The digit 3 represents a multiple of 1, i.e. 3 x 1 = 3 (1 is 10 to the power of 0)
      • The digit 1 represents a multiple of 10, i.e. 1 x 10 = 10 (10 is 10 to the power of 1)
      • The digit 7 represents a multiple of 100, i.e. 7 x 100 = 700 (100 is 10 to the power of 2)
      • The digit 5 represents a multiple of 1000, i.e. 5 x 1000 = 5000 (1000 is 10 to the power of 3)

      So the total value is 3×1 + 1×10 + 7×100 + 5×1000 = 3 + 10 + 700 + 5000 = 5713

      The decimal number system is “base 10” which means that each digit in a decimal number represents a multiple of a different power of 10 (1, 10, 100, 1000, etc). You will also notice that in decimal numbers (base 10), there are exactly ten possible values for each digit – 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9.

      The binary number system works exactly the same, except that it’s “base 2” which means that each digit represents a multiple of a different power of 2 (1, 2, 4, 8, 16, etc). Also there are only two possible values for each digit – 0 or 1. This takes a bit of getting used to, but it’s important to remember that it works exactly the same as decimal numbers. Here’s an example of a 4-bit binary number (that just means a binary number with 4 binary digits):

      Consider the binary number 1100. From right to left, the four digits are:

      • bit 0: 0, multiple of 2 to the power of 0. i.e. 0 x 1 = 0
      • bit 1: 0, multiple of 2 to the power of 1. i.e. 0 x 2 = 0
      • bit 2: 1, multiple of 2 to the power of 2. i.e. 1 x 4 = 4
      • bit 3: 1, multiple of 2 to the power of 3. i.e. 1 x 8 = 8

      So the total value is 0x1 + 0x2 + 1×4 + 1×8 = 0 + 0 + 4 + 8 = 12 (decimal).

      In other words the binary number 1100 is just a different way of writing the decimal number 12 (twelve).

      Note that many C compilers (including the one we’re using for the MSP430) allow us to write numbers in binary form by preceding the number with “0b”. This lets the compiler know you mean “0b1100” (binary 1100) rather than simply “1100” (one thousand one hundred). In the example programs, all binary numbers are written this way.

      Ok, so that’s a brief introduction to binary numbers. The reason this is relevant is that we’re using the individual bits (digits) of binary numbers to switch different pins on and off on the microcontroller. I mentioned that computers store everything as numbers (and specifically binary numbers). Each binary number is stored in what we call a “register”, which is basically just a small number storage area inside the chip which can hold one number at a time. Inside the MSP430, some registers can store 16-bit binary numbers and some can store 8-bit binary numbers.

      Most registers are just used to remember numbers, and that’s all. However, some registers have special jobs. For example, P2DIR, P2IN and P2OUT are all special registers which we use to control the 8 pins that make up what we call “Port 2”. Each of these three registers stores an 8-bit binary number. The MSP430G2553 has two 8-bit “ports”:

      • Port 1 consists of the following 8 pins: P1.0, P1.1, P1.2, P1.3, P1.4, P1.5, P1.6, P1.7
      • Port 2 consists of the following 8 pins: P2.0, P2.1, P2.2, P2.3, P2.4, P2.5, P2.6, P2.7

      The diagram below shows which pins are which on the chip (if you look closely at the LaunchPad, you’ll see that the same pin labels are shown beside the chip socket):

      To make it clearer, I’ve made the 8 pins that make up port 1 yellow, and the 8 pins that make up port 2 are coloured blue. I’m going to describe how P2DIR, P2IN and P2OUT control port 2 (the blue pins), but the same description basically applies to how P1DIR, P1IN and P1OUT control port 1 (the yellow pins).

      In the example code, we store the 8-bit binary number 0b00000111 into the P2DIR register, using the following statement:

      P2DIR = 0b00000111;
      

      Each bit in this 8-bit value controls the “direction” of one pin in Port 2 – i.e. whether it’s an input or an output. A 1 makes a pin an output. A 0 makes a pin an input. That value (P2DIR = 0b00000111) sets the 8 pins as follows (bits are counted from right to left starting at 0):

      • bit 0 = 1, This make P2.0 (pin 8) an output
      • bit 1 = 1, This make P2.1 (pin 9) an output
      • bit 2 = 1, This make P2.2 (pin 10) an output
      • bit 3 = 0, This make P2.3 (pin 11) an input
      • bit 4 = 0, This make P2.4 (pin 12) an input
      • bit 5 = 0, This make P2.5 (pin 13) an input
      • bit 6 = 0, This make P2.6 (pin 19) an input
      • bit 7 = 0, This make P2.7 (pin 18) an input

      Ok, hopefully you’re still with me here!

      Now, the number we store in P2OUT controls the voltage of any pins in Port 2 that are configured as outputs (remember, we’ve just made 3 pins outputs). Let’s say the following statement appears in our program:

      P2OUT = 0b00000101;
      

      Only the last three digits (“101”) of this number are going to have any effect, since these are the only pins which are configured as outputs.

      • bit 0 = 1, This sets pin P2.0 high (i.e. P2.0 voltage is 3V)
      • bit 1 = 0, This sets pin P2.1 low (i.e. P2.1 voltage is 0V)
      • bit 2 = 1, This sets pin P2.2 high (i.e. P2.2 voltage is 3V)
      • bit 3 = 0, P2.3 is an input, so this bit is ignored
      • bit 4 = 0, P2.4 is an input, so this bit is ignored
      • bit 5 = 0, P2.5 is an input, so this bit is ignored
      • bit 6 = 0, P2.6 is an input, so this bit is ignored
      • bit 7 = 0, P2.7 is an input, so this bit is ignored

      Of course, we can store different 8-bit binary numbers in P2OUT at different times, which will switch different pins on and off. If you look carefully at the circuit, you can probably make out how pins P2.0, P2.1 and P2.2 are controlling the motors and LED.

      • P2.0 (pin 8) controls one motor via a resistor and transistor.
      • P2.1 (pin 9) controls one motor via a resistor and transistor.
      • P2.2 (pin 10) controls the LED via the orange wire.

      I won’t go into detail about P2IN here, but suffice it to say that we use this to read the voltage on any Port 2 pins which are configured as inputs.

      Finally, I’ll just mention that it’s a matter of convenience that we’re writing the values in binary form in the program. For example, because binary 101 equals decimal 5, instead of writing

      P2OUT = 0b00000101;
      

      we could write

      P2OUT = 5;
      

      and the program would work exactly the same. The main advantage of writing the numbers in binary form in the program is that we can see which bits (and therefore which pins) we’re setting high and low.

      Ok, that was a very long answer to your question, and I’m not sure how clear it was. Please let me know if I need to explain it any more!

      Ted

      Like

  4. Jim says:

    Wow! This is so awesome. Thank you for such a detailed reply. This has been a fantastic description for some of my students who wanted to know more!

    Like

  5. Jim says:

    And me, too!

    Like

  6. Rahul says:

    i have to interface external led to port2 what was the requirement for this and what is the program.

    Like

    • batchloaf says:

      Hi Rahul,

      I’ll assume you’re using an MSP430G2553 microcontroller and that you’re connecting the LED to P2.0 (pin 8).

      Firstly, you need to connect a resistor in series with the LED. I suggest a value of approximately 200 Ohms, but this could vary according to your supply voltage and the specification of the LED. Anyway, 200 Ohms should be safe to test with and you should be able to clearly see the LED light up. Connect pin 8 on the MSP430 to the positive leg of the LED (normally the longer leg). Connect the negative leg of the LED to one leg of the resistor. Connect the other leg of the resistor to ground (0V). This completes the hardware part.

      The software part is fairly simple – you just need to set pin 8 as a digital output and then switch it high and low to switch the LED on and off.

      To make P2.0 (pin 8) an output, insert the following line at the start of your program:

      P2DIR = 0b00000001;
      

      Note that by writing the 8-bit binary number above into the P2DIR register, we are setting the direction (input or output) of all 8 pins in port 2 (i.e. P2.0, P2.1, P2.2, P2.3, P2.4, P2.5, P2.6 and P2.7). Each “0” in the 8-bit binary number sets one pin as an input, while each “1” sets one pin as an output. The line above sets every pin in port 2 as an input, except for P2.0 which is configured as an output.

      Then, anywhere else in your program, to switch on the LED…

      P2OUT = 0b00000001;
      

      …or to switch off the LED…

      P2OUT = 0b00000000;
      

      In the event that you have created additional outputs in port 2 (which may be required in your application), you may prefer to set just bit 0 of the P2OUT register to switch the LED on and off without affecting the other output pins in port 2. In that case, you can use the following to switch on the LED:

      P2OUT |= BIT0;
      

      …and the following to switch off the LED…

      P2OUT &= ~BIT0;
      

      Hopefully, that helps. Best of luck.

      Ted

      Like

  7. gajanan gadekar says:

    Can you tell me how interface the msp430g board to sim808 GPS GPRS GSM module and send me code and command to handle it ??
    Or send mail me to my id is gadekar.gajanan15@gmail.com

    Like

  8. Nilu says:

    Hello sir…
    Can you please help me about how to interface external adc 8051 to msp430g2553…
    It will be a great help sir… 🙂

    Like

Leave a comment