Basic example program for MSP430G2231

My colleagure Damon Berry and I have just spent a couple of hours playing around with a selection of components that we’re considering including in an ultra low-cost robotics kit that we’re designing. The kit is intended for a series of workshops we’re organising in the near future.

Damon and I both teach on the DIT RoboSumo module (aka Engineering Practice and Design) where our students use PIC18F4620 microcontrollers to control the robots. PICs are great in a lot of ways (robust, available in dual in-line packages, 5V compatible, relatively high current digital i/o, etc – all things that make life easier in our RoboSumo classes). We program the PIC microcontrollers with Microchip’s PICkit2 device, which I love. Unfortunately, it still costs about €30 and Microchip also seem to be phasing it out which makes me nervous about relying on its continuing availability. €30 is far too expensive for our robotics kit anyway, so we went in search of an ultra low-cost alternative.

The one we tried out today is the MSP430G2231 (14PDIP package for easy breadboard mounting), which is included in the Texas Instruments MSP-EXP430G2 LaunchPad starter kit. Amazingly, this great little kit (which includes two microcontrollers, a USB programmer/debugger, and a USB cable) costs only $4.30! It includes everything you need to get up and running with a basic microcontroller for the price of a breakfast roll.

We wanted to check that we could easily interface the MSP430G2231 to a colour sensor and a couple of motors using only low-cost parts and power the whole lot from either two or four AA batteries. We’ve still got one or two minor issues to iron out, but we got the MSP430 controlling a motor so things are looking promising. Here’s what we’re using:

  • MSP-EXP430G2 LaunchPad USB programmer
  • MSP430G2231 microcontroller
  • SN754410NE driver chip (pin compatible with the L293D; used to interface the MSP430 to the motors)
  • Cheap geared DC motor
  • 4xAA batteries in battery pack (we used these to supply the driver IC but not the MSP430 because we still need to find a suitable 3.3V regulator).
  • The IDE we used was IAR Embedded Workbench Kickstart for MSP430 v5.40.

Here’s the simple example program that we used:

// motor.c - Motor example for MSP430G2231
// written by Ted Burke and Damon Berry
// last updated 21-8-2014
#include "io430.h"
void ms_delay(long milliseconds);
int main( void )
    // Stop watchdog timer to prevent
    // time out reset
    // Make P1.0, P1.6, P1.7 outputs
    P1DIR_bit.P0 = 1;
    P1DIR_bit.P6 = 1;
    P1DIR_bit.P7 = 1;
        // LED on, motor forward
        P1OUT_bit.P0 = 1;
        P1OUT_bit.P6 = 0;
        P1OUT_bit.P7 = 1;
        ms_delay(1000); // 1s delay
        // LED off, motor backward
        P1OUT_bit.P0 = 0;
        P1OUT_bit.P6 = 1;
        P1OUT_bit.P7 = 0;
        ms_delay(1000); // 1s delay
    return 0;
// This function delays for the
// specific number of milliseconds.
// NB It's _very_ approximate!!
void ms_delay(long milliseconds)
    int n;
        for (n=0 ; n<200 ; ++n);

Once we decide on the rest of the components in the kit, I’ll post a bit more about it.

This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

9 Responses to Basic example program for MSP430G2231

  1. Bhavani says:

    this is one of the good program…..:-)

  2. alex says:

    Hey 🙂
    how can you be sure that P1DIR_bit.P6 = 1; is for port 2.6 ? I have a msp430G2553 and there are ports 1.0 to 1.7 and 2.0 to 2.5. So when i use “P1DIR_bit.P6 = 1;” will it be 1.6 oder 2.6 ?
    thanks for a reply
    Alex 🙂

    • batchloaf says:

      Hi Alex,

      There are separate registers for the two 8-bit ports P1 and P2.

      For example, digital intput/output through the P1 pins is achieved using the three registers P1DIR, P1IN, P1OUT. Each of the 8 bits in P1DIR controls the direction of one of the 8 pins in Port 1 (P1). The voltage on each input pin in P1 can be read by reading the corresponding bit in the P1IN register. Conversely, the voltage on each output pin in P1 can be controlled by setting the value of the corresponding bit in P1OUT.

      Digital input/output through the pins in Port 2 (P2) is controlled by three entirely separate 8-bit registers: P2DIR, P2IN and P2OUT.

      So, regarding the specific example you mentioned:

      P1DIR_bit.P6 = 1;

      This sets pin 6 in Port 1 (i.e. P1.6) as an output, because…

      • P1DIR is the register that controls the direction (input or output) of the eight pins in Port 1.
      • P6 selects bit 6 in the P1DIR register, which controls the direction of pin P1.6
      • Setting this bit equal to 1 makes the pin a digital output. (Setting it to zero would make it an input.)

      Hopefully that helps to clarify it?


      • batchloaf says:

        PS As it happens, I wrote a reply to a comment on another blog of mine which is closely related to this. You might find it helpful:


      • alex says:

        thanks, it helped very much.
        Anyway in this article above, you said “make output 2.6 and 2.7”. That still makes no sense for me, relating to your reply. You use P1DIR to control the direction of one of the pins in Port 1 (i don’t know how many, because i don’t have exactly the same board). So, as i understood, you set 1.x as output (1.0,1.1,1.2 and so on). As is said, you wrote “make output 2.6 and 2.7”. That’s what confuses me. Do you know what i mean ?
        In my opinion and my understanding it has to be 1.6 and 1.7, because of the Port 1, which only sets 1.x as output…
        well, i gonna repeat myself 😀

        PS: Sry for my bad english…

      • batchloaf says:

        Ahhhhh! Sorry, yes, I see what you mean now. That comment is wrong in my code example – it should say P1.6 and P1.7. I’ll change it now!

        Thanks for spotting that. I must have changed the code around and accidentally ended up with a comment that didn’t match the final version of the code.


      • Alex says:

        Okay 🙂
        Thanks anyway for the explanation!


  3. Hitanshu Patel says:

    Can you display your circuit diagram and Images of your projects.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s