MSP430 Embedded Programming Tutorial TUTORIAL


Using a Port Pin as an Input Port


So far we have been using the Port Pins only as an Output pin. In this Chapter we will use a Port Pin as an input Pin. In this particular example we will use Port Pin P1.0 as an Input Pin. Imagine that a push button connected to P1.0. The hardware is designed such that the Input to the Pin p1.0 in normally 0 and when the button is pressed, the Pin becomes 1. We now need to write a code that will blink the LED, while the push button is kept pressed.

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 and a push button is connected at Pin P1.0. Pin P5.0 is used as an output pin and Pin P1.0 is used as an input pin. 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;
P1DIR&=~BIT0; //P1.0 is used as Input Pin

while(true)
{
if ( (P1IN & BIT0) == 0x01)
{
P5OUT|= BIT0 ; // P5.0 High
delay(100);
P5OUT&=~BIT0; // P5.0 Low
delay(100);
}
else
{
P5OUT|= BIT0 ; // P5.0 High
delay(100);
}
}

return 0;
}


If we run the program freely, it will make the LED blink if you keep the pin P1.0 high be pressing the key.

To define a pin to be used as an Input pin we use the following statement.

P1DIR&=~BIT0; //P1.0 is used as Input Pin


To completelet understand this statement, you will need to go to the msp430x14x.h and search for P1DIR. You will notice that P1DIR corresponds to memory location 0x0022. If you want to read the deatils you may like to open the document slau049f.pdf from Texas Instruments and read the details of this memory location.

It is beyond the scope of this tutorial to describe the memory map of the MSP430. You should refer to the Texas Instruments documents whenever you need to find the details of an internal memory location and settings.

In the next chapter we will learn the concept of interrupt and timers.