Home > Uncategorized > MSP430 – LED ON and OFF Program

MSP430 – LED ON and OFF Program

February 27th, 2011 Leave a comment Go to comments

We will now move on to write a code that will be make an LED connected to an IO port to go ON and OFF.

Replace the io430.h include file in main.cpp with the file that correspond to the microcontroller that we are using (MSP430F149). In our case , this file is io430x14x.h. You may like to browse through the directory, that goes something like.

C:\Program Files\IAR Systems\Embedded Workbench 5.0\430\inc\

to check which include file needs to be used. Note that io430x14x.h corresponds to MSP430F149 – the microcontroller that we are using. This file contains the definitions of all the ports and memory locations. You may like to take a brief look at this file for now. You will need to refer this file many times in future. This is an important include file.

Now add the following code

#include
#define BIT0 0x01

int main( void )
{

// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
// Set the direction
P6DIR |= (BIT0) ;
// Set the Output to high
P6OUT|= (BIT0) ; // P6.0 High
// Set the output to low
P6OUT&=~(BIT0); // P6.0 Low

return 0;
}

In this exercise we will use the real hardware and will use the Emulator instead of Simulator. With the first row in the Files selected ( project name), select Project -> Options. Select Debugger, and in the Setup -> Driver select Fet Debugger.

This is your first introductory Program of the Microcontroller world. The codes in this program will give a digital low output on the port pin P6.0 and thereby make the LED glow.

Compile this program by hitting the F7 button.Make sure you have no errors. Now Press Control-D with the emulator and the board gets connected. This will download the object code to the evaluation board containing the LED. Run this program by hitting F5 Button. You will observe that the LED is glowing.

Understanding the Code

We are trying to make  a pin Рthe Port pin P6.0 of MSP430 as an IO Port.  Any Input Output pin of the microcontroller can be used either as an Input Pin or as an Output pin. The direction of the port pin is defined using the P6DIR. The statement

P6DIR |= (BIT0) ;

states that the P6.0 port pin has been configured as an out put pin. On a similar line, the statement P1DIR |= (0x04), will assign P1.2 to be an output pin. P1DIR is defined in the file io430x14x.h. You may like to open this file and search for P6DIR, if you want to get into details of it. You will notice that it corresponds to the memory location 0x036. You may now like correlate it with the memory map of the MSP430F149. If you look at the datasheet of MSP430F149 you will find that the direction of PORT6 is defined at memory location 0x036.

Now take a looks at the statement

P6OUT|= (BIT0) ; // P6.0 High

This statement will make the P6.0 pin to high. If your hardware was connected as as shown in Page 2 of the tutorial, this will make the LED go off.

The statement

P6OUT&=~(BIT0); // P6.0 Low

This statement will make the P6.0 pin to low. If your hardware was connected as as shown in Page 2 of the tutorial, this will make the LED go on.

You you write the program as

#include
#define BIT0 0x01

int main( void )
{

// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;

P6DIR |= (BIT0) ;
P6OUT&=~(BIT0); // P6.0 Low
P6OUT|= (BIT0) ; // P6.0 High

return 0;
}

and run it using F5, the LED will go off.

This program demonstrate the LED glowing ON and OFF case. Take a break and then learn more about it.

Categories: Uncategorized Tags:
  1. No comments yet.