Although the ordinary delay function can be used to distinguish the long press and short press of the button, it is not easy to use and flexible in actual engineering applications and project development. More use the timer interval timing to calculate the time interval from pressing to releasing the key, and then by judging the time value to distinguish the state of long press and short press of the key. On the black Zigbee module of the Newland National Games equipment, or in the XMF09B and XMF09C made by Little Bee, short press the button SW1 to switch the switch state of the D5 light; long press the button SW1 to switch the switch state of the D6 light. Button SW1----------P1_2 D5 light-------------P1_3 (high level lights up) D6 light-------------P1_4 (high level lights up) [Implementation ideas] "1" Define a variable K_Press to mark the state of the button. When the button is in the pressed state, the value is 1; when the button is in the released state, the value is 0. "2" Define a variable count_t to calculate the time when the button is in the pressed state, that is, the time when K_Press is 1. "3" After the button is released, the state of long press and short press is distinguished by judging the value of count_t. "4" Every time a key state is processed, count_t is cleared to 0. [Implementation code] #include “ioCC2530.h†#define D3 P1_0 #define D4 P1_1 #define D5 P1_3 #define D6 P1_4 #define SW1 P1_2 unsigned char K_Press = 0; unsigned char count_t = 0; /*====================== Simple delay function============================================================================================================================================================================================================================================================================> ====*/ void Delay (unsigned int t) { while(t--); } /*======================Port initialization function======================= =*/ void Init_Port() { P1SEL &= ~0x1b; //P1_0, P1_1, P1_3 and P1_4 are used as general-purpose I/O ports P1DIR |= 0x1b; //P1_0, P1_1, P1_3 and P1_4 port output P1SEL &= ~0x04; //P1_2 as a general I/O port P1DIR &= ~0x04; //P1_2 port input P1INP &= ~0x04; //P1_2 is set to pull-up/pull-down mode P2INP &= ~0x40; //P1_2 is set to pull up D3 = 0; D4 = 0; D5 = 0; D6 = 0; } /*======================= Timer 1 initialization ================================================================================================================================================================================================================================================================================ ===*/ void Init_Timer1() { T1CC0L = 0xd4; T1CC0H = 0x30; //16MHz clock, divided by 128, timing 0.1 second T1CCTL0 |= 0x04; //Enable the output comparison mode of channel 0 T1IE = 1; EA = 1; T1CTL = 0x0e; //The frequency division factor is 128, modulo mode } /*==================== Timer 1 service function =============================================================================================================================================================================================================================================================== =*/ #pragma vector = T1_VECTOR __interrupt void TImer1_int() { T1STAT &= ~0x20; //Clear the overflow interrupt flag of timer 1 if (K_Press!= 0) //Press the button { count_t++; //Calculate the time value of the press } } /*====================Key scan processing function================================================================================================================================================================================================================================================================================= */ void Scan_Keys() { if (SW1 == 0) { Delay(100); //De-jitter processing if (SW1 == 0) { K_Press = 1; //Mark the button is being pressed while (SW1 == 0); //Wait for the button to be released K_Press = 0; //Mark the button has been released if (count_t》 5) //Long press the button { D6 = ~D6; } else //short press the button { D5 = ~D5; } count_t = 0; //Clear the key count value } } } /*=========================Main function============================================================================================================================================================================================================================================================================== ========*/ void main() { Init_Port(); Init_TImer1(); while(1) { Scan_Keys(); } } Suspension insulator,Post insulator,Metal end fittings,Power fittings Taizhou Yunzhi Power Equipment Co., Ltd. , https://www.thim-insulator.com