"CC2530 Intensive Training 02" General delay function to achieve long press and short press of keys

Source: Internet
Author: User

"CC2530 Intensive Training 02" General delay function to achieve long press and short press of keys

"topic Requirements"
the function of clicking and double clicking with a key is already a common method of many embedded products. It is common practice to use timer interval timing to calculate key press time, however, it is a fast way to distinguish between keystrokes and double clicks using the normal delay function. Press the button SW1 click , switch the D3 light switch State, the button SW1 double- Click, switch the D4 light switch state. among them:
key SW1-------p1_2
D3 lamp-----------p1_0(high-level lit)
D4 lamp-----------p1_1(high-level lit)

"Realization Idea"
<1> defines a normal delay function delay (), a variable count of time and a delay threshold of TT.
<2> when the SW1 is pressed for the 1th time, wait for the button to release, as long as count is less than TT, call delay () to accumulate the count variable.
<3> when Count is less than TT, the SW1 button is found to press again, then double-click the key.
<4> If there is no second keystroke pressed during the delay of count greater than or equal to TT, click the key.


"Implementation Code"

#include"ioCC2530.h"#defineD3 P1_0#defineD4 p1_1#defineK1 p1_2#defineTT 2000unsignedintCount =0;voidDelay (unsignedintt) {   while(t--);}voidInit_port () {P1sel&= ~0x03;//set P1_0 and p1_1 as Universal I/O portsP1dir |=0x03;//set P1_0 and p1_1 to output modeP1sel &= ~0x04;//set P1_2 to Universal I/O portP1dir &= ~0x04;//set P1_2 to input mode}voidScan_keys () {if(K1 = =0) {Delay ( -);//key to Jitter processing    if(K1 = =0)//Confirm that the button is pressed    {       while(K1 = =0);  while(Count <TT) {Delay ( -); Count++; if(K1 = =0)//double-click Processing{Delay ( -); if(K1 = =0)          {             while(K1 = =0); D4= ~D4; Count=0;  Break; }        }      }            if(Count >= TT)//Click Process{D3= ~D3; Count=0; }    }  }}voidMain () {init_port (); //Initialize PortD3 =1; D4=1; Delay (50000); D3=0; D4=0;  while(1) {Scan_keys (); //Scan button  }}

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

"CC2530 Intensive Training 02" General delay function to achieve long press and short press of keys

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.