Home > Uncategorized > MSP430 – Using a Port Pin as an Input Port

MSP430 – Using a Port Pin as an Input Port

February 28th, 2011 Leave a comment Go to comments

So far we have been using the Port Pins of the MSP430 only as an Output pin. In this section we will use a Port Pin as an input Pin. We will use Port Pin P1.0 as an Input Pin, in this particular example, but you may similarly use other port pins.

Imagine that a push button connected to P1.0. Assume that the MSP430 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. When the push button is not pressed, there is no LED blinking.

Copy paste the code given below in the IAR embedded system. As earlier, 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
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 )
{

WDTCTL = WDTPW + WDTHOLD;

// Assign P5.0 as the output pin
P5DIR |= BIT0;

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

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 by hitting F5, 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 completely 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.

If you would like to get an in depth understanding of the memory map of MSP430 you may like to refer to the MSP430 datasheets to learn about the memory maps.

Categories: Uncategorized Tags:
  1. No comments yet.