Blinking LED(hello world! of micro controllers)

This post is about the blinking led’s. Those of you who know c/c++/java or any programming language for that matter would have definitely written the first program as hello world. In case of micro controllers blinking led’s is the hello world sort of thing.

Theory:

On launchpad there are two surface mount led’s (smd led’s), they differ from normal led’s in the sense that there are tiny as compared to the normal ones. We generally use current limiting resistors while using led’s of any sort, but Texas has taken care of that for us already by connecting these on board led’s with current limiting smd resistors. We make use of port 1 pins 0 and 6 to which these led’s are connected. When the port is made high the led glows and when its made low the led turns OFF. Thus we first configure the P1.0 and P1.6 pins as output pins using the direction register. Then we toggle the output register after some delay so that the change is registered by human eyes(persistence of vision remember??).

Code:

//Author: Manpreet Singh Minhas
//Date : 2/10/2013
//learningmsp430.wordpress.com
#include <msp430g2211.h>// include the header file for msp430g2211
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;                                                                          //stop watchdog timer
P1DIR|=BIT0+BIT6;                                                                                                    //set the port 1 pin 0 and pin 6 as output you can do this by using hex                                                                                                                                              *command word as well i.e 01000001 or 0x041 or 41h
P1OUT|=BIT0; // make port1 pin0 i.e P1.0 high
int i=0;
for(;;){                                                                                                                               // infinite loop so that the action is repeated indefinitely

P1OUT ^= (BIT0+BIT6);                                                                                                 // toggle using ex-or with 1 ( refer truth table of ex-or operation.)
for(i=0;i<20000;i++){

// some delay for the human eye to observe the change
}

}
}

Video:

*: Those of you who don’t know what is a command word. Command word is 8bit or 16 bit data that is given to the special function registers to configure the device how we want. In this case it is the P1DIR and P1OUT registers. Thus these two are command word registers and the data we send is command word. In msp430g2xx1 making P1DIR.x high/1 makes the port pin as output and low/0 makes it input. P1OUT makes the port high if the bit is set and makes it low if the bit is reset.

Advertisements