LED blinker is certainly the equivalent of "Hello World" in the embedded world. This tutorial explains how to blink a LED using REOBoard. We assume that you are already familiar with integrated development environments such and Eclipse or equivalent. We also assume that you have a minimal knowledge about C/C++ programming language.

 Using ports

Blinking a LED requires that you use GPIO (General Purpose Input Output) ports. A GPIO port is basically a set of input output (I/O) pins, generally 8, 16, or 32 depending on the architecture. The MSP430 variant used in this tutorial (MSP430F5659) has several ports with various characteristics. P1 ~ P4 are GPIO with interrupt features. Interrupts will be explained in later tutorials.

In order to use a GPIO port, you have to set its direction. Usually, direction can be set bit by bit. In MSP430, a 1 on a given bit sets this bit to output. For example, setting 0x01 to the direction configuration register of port 1 will set bit 0 of port 1 to output mode.

Usage examples

(A) P1DIR = 0x01; Set port 1 bit 0 to output and set all other bits to input
(B) P1DIR |= 0x01; Set port 1 bit 0 to output and do not change other bits
(C) P1DIR = 0x05; Set port 1 bits 0 and 2 to output and set all other bits to input
(D) P1DIR &= ~0x01; P1DIR &= ~0x01; Clear port1 bit 0 and do no change other bits
(E) P1DIR ^= 0x01; Toggla port 1 bit 0

 

 

 

 

Of course, all these operations would have the same effect on any register.

Now similarily, we can actually set the value of, say, bit 0 of port 1 with the following command: P1OUT = 0x01; All the above variants are also valid

LED blinker program

This program will be extremely simple. But we will need a delay function otherwise we will not be able to see that the LED blinks. MSP430's default clock frequency is defined internally and is in the range of 1 MHz. One iteration of a while loop in this case takes about 10 clocks, which is also valid for a for loop. Therefore, one millisecond will take about 100 clocks. If we define a constant MILLISECOND as 100, then we can set a delay in milliseconds.

-------------------

//    Includes
#include <msp430F5659.h>

//    Constant definitions
#define MILLISECOND        100
//    Program protocols
void delay(unsigned short ms);

//    Main program
void main(void) {
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    P2DIR = 0x01;
    while(1) {
        P2OUT ^= 0x01;
        delay(1000);
    }
}

//    Delay functions
void delay(unsigned short ms) {
    volatile unsigned short i, j;
    for(i = 0 ; i < ms ; ++i) {
        for(j = 0 ; j < MILLISECOND ; ++j);
    }
}

-------------------

LEDGreen320Now in order to blink a LED, you need... a LED. A quick way to make the proper test is to solder a LED as shown by the picture on the right. Cut one of the pins and solder a resistor. The resistor value is not critical. We are working with 3.3V power. MSP430 can source at most 20 mA. As the LED itself drops between 1.6 and 3V depending on the color (less for red than for blue), you will need a resistor that can conduct about, say,  at most 10 mA. The calculation is as follows:

I = U/R and I < 10mA.

The worst case is when U at resistor is large. It happens for red leds. and in this case, it is (3.3 - 1.6) = 1.7V. The result is 170Ω which is the lower bound. Now if the resistor value is too high, the luminosity will be low. Therefore while choosing above 170Ω, we should choose a low value. The picture shows a resistor of 220Ω, that's what has been used. The current is therefore 6mA and the luminosity is enough.

 

LEDMount320You can now put the LED assembly directly into REOBoard's connectors. There is a ground pin close to P2 pin 0 (that's the reason why port 2 has been chosen).

That's about it. Compile, load, run, the LED should now blink as shown by the following picture.

Now some comments about this program. As we have briefly explained, we have to use some kind of delay in order to blink at relatively long intervals. What this means is that we are consuming processor cycles only to kill time. This is absolutely not efficient.

Beside this, if we want to do some other processing, like this:

while(1) {    ToggleLed();    Some processing();}

we would face another inconvenience: the loop time would depend on the processing. It would therefore be difficult to ensure a correct timing.

You said timing? We are going to learn in the next tutorial how to work around this inconvenience. We will introduce timers and interrupts, which will give us a completely new approach to get regularily spaced events in a program.

Download the code

A summary of the first tutorials has been added as a downloadable zip file here.