OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2516|回复: 8

(求酷)比较适合实际应用的按键程序,用了状态机的思想

[复制链接]

1

主题

2

帖子

0

精华

新手入门

积分
26
金钱
26
注册时间
2013-9-19
在线时间
0 小时
发表于 2014-10-11 11:48:39 | 显示全部楼层 |阅读模式
废话不多说,直接上程序吧。大家可以讨论讨论,多聊聊程序架构的问题,希望不要断掉哦。。。
直接贴一部分程序,没办法字数限制。。。
[mw_shl_code=c,true]/********************************************************************************************** * File Name : bsp_key.c * Required Perconditions : * Called Functions : TIM_IsExpired(),TIM_GetTimestamp() * Author : Abner * Created : 02/09/2014 * Updated : 28/09/2014 * * =========================================================================================== * @attention * KeyValue : [15..13]->KeyState,[12..9]->Reserved,[8..3]->KeyID,[2..0]->KeyEvent * KeyState = keyval & 0xE000,KeyID = keyval & 0x01F8,KeyEvent = keyval & 0x0007, * bsp_timer.h and bsp_key.h ought to be included * Call Key_Init() before Key_GetValue() * * * <------ Add to Group BSP/HAL/SRC -------> * =========================================================================================== * @updated * 1.Deleted some Key_State * 2.Add the Current KeyState(KEY_STATE_INIT or KEY_STATE_UNRELEASED) to rtnval * 3.Replace defines for KeyEventime(in bsp_key.h) with Function(Key_SetEventTime) * 4.Define KEY_x_EffectiveLevel in header_file to make it convenient to modify it * so it can apply to other boards. * 5.Support 6 Independent_Keys(Max upto 10,but need modify Key_Init() & Key_GetID()) * 6.MultiKeyID were Deleted * 7.Fix bug : When you release other key during MultiPressed state,return KeyID won't * reflect this changes. * *********************************************************************************************/ /* ********************************************************************************************************* * INCLUDE FILES ********************************************************************************************************* */ #include "bsp_key.h" #include "bsp_timer.h" /* ********************************************************************************************************* * LOCAL CONSTANTS ********************************************************************************************************* */ /* ********************************************************************************************************* * LOCAL TABLES ********************************************************************************************************* */ /* ********************************************************************************************************* * LOCAL GLOBAL VARIABLES ********************************************************************************************************* */ static u32 KeyTime_Jitter = 5 ; //Time for excluding Jiteter interference static u32 KeyTime_Interval = 400 ; //Time for confirming status had swtiched static u32 KeyTime_Long = 800 ; //Time for confirming long pressed static u32 KeyTime_Double = 300 ; //Time between double pressed /* ********************************************************************************************************* ********************************************************************************************************* ** GLOBAL FUNCTIONS ********************************************************************************************************* ********************************************************************************************************* */ /* ********************************************************************************************************* * Key_Init() * * Description : Initialize GPIO Pin,Speed,Mode for Key. * * Argument(s) : None. * * Return(s) : None. * * Note(s) : (1) Key's GPIO(Pin,Speed,Mode) is defined in the bsp_key.h. ********************************************************************************************************* */ void Key_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; #if (KEY_SUPPORTNUM > 0) RCC_APB2PeriphClockCmd (KEY_0_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_0_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_0_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_0_GPIO_Mode; GPIO_Init (KEY_0_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 1) RCC_APB2PeriphClockCmd (KEY_1_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_1_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_1_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_1_GPIO_Mode; GPIO_Init (KEY_1_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 2) RCC_APB2PeriphClockCmd (KEY_2_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_2_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_2_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_2_GPIO_Mode; GPIO_Init (KEY_2_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 3) RCC_APB2PeriphClockCmd (KEY_3_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_3_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_3_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_3_GPIO_Mode; GPIO_Init (KEY_3_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 4) RCC_APB2PeriphClockCmd (KEY_4_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_4_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_4_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_4_GPIO_Mode; GPIO_Init (KEY_4_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 5) RCC_APB2PeriphClockCmd (KEY_0_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_5_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_5_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_5_GPIO_Mode ; GPIO_Init (KEY_5_GPIO_Port,&GPIO_InitStructure ); #endif } /* ********************************************************************************************************* * Key_GetID() * * Description : Get pressed Key(s)'(s) ID(s). * * Argument(s) : None. * * Return(s) : Pressed Key(s)'(s) ID(s). * * Note(s) : (1) Key's EffectiveLevel is defined in the bsp_key.h. ********************************************************************************************************* */ KeyID_Enum_t Key_GetID(void) { KeyID_Enum_t KeyIDTmp = KEY_ID_NONE ; #if (KEY_SUPPORTNUM > 0)&&(USE_WKUP_PA0 == 0) if(GPIO_ReadInputDataBit (KEY_0_GPIO_Port ,KEY_0_GPIO_Pin ) == KEY_0_EffectiveLevel) KeyIDTmp |= KEY_ID_0; #endif #if (KEY_SUPPORTNUM > 1) if(GPIO_ReadInputDataBit (KEY_1_GPIO_Port ,KEY_1_GPIO_Pin ) == KEY_1_EffectiveLevel) KeyIDTmp |= KEY_ID_1; #endif #if (KEY_SUPPORTNUM > 2) if(GPIO_ReadInputDataBit (KEY_2_GPIO_Port ,KEY_2_GPIO_Pin ) == KEY_2_EffectiveLevel) KeyIDTmp |= KEY_ID_2; #endif #if (KEY_SUPPORTNUM > 3) if(GPIO_ReadInputDataBit (KEY_3_GPIO_Port ,KEY_3_GPIO_Pin ) == KEY_3_EffectiveLevel) KeyIDTmp |= KEY_ID_3; #endif #if (KEY_SUPPORTNUM > 4) if(GPIO_ReadInputDataBit (KEY_4_GPIO_Port ,KEY_4_GPIO_Pin ) == KEY_4_EffectiveLevel) KeyIDTmp |= KEY_ID_4; #endif #if (KEY_SUPPORTNUM > 5) if(GPIO_ReadInputDataBit (KEY_5_GPIO_Port ,KEY_5_GPIO_Pin ) == KEY_5_EffectiveLevel) KeyIDTmp |= KEY_ID_5; #endif return KeyIDTmp; } /* ********************************************************************************************************* * Key_GetValue() * * Description : Get pressed Key's ID,and the first event key's event and status. * * Argument(s) : None. * * Return(s) : @ Description. * * Note(s) : (1) wait to update to support for getting each key's event and status independently. * (2) KeyState = keyval & 0xE000,KeyID = keyval & 0x01F8,KeyEvent = keyval & 0x0007. * (3) the time for Jitter and others is based on set Timestamp period to 1 ms (@bsp_timer) ********************************************************************************************************* */ u16 Key_GetValue(void) { static KeyAction_Struct_t KeyAction = {KEY_ID_NONE ,KEY_STATE_INIT ,KEY_EVENT_NONE ,0,0,0}; static u16 KeyValue = KEY_STATE_INIT | KEY_ID_NONE | KEY_EVENT_NONE; static KeyID_Enum_t KeyIDTmp = KEY_ID_NONE ; static u16 LastKeyValue = KEY_STATE_INIT | KEY_ID_NONE | KEY_EVENT_NONE ; KeyAction .KeyID = Key_GetID() ; switch(KeyAction .KeyState ) { case KEY_STATE_INIT : { if(KeyAction .KeyID != KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_JITTER ; KeyAction .KeyJitterTime = TIM_GetSTime (); } }break; case KEY_STATE_JITTER : { if(TIM_IsExpired (KeyAction .KeyJitterTime + KeyTime_Jitter )) { if(KeyAction .KeyID != KEY_ID_NONE ) { KeyIDTmp = KeyAction .KeyID ; KeyAction .KeyState = KEY_STATE_PRESSED ; KeyAction .KeyPressedTime = TIM_GetSTime (); } else { KeyAction .KeyID = KEY_ID_NONE ; KeyAction .KeyState = KEY_STATE_INIT ; } } }break; case KEY_STATE_PRESSED : { if(KeyAction .KeyID == KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_INTERIM1 ; KeyAction .KeyReleasedTime = TIM_GetSTime (); } if(TIM_IsExpired (KeyAction .KeyPressedTime + KeyTime_Long ) ) { if(KeyAction .KeyID == KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_INIT ; } else { KeyAction .KeyState = KEY_STATE_UNRELEASED ; } LastKeyValue = KeyAction .KeyState | KeyIDTmp | KEY_EVENT_LONG ; return LastKeyValue ; } }break; case KEY_STATE_INTERIM1 : { if(TIM_IsExpired (KeyAction .KeyReleasedTime + KeyTime_Interval )) { KeyAction .KeyState = KEY_STATE_JITTER ; } if(KeyAction .KeyID != KEY_ID_NONE ) { KeyIDTmp = KeyAction .KeyID ; KeyAction .KeyPressedTime = TIM_GetSTime (); KeyAction .KeyState = KEY_STATE_INTERIM2 ; } else { if(TIM_IsExpired (KeyAction .KeyReleasedTime + KeyTime_Double )) { KeyAction .KeyState = KEY_STATE_INIT ; LastKeyValue = KEY_STATE_INIT | KeyIDTmp | KEY_EVENT_SHORT ; return LastKeyValue ; } } }break; case KEY_STATE_INTERIM2 : { if(KeyAction .KeyID == KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_INIT ; LastKeyValue = KEY_STATE_INIT | KeyIDTmp | KEY_EVENT_DOUBLESHORT ; return LastKeyValue ; } else { if(TIM_IsExpired (KeyAction .KeyPressedTime + KeyTime_Long )) { KeyAction .KeyState = KEY_STATE_UNRELEASED ; LastKeyValue = KEY_STATE_UNRELEASED | KeyIDTmp | KEY_EVENT_SHORTLONG ; return LastKeyValue ; } } }break; case KEY_STATE_UNRELEASED : { if(KeyAction .KeyID == KEY_ID_NONE) { KeyAction .KeyState = KEY_STATE_INIT ; } else { KeyIDTmp = KeyAction .KeyID ; return KEY_STATE_UNRELEASED | KeyIDTmp | (LastKeyValue & 0x0007 ) ; } }break; default : break; } return KeyValue ; } /********************************************************************************************** * Function Name : Key_SetEventTime * Function Prototype : void Key_SetEventTime(u32 NewKeyTime_Jitter , * u32 NewKeyTime_Interval , * u32 NewKeyTime_Long , * u32 NewKeyTime_Double ) * Behavior Description : Set KeyTime_Jitter,KeyTime_Interval,KeyTime_Long,Time_Double * Input Parameter : NewKeyTime_Jitter,NewKeyTime_Interval, * NewKeyTime_Long,NewTime_Double * * Output Parameter : None * Return Parameter : None * Required Perconditions : None * Called Functions : None * * =========================================================================================== * @ attention * * * * *********************************************************************************************/ /* ********************************************************************************************************* * Key_SetEventTime(KeyTime_Jitter,...) * * Description : Set JitterTimer, IntervalTime, LongTime, and DoubleTime. * * Argument(s) : NewKeyTime_Jitter,NewKeyTime_Interval,NewKeyTime_Long,NewKeyTime_Double. * * Return(s) : None. * * Note(s) : (1) you'd better not to call this function ********************************************************************************************************* */ void Key_SetEventTime(u32 NewKeyTime_Jitter , u32 NewKeyTime_Interval , u32 NewKeyTime_Long , u32 NewKeyTime_Double ) { KeyTime_Jitter = NewKeyTime_Jitter ; KeyTime_Interval = NewKeyTime_Interval ; KeyTime_Long = NewKeyTime_Long ; KeyTime_Double = NewKeyTime_Double ; }[/mw_shl_code]

