Programming – IAR version

Downloading and installing IAR Embedded Workbench

The development tool we’ll be using to compile the C code for the robot is IAR Embedded Workbench Kickstart for MSP430. Information about this software is available here:

http://www.ti.com/tool/iar-kickstart

There are several versions of this software, but the one we want in the “code size limited” version, which is completely free (i.e. not a time limited trial version). The code size limit just means that there is a restriction on the length of the programs you can write, but the limit is very unlikely to affect anything we do for simple robots. Here’s a direct link to the installation zip file for the correct version of IAR Embedded Workbench:

http://www.ti.com/lit/zip/slac050

Separator image

Creating a new project in IAR Embedded Workbench

Begin by creating a folder called RoboSlam somewhere on your computer, in which all the files for the robot program will be stored. I created mine on the desktop, as shown below:

Screenshot of RoboSlam project folder

Assuming you have already installed IAR Embedded Workbench, start it now. (If you haven’t installed it yet, you will need to do so before continuing.) Here’s what you should see when it opens:

Screenshot of IAR Embedded Workbench with no project open

Before we write the C code to control the robot, we need to create a “project” in IAR Embedded Workbench. To do this, open the “Project” menu and click “Create New Project…” as shown below:

Screenshot of IAR Embedded Workbench project menu

The “Create New Project” dialog box will open. The tool chain should already be set to “MSP430”. In the Project Templates list, click on “C” and then “main” and then “OK” as shown below:

Screenshot of IAR Embedded Workshop Create New Project dialog

You will be prompted to select a location where the project will be saved. Set the folder to the RoboSlam folder you created above and set the project file name to “RoboSlam” as shown below:

Screenshot of IAR Embedded Workbench Save Project As dialog

When the new project is created, IAR Embedded Workbench provides some very basic example code to get things started:

Screenshot of IAR Embedded Workbench with new project

Before compiling the code for the robot, we need to set some project configuration options. To open the project options dialog box, right click on the RoboSlam project at the top of the list in the Workspace pane on the left side of the IAR Embedded Workbench window, as shown below:

Screenshot of IAR Embedded Workbench showing project options menu item

Firstly, in the “General Options” section, under the “Target” tab, set the “Device” to “MSP430G2553”. The “Device” just means the model of MSP430 microcontroller that we’re using and the MSP430G2553 happens to be the one that is supplied with the LaunchPad.

Screenshot of IAR Embedded Workbench project options target dialog

Secondly, in the “Debugger” section, under the “Setup” tab, set “Driver” to “FET Debugger”, as shown below:

Screenshot of IAR Embedded Workbench project options dialog (driver )

The code shown below will blink the LED in the robot circuit. Type this into IAR Embedded Workbench.

Screenshot of IAR Embedded Workbench showing simple example code

Once you have typed in the program, to try it out on the robot, start the debugger by pressing Ctrl-D. The first time you do this, you will probably be prompted to save the “workspace” first. If so, just specify the RoboSlam folder that you created above and set the workspace file name to “RoboSlam”, as shown below:

Screenshot of IAR Embedded Workbench Save Workspace As dialog

When debugging starts, the program is paused initially. Don’t worry if you see a warning saying “Warning[Pe111]: statement is unreachable” – it’s just letting us know that the last line of the program (“return 0”) will never be reached because the “while loop” that switches the LED on and off is going to repeat forever. You should see something similar to the following:

Screenshot of IAR Embedded Workbench debugger paused

To start the program running, press F5. The LED on the robot circuit should start flashing slowly. If so, congratulations – you’ve just programmed the robot for the first time. To stop debugging, click on the red “X” in the debugging toolbar (circled in red below).

Screenshot of IAR Embedded Workbench debugger stop button

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

Separator image

Copying and pasting code examples

In each of the example programs below, line numbers are shown to the left of the C code. If you’re copying and pasting code from here into IAR Embedded Workbench, you don’t want the line numbers to appear. In order to copy the code without the line numbers, hover the mouse pointer over the code and you should see the following set of four icons appear at the top right corner of the code listing:

