Jump to content
43oh

Recommended Posts

I need to implement a push button like function on a input pin for a MSP430F5438A. The only way I know to implement it would be something like this:

 

while(1)

{

if(P1IN & 0x03 == 0)

...

else

...

}

 

But is it possible to have this same functionality without the use of an infinite loop?

 

Thanks in advance for any advice.

Link to post
Share on other sites

Interrupt?

 

Set the P1IE and P1IES to desired values, and write an interrupt under

 

#pragma vector = PORT1_VECTOR

__interrupt void P1_ISR (void){

//Check which pin interrupted and preform appropriate actions.

}

 

Also you don't need the ==0 in your code, if(P1IN & 0x3) will do the same thing, maybe even faster and take up less room.

Link to post
Share on other sites

As dkedr said interruptions are the best option. Try this:

#include 

int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= (BIT0 + BIT1); // Set P1.0 to output direction 
P1OUT &= ~(BIT0 + BIT1); // set P1.0 to 0 (LED OFF)
P1IE |= BIT3; // P1.3 interrupt enabled
P1IFG &= ~BIT3; // P1.3 IFG cleared
__enable_interrupt();

while(1)
{ }
}


#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= (BIT0 + BIT1); // P1.0 = toggle
P1IFG &= ~BIT3; // P1.3 IFG cleared
P1IES ^= BIT3; // toggle the interrupt edge,
}

 

Push button: P1.3

Led: P1.0

Link to post
Share on other sites
As dkedr said interruptions are the best option.

 

Interrupts should *never* be used on switches unless you are debouncing them in hardware. If you interrupt on an bouncy switch you'll get a train of high-speed interrupts every time you open and close the switch.

 

Anyone using a switch on a micro should read this:

 

http://www.eng.utah.edu/~cs5780/debouncing.pdf

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...