2228 views|0 replies

3836

Posts

19

Resources
The OP
 

【CC2530 Intensive Training 04】Timer interval timing to achieve N-click button [Copy link]

【Question Requirements】 The difficulty of developing the perception layer in the 2018 National Vocational College Skills Competition "Internet of Things Technology Application" National Competition (Higher Vocational Group) has increased sharply. All three questions are completed under the Zigbee protocol stack. The first question "Intelligent Control System of Warehouse Temperature and Humidity" examines single-click, double-click and triple-click of buttons. In order to help everyone better master the complex processing ideas of buttons, here we use a specific practical training case to explain the basic idea of realizing N-click of buttons through interval timing. On the black Zigbee module of the New World National Competition equipment, or in the XMF09B and XMF09C made by Little Bee, single-clicking the button SW1 switches the on/off state of the D5 lamp; double-clicking the button SW1 switches the on/off state of the D6 lamp; triple-clicking the button SW1 switches the on/off state of the D3 lamp; quadruple-clicking the button SW1 switches the on/off state of the D4 lamp. Button SW1-----------P1_2 D5 lamp--------------P1_3 (high level lights up) D6 lamp--------------P1_4 (high level lights up) D3 lamp--------------P1_0 (high level lights up) D4 lamp--------------P1_1 (high level lights up) 【Implementation ideas】 Each button press defines a life cycle. If it is 0.5 seconds, the final state of the button is determined at the end of the life cycle. If a new button is pressed during the life cycle of the button, the life cycle will be recalculated, and this is a double click. If a new button is pressed during the life cycle of a double click, the life cycle will be recalculated, and this is a triple click. If no new key is pressed during the entire life cycle, the final key state is a triple click. And so on. [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 #define SW2 P0_1 unsigned char count_t = 0; unsigned char K_Num = 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 general I/O ports P1DIR |= 0x1b; //P1_0, P1_1, P1_3 and P1_4 port output P1SEL &= ~0x04; //P1_2 is 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 } /*========================Timer 1 initialization=========================*/ void Init_Timer1() { T1CC0L = 0xd4; T1CC0H = 0x30; //16MHz clock, 128 division, timing 0.1 second T1CCTL0 |= 0x04; //Turn on the output compare mode of channel 0 T1IE = 1; EA = 1; T1CTL = 0x0e; //The division coefficient 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_Num != 0 && SW1 != 0) { count_t++; //Timer 1 overflows and adds 1 once. The overflow period is 0.1S } } /*=====================Key scanning processing function========================*/ void Scan_Keys() { if(SW1 == 0) { Delay(100); //Debounce processing if(SW1 == 0) { while(SW1 == 0); //Wait for the key to be released count_t = 0; //Recalculate the life cycle of the key K_Num++; //Change the button status if(K_Num > 4) //All four consecutive clicks are judged as four consecutive clicks { K_Num = 4; } } } if(count_t > 5) //The life cycle of the button ends { switch(K_Num) { case 1: //Single click of the button D5 = ~D5; break; case 2: //Double click of the button D6 = ~D6; break; case 3: //Click the button three times D3 = ~D3; break; case 4: //Click the button four times D4 = ~D4; break; } K_Num = 0; //After each button press, the status is cleared count_t = 0; //Reset the timer } } /*==========================Main function=============================*/ void main() { Init_Port(); Init_Timer1(); D3 = 0; D4 = 0; D5 = 0; D6 = 0; while(1) { Scan_Keys(); } }//Four consecutive button presses D4 = ~D4; break; } K_Num = 0; //After each button press, the status is cleared count_t = 0; //Timing is cleared } } /*==========================Main function=============================*/ void main() { Init_Port(); Init_Timer1(); D3 = 0; D4 = 0; D5 = 0; D6 = 0; while(1) { Scan_Keys(); } }//Four consecutive button presses D4 = ~D4; break; } K_Num = 0; //After each button press, the status is cleared count_t = 0; //Timing is cleared } } /*==========================Main function=============================*/ void main() { Init_Port(); Init_Timer1(); D3 = 0; D4 = 0; D5 = 0; D6 = 0; while(1) { Scan_Keys(); } }

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list