msp430f5525实现按下P2.1,LED1亮1s,快速按下P2.1两次,LED2亮1s
//按下P2.1,LED1亮1s,快速按下P2.1两次,LED2亮1s
#include "msp430f5525.h"
unsigned int n=0;
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P1DIR |= BIT0;
P4DIR |= BIT7;
P2DIR &= ~BIT1;
P2REN |= BIT1;
P2OUT |= BIT1;
P1OUT &= ~BIT0;
P4OUT &= ~BIT7;
P2IE |= BIT1;
P2IES |= BIT1;
P2IFG &= ~BIT1;
_EINT();
while(1)
{
if(n==0)
{
P1OUT &= ~BIT0;
P4OUT &= ~BIT7;
}
else if(n==1)
{
P1OUT |= BIT0;
P4OUT &= ~BIT7;
__delay_cycles(1000000);
n=0;
}
else if(n==2)
{
P1OUT &= ~BIT0;
P4OUT |= BIT7;
__delay_cycles(1000000);
n=0;
}
}
return 0;
}
#pragma vector = PORT2_VECTOR
__interrupt void Port2()
{
unsigned int i=0;
if(!(P2IN & BIT1))
{
__delay_cycles(5000); //消抖
if(!(P2IN & BIT1))
{
//__delay_cycles(5000); //如果还是处于按下的状态,即视作未松手,第一次按下
while(!(P2IN & BIT1)){} //第一次松手
n = 1;
P2IFG &= ~BIT1;
//__delay_cycles(50000); //500ms后如果有按键按下,视作第二次按下
while((P2IN & BIT1)) //500ms-1000ms内如果不按下,视为只按一次
{
i++;
__delay_cycles(1000);
if(i>1000)
return;
} //程序因按下按键在此处跳出
if(!(P2IN & BIT1))
{
__delay_cycles(5000); //消抖
if(!(P2IN & BIT1))
{
while(!(P2IN & BIT1)){} //第二次松手
n = 2;
P2IFG &= ~BIT1;
}
}
}
}
}
|