MSP430 Embedded Programming Tutorial TUTORIAL


LED Blinking Program


The program till now did not do anything visible. To strengthen our understanding let us write a program that will make the LED blink. We will write a simple delay function. The main program will call the dealy function between LED on and LED off.

Go ahead and copy paste the following code in the IAR embedded system. As before Replace the msp430x14x.h and the IO pin corresponding to your hardware. In this particular example there is an LED connected to Port P5.0. If you have an LED connected to, for example P2.3, then replace statement P5DIR |= BIT0 with P2DIR |= BIT3 and similarly the P5OUT lines.
#include <msp430x14x.h>

unsigned int delay ( unsigned int x)
{
unsigned int i,j;
for (i = 0; i<= x; i++)
{
for(j=0;j<=1000; j++)
;
}
return 0;
}

int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P5DIR |= BIT0;
while(true)
{
P5OUT|= BIT0 ; // P5.0 High
delay(100);
P5OUT&=~BIT0; // P5.0 Low
delay(100);
}

return 0;
}


If we run the program freely, it will make the LED blink. You may change the duration of the blink by changing the parameter of the delay(100).

Till now we have been experimenting by making a port pin act as an out pin. In the next chapter we will see how to use a port pin as an input pin.