Long press and short press based on MSP430F5529 buttons
[Copy link]
The difference between a long press and a short press is just adding an appropriate delay and then judging the pin status.
definition:
#define KEYDIR P2DIR
#define KEYIN P2IN
#define KEYIFG P2IFG
#define KEYIE P2IE
#define KEYIES P2IES
#define KEYREN P2REN
#define S1 BIT0
extern unsigned char key_key;
Handle long press and short press in interruption:
#pragma vector=PORT2_VECTOR //Declare interrupt vector address
__interrupt void GPIOIntHandler(void)
{
unsigned int IntState,vextend,cv;
unsigned long i;
IntState=KEYIFG;
if (IntState & S1) //When the key is pressed
{
vextend=0;
cv=0; //Long press flag is clearedwhile
(KEYIN& S1==S1) //If the key is in the pressed state, it will keep looping
{
for(i=1000;i>0;i--); //Appropriate delayvextend
++; //Count value plus 1
if(vextend==2500) //If the count value is equal to 2500, it means it is in the long press state (the time here can be set according to your needs)
{
vextend=0; //Count value is clearedwhile
(KEYIN& S1==S1)//The key is still in the pressed state
{ //Enter the long press state processingif
(key_key==99)
key_key=0;
else
key_key++;
LCD_ShowNum(0,0,key_key,3);
for(i=2000000;i>0;i--);//Appropriate delay to prevent the display from changing too quickly
cv=1; //The long press flag is set to 1 (indicates entering the long press handler)
}
}
}
if(cv==0)//The long press flag is 0
{
if(key_key==99)
key_key=0;
else
key_key++;
LCD_ShowNum(0,0,key_key,3);
}
}
KEYIFG &=~(S1);//Interrupt flag cleared
}
|