bsp_timer.h

2.26 KB, 下载次数: 195

bsp_timer.c

9.61 KB, 下载次数: 66

bsp_key.c

12.64 KB, 下载次数: 195

bsp_key.h

5.71 KB, 下载次数: 143

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

13

主题

314

帖子

0

精华

高级会员

Rank: 4

积分
713
金钱
713
注册时间
2012-7-20
在线时间
102 小时
发表于 2014-10-11 14:45:15 | 显示全部楼层
互联网,智能设备爱好者,欢迎讨论任何有意思的想法。
回复 支持 反对

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-10-11 21:40:39 | 显示全部楼层
谢谢分享....
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

56

主题

1237

帖子

0

精华

论坛大神

Rank: 7Rank: 7Rank: 7

积分
2644
金钱
2644
注册时间
2014-2-13
在线时间
518 小时
发表于 2014-10-12 15:54:35 | 显示全部楼层
收藏了!!!
技术交流,Sell 中颖单片机,欢迎私信骚扰
回复 支持 反对

使用道具 举报

3

主题

138

帖子

0

精华

初级会员

Rank: 2

积分
195
金钱
195
注册时间
2014-5-12
在线时间
4 小时
发表于 2014-10-12 21:44:54 | 显示全部楼层
看不懂。。。
回复 支持 反对

使用道具 举报

10

主题

561

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1878
金钱
1878
注册时间
2014-6-27
在线时间
995 小时
发表于 2014-10-13 08:19:07 | 显示全部楼层
有点复杂了,其实还可以更简单点。
回复 支持 反对

使用道具 举报

1

主题

2

帖子

0

精华

新手入门

积分
26
金钱
26
注册时间
2013-9-19
在线时间
0 小时
 楼主| 发表于 2014-10-17 13:07:55 | 显示全部楼层
回复【6楼】TinyBoy:
---------------------------------
嗯,可能没考虑充分吧。请问有什么好的建议吗?
回复 支持 反对

使用道具 举报

19

主题

248

帖子

2

精华

高级会员

Rank: 4

积分
842
金钱
842
注册时间
2012-2-8
在线时间
19 小时
发表于 2015-11-14 22:32:06 | 显示全部楼层
给画个图好理解点啊
回复 支持 反对

使用道具 举报

10

主题

147

帖子

0

精华

高级会员

Rank: 4

积分
602
金钱
602
注册时间
2015-7-11
在线时间
94 小时
发表于 2017-3-23 15:42:58 | 显示全部楼层
多谢分享学习了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-8-23 01:25

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表