Press P2.1, LED1 lights up for 1s, quickly press P2.1 twice, LED2 lights up for 1s
// Press P2.1, LED1 lights up for 1s, quickly press P2.1 twice, LED2 lights up for 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); //Debounceif
(!(P2IN & BIT1))
{
//__delay_cycles(5000); //If it is still in the pressed state, it is regarded as not released, the first presswhile
(!(P2IN & BIT1)){} //First releasen
= 1;
P2IFG &= ~BIT1;
//__delay_cycles(50000); //If a key is pressed after 500ms, it is regarded as the second presswhile
((P2IN & BIT1)) //If it is not pressed within 500ms-1000ms, it is regarded as only pressed once
{
i++;
__delay_cycles(1000);
if(i>1000)
return;
} //The program jumps out here because the key is pressedif
(!(P2IN & BIT1))
{
__delay_cycles(5000); //Debounceif
(!(P2IN & BIT1))
{
while(!(P2IN & BIT1)){} //Second releasen
= 2;
P2IFG &= ~BIT1;
}
}
}
}
}
|