The first icon (the left-most one) opens a clean copy of the code (i.e. without line numbers) in a new window. The second icon copies the complete code example to the clipboard.

Example 1: Flashing the LED

This program just flashes the LED attached to pin P2.0 on and off slowly.

//
// RoboSlam Example Code for MSP430G2553
// Written by Ted Burke
// Last updated 26-11-2012
//
// This example flashes the LED
//

#include "io430.h"

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  // Make P2.0 a digital output
  P2DIR_bit.P0 = 1;

  // Infinite loop
  while(1)
  {
     P2OUT_bit.P0 = 1;	// LED on
     __delay_cycles(1000000);	// 1 second delay
     P2OUT_bit.P0 = 0;	// LED off
     __delay_cycles(2000000);	// 2 second delay
  }

  return 0;
}

Separator image

Example 2: Controlling the motors

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

//
// RoboSlam Example Code for MSP430G2553
// Written by Ted Burke
// Last updated 26-11-2012
//
// This drives the robot around in a square pattern
//

#include "io430.h"

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  // Make P2.0,1,2,3,4 digital outputs
  P2DIR_bit.P0 = 1;
  P2DIR_bit.P1 = 1;
  P2DIR_bit.P2 = 1;
  P2DIR_bit.P3 = 1;
  P2DIR_bit.P4 = 1;

  while(1)
  {
     P2OUT_bit.P0 = 1;	// LED on
     P2OUT_bit.P1 = 1;
     P2OUT_bit.P2 = 0;
     P2OUT_bit.P3 = 1;
     P2OUT_bit.P4 = 0;
     __delay_cycles(2000000);	// 2 second delay

     P2OUT_bit.P0 = 0;	// LED off
     P2OUT_bit.P1 = 0;
     P2OUT_bit.P2 = 1;
     P2OUT_bit.P3 = 1;
     P2OUT_bit.P4 = 0;
     __delay_cycles(1000000);	// 1 second delay
  }

  return 0;
}

Separator image

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 high, so the program reads P1.5 as 1 and sets P2.0 to 1, which lights the LED.
  • When the colour sensor is close to a white surface, the output voltage of the sensor is low, so the program reads P1.5 as 0 and sets P2.0 to 0, which switches off the LED.
//
// RoboSlam Example Code for MSP430G2553
// Written by Ted Burke
// Last updated 26-11-2012
//
// This example uses the colour sensor to control the
// LED. When white is detected, the LED turns off.
// When black is detected, the LED turns on.
//

#include "io430.h"

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  // Make P2.0 a digital output
  P2DIR_bit.P0 = 1;

  // Infinite loop
  while(1)
  {
     if (P1IN_bit.P5 == 1)
     {
       P2OUT_bit.P0 = 1; // LED on
     }
     else
     {
       P2OUT_bit.P0 = 0; // LED off
     }
  }

  return 0;
}

Separator image

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 = 0) the robot stops and turns around before driving forwards again.

//
// RoboSlam Example Code for MSP430G2553
// Written by Ted Burke
// Last updated 26-11-2012
//
// This example uses the colour sensor to control the
// LED. When white is detected, the LED turns off.
// When black is detected, the LED turns on.
//

#include "io430.h"

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;

  // Make P2.0, P2.1, P2.2, P2.3, P2.4 digital outputs
  P2DIR_bit.P0 = 1;
  P2DIR_bit.P1 = 1;
  P2DIR_bit.P2 = 1;
  P2DIR_bit.P3 = 1;
  P2DIR_bit.P4 = 1;

  // Infinite loop
  while(1)
  {
     // Check to see if white line is detected
     if (P1IN_bit.P5 == 0)
     {
       // If it is, turn around
       P2OUT_bit.P1 = 0;
       P2OUT_bit.P2 = 1;
       P2OUT_bit.P3 = 1;
       P2OUT_bit.P4 = 0;
       __delay_cycles(800000);
     }

     // When white line is not detected,
     // just drive forwards
     P2OUT_bit.P1 = 0;
     P2OUT_bit.P2 = 1;
     P2OUT_bit.P3 = 0;
     P2OUT_bit.P4 = 1;
  }

  return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s