Long key C language program

Publisher:genius5Latest update time:2013-12-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A total of four buttons are used.
#define _KEYDOWN_TEST_TIME (20)
unsigned char IsKeyDown(volatile unsigned char Value, unsigned char pin)
{
unsigned long CurState = 0, i;
for(i = 0; i < _KEYDOWN_TEST_TIME; ++i)
CurState += _GET_BIT(Value, pin)? 0:1; //The keyboard is connected to a pull-up resistor, and a low level is pressed

if(CurState == _KEYDOWN_TEST_TIME)
return 1;

return 0;
}

//All the following values ​​are based on the calling frequency of the DealWithKey function, which is determined by the timing value of timer 2.
//The count value of a short key press. A short press is considered only when the key detection counter counts greater than or equal to this value.
#define _KEY_SHORT_CNTR (2)
//The count value of a long key press. A long press is considered only when the key detection counter counts greater than or equal to this value.
#define _KEY_LONG_CNTR (_KEY_SHORT_CNTR * 3)
//Key detection interval when no key is pressed, used for key debounce.
#define _KEY_NORMAL_DEVIDER (2)

//This value is used for the execution frequency of the button detection function after a long press.
#define _ADDMIN_FAST_DEVIDER (15)
//This value is used for the execution frequency of the button detection function after a short press.
#define _ADDMIN_LONG_DEVIDER (_ADDMIN_FAST_DEVIDER * 3)
//This value is used to set the buzzer sounding time
. #define _BEEPER_DEVIDER (10)
void DealWithKey(void)
{
static unsigned int AddCntr = 0;
static unsigned int MinCntr = 0;
static unsigned int UnitCntr = 0;
static unsigned int RunCntr = 0;

static unsigned char BeepCntr = 0;

static unsigned int DevCntr = 0;

static unsigned char LastRunState = 0;

ed char temp;

if(g_State != Setting) //Running state, all keys are invalid
return;

if(BeepCntr > 0) //Process the buzzer
--BeepCntr;
else
_BEEPER_OFF;
 

if(DevCntr) //Key processing divider
{
--DevCntr;
return;
}

//This button can be entered repeatedly. After long pressing, it will act at a low frequency first, and then at a high frequency after several actions.
AddCntr = IsKeyDown(_PIN(_TO_KEY_ADD), _KEY_ADD)? AddCntr + 1 : 0;
if((temp = (AddCntr >= _KEY_LONG_CNTR)) || AddCntr >= _KEY_SHORT_CNTR)
{
...
}

//Same as above
MinCntr = IsKeyDown(_PIN(_TO_KEY_MIN), _KEY_MIN)? MinCntr + 1 : 0;
if((temp = (MinCntr >= _KEY_LONG_CNTR)) || MinCntr >= _KEY_SHORT_CNTR)
{
...
}

//This button cannot be entered repeatedly, that is, it will only work after it is lifted and then pressed
UnitCntr = IsKeyDown(_PIN(_TO_KEY_UNIT), _KEY_UNIT)? UnitCntr + 1 : 0;
if(UnitCntr == _KEY_SHORT_CNTR)
{
...
}
else if(UnitCntr > _KEY_SHORT_CNTR)
{
UnitCntr = _KEY_SHORT_CNTR + 1; //Repeated execution is prohibited
}

//This button will execute other functions only after the button is lifted
if(IsKeyDown(_PIN(_TO_KEY_RUN), _KEY_RUN))
{
++RunCntr;
}
else
{
RunCntr = 0;
if(LastRunState) //Indicates that the key was lifted only after it was pressed
{
LastRunState = 0;
g_State = EmptyInflatting;
}
}
if(RunCntr == _KEY_SHORT_CNTR)
{
BeepCntr = _BEEPER_DEVIDER;
_BEEPER_ON;
LastRunState = 1; //This key is effective when it is lifted.
}
else if(RunCntr > _KEY_SHORT_CNTR)
{
RunCntr = _KEY_SHORT_CNTR + 1; //Repeated execution is prohibited
}

DevCntr = _KEY_NORMAL_DEVIDER;
}
 

Reference address:Long key C language program

Previous article:Multiplexing approach leads to LED displays with fewer pins
Next article:Implementation of electronic photo frame based on Atmel-View

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号