"CC2530 Intensive Training 03" Timer interval timing implementation key long press and short press

Source: Internet
Author: User

"CC2530 Intensive Training 03" Timer interval timing implementation key long press and short press

"Topic Requirements"

Although the normal delay function can be used to achieve the key long press and short press, but in the actual engineering applications and project development is not easy to use and flexible. It is more to use the timer interval timing to calculate the key from the press to the release of the interval, and then by judging the time value to distinguish between the key long press and the state of the short press.

In the Black ZigBee module of the New World national race equipment , or in the xmf09b and xmf09c made by the Bee , the key SW1 Short Press , switch the D5 light switch state; SW1 Long Press , switch the D6 light switch state.

Key SW1----------p1_2

D5 lamp--------------p1_3(high-level lit)

D6 lamp--------------p1_4(high-level lit)

"Realization Idea"

<1> defines a variable k_press, flags the key state. When the key is pressed, the value is 1, and when the key is released, the value is 0.

<2> defines a variable count_t that calculates the time at which the key is pressed, that is, when k_press is 1 o'clock.

<3> after the key is released, by judging the value of the count_t to distinguish the key long press and short press state.

<4> after each processing of a key state, the count_t is cleared 0.

"Implementation Code"

1#include"ioCC2530.h"2 3 #defineD3 P1_04 #defineD4 p1_15 #defineD5 p1_36 #defineD6 p1_47 #defineSW1 p1_28 9UnsignedCharK_press =0;TenUnsignedCharcount_t =0; One  A /*======================= Simple delay function ========================*/ - voidDelay (unsignedintt) - { the    while(t--); - } -  - /*====================== Port initialization function ========================*/ + voidInit_port () - { +P1sel &= ~0x1b;//p1_0, P1_1, P1_3, and p1_4 as general-purpose I/O ports AP1dir |=0x1b;//p1_0, P1_1, P1_3, and p1_4 port outputs at    -P1sel &= ~0x04;//P1_2 as a general-purpose I/O port -P1dir &= ~0x04;//p1_2 Port Input -P1INP &= ~0x04;//P1_2 set to pull-up/drop-down mode -P2INP &= ~0x40;//P1_2 set to pull- up -    inD3 =0; -D4 =0; toD5 =0; +D6 =0; - } the  * /*======================= Timer 1 Initialize ========================*/ $ voidInit_timer1 ()Panax Notoginseng { -t1cc0l =0xd4;  thet1cc0h =0x30;//16MHz Clock, 128-way, 0.1-second timing +T1cctl0 |=0x04;//turn on Channel 0 output comparison mode AT1ie =1; theEA =1; +T1ctl =0x0e;//The crossover factor is 128, modulo mode - } $  $ /*==================== Timer 1 service function ========================*/ - #pragmaVector = T1_vector -__interruptvoidTimer1_int () the { -T1stat &= ~0x20;//clears the overflow interrupt flag bit for timer 1Wuyi   if(K_press! =0)//Press the button the   { -count_t++;//Calculate the time value pressed down Wu   } - } About /*==================== key Scanning processing function ========================*/ $ voidScan_keys () - { -   if(SW1 = =0) -   { ADelay ( -);//de-jitter processing +     if(SW1 = =0) the     { -K_press =1;//the flag button is pressing $        while(SW1 = =0);//wait for the button to release theK_press =0;//Flag button has been released the        the       if(Count_t >5)//Press the long button the       {     -D6 = ~D6; in       } the       Else              //Press the button short press the       { AboutD5 = ~D5; the       } thecount_t =0;//Key count value clear 0 the     } +   } - } the Bayi /*========================= main function =============================*/ the voidMain () the { - Init_port (); - init_timer1 (); the  the    while(1) the   { the Scan_keys (); -   } the}

"Guangdong Vocational and Technical College Aohaoyuan < bee teacher > [email protected]"

"CC2530 Intensive Training 03" Timer interval timing implementation key long press and short press

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.