MSP430 LED’s & Switches

Now that we have the board and software ready lets blink a few led’s and switches present on the Launch Pad.The Launch pad comes with 2 LED’s ,

  1. green connected to the pin P1.0
  2. red connected to the pin P1.6.

There are 2 switches ,

  1. S1 connected to reset
  2. S2 connected to pin P1.3.

The IO’s are memory mapped in msp430 and hence no separate instructions are required to control them. The Ports are usually related to the following registers:.

>> PxDIR                    >> PxIE

>> PxOUT                  >> PxIFG

>> PxIN                       >> PxIES

>> PxSEL                      >> PxREN

We need to understand the first three for controlling the IO’s.

The PxDIR register is used to configure a particular port pin as input or output.Each bit corresponds to a pin on the port.Setting a particular bit makes that particular pin output and resetting it makes it input i.e,
1  =  output
0 =  input.       So to make the first pin P1.0 & pin P1.6 as an output we set the bits and to make the 3rd pin P1.3 input we reset the bit. So we write 0x41 to the register P1DIR  i.e, binary 0100 0001.

To write the values to the pins we set a bit in the PxOUT register to make the pin high and reset the bit to make it low.So to make the leds’s light up, which are active high we set the first and the sixth bit.The following code is used

P1OUT      =     0x41;             or           P1OUT    =    BIT0 + BIT6;

the second format makes use of the macros defined in the library files of the particular device.BIT0 is equal to 0x01 and BIT6 is equal to 0x40.These are easy to use and to understand and are defined from BIT0 to BIT15 with values 0x00 – 0xff.There are other macros defined for other registers as well which we will be seeing some other time.

To read a particular value we need to mask the PxIN register to check the bit.Since here we need to check the fourth pin or pin P1.3.This is done as follows.

(P1IN  & BIT3)

the above expression equates to a 8 bit value which will be 0 if the 4th pin is reset and equal to 0x08 if the pin is set.If the expression is equated to these values in a if condition then the required action can be written in the body of the loop.

Now coming to blinking an led.The following code is written.The code is self explanatory.

#include "io430.h"
int main( void )
  {
    int i;
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR=BIT0+BIT6;         // make pin 0 and 6 output
    P1OUT=BIT0+BIT6;        // make pin 0 and 6 high
    while(1)
    {
    P1OUT^=(BIT0+BIT6);    // toggle the led's
    for(i=0;i<9999;i++);    // delay between the toggle.
    }
   
  }

In the above code we XOR port 1 with BIT0 and BIT6 to toggle those particular bits.The loop is to give a delay so that toggling is seen.

Compile the code.It might give a warning as statement unreachable which is because of the return statement.Deleting it will give zero warnings.Then set the target options and hit download and debug.the Debug window will open.Press F5 to run the program or F11 to start Single step debugging.You can also click on menu option Debug > Go.

We see a line in the above code as WDCTL=WDTPW+WDTHOLD;

this is stop the watchdog timer which will be discussed later.But keep in mind that this line needs to be included else the cpu will reset every 32ms.

A program to toggle LED when a switch is pressed.The delay should be properly selected to compensate for the switch debouncing effect.

#include "io430.h"
  int main( void )
  {
    int i;
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
  P1DIR=BIT0+BIT6;         // make pin 0 and 6 o
utput
    P1OUT=BIT0+BIT6;        // make pin 0 and 6 high
    while(1)
    {
               if((P1IN & BIT3) == 0)
              {
                     P1OUT^=(BIT0+BIT6);    // toggle the led's
               }  
              for(i=0;i<27000;i++);    // delay between the toggle.
    }
}
Advertisements

2 thoughts on “MSP430 LED’s & Switches

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s