/******************************************************************************* * File Name: Key.c * Function Describe:device for the Keys * Relate Module: * Explain: Hardware version is HS110C * Writer: ShiLiangWen * Date: 2015.1.26 *******************************************************************************/ #include "stm32f10x.h" #include "Key.h" #include "includes.h" //key value variable unsigned char g_ucKeyValue; unsigned char g_ucDKC=0; unsigned char g_ucUKC=0; unsigned char g_ucKeyFree=0; unsigned char g_ucKeyMode=0; ////////////////////////// function declare ////////////////////////////// unsigned char KeyScanPort(void); ////////////////////////// end declare ////////////////////////////// /******************************************************************************** KeyInit PB5 CH_DM_KEY PB6 PPT_KEY PB7 CH_UP_KEY PB8 FUN_KEY *******************************************************************************/ void KeyInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /*GPIO先全部设置为上拉输入*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8; GPIO_Init(GPIOB, &GPIO_InitStructure); g_ucKeyMode=0;//持续模式 } /******************************************************************************** * Function:KeyScanPort *******************************************************************************/ unsigned char KeyScanPort(void) { unsigned long k; unsigned char ck; k=GPIOB->IDR; k&=0x01e0; #ifdef HS110C_MB_B k^=0x100; #endif ck=(unsigned char)(k>>4); return ck; } /************************************************************ *GetKey 读键盘数据 建议20ms调用一次此函数 有按键按下返回1 否则返回0 持续按下按键分2种情况: g_ucKeyMode=1时,只产生1次按键按下 g_ucKeyMode=0时,每秒产生1次按键按下实践 *************************************************************/ unsigned char GetKey(void) { static unsigned char sucCt=0;//连续模式下响应频率控制 static unsigned char sucKey=0; static unsigned char sucFlag=0; static unsigned char Key1,Key2; //去抖动 if(++sucFlag>3){ sucFlag=0; } if(sucFlag==0){ Key1=KeyScanPort(); }else if(sucFlag==2){ Key2=KeyScanPort(); }else return 0; if(Key1!=Key2)return 0; //无按键按下 if(Key1==0x1e){ g_ucKeyFree=1; g_ucDKC=0; sucKey=0; sucFlag=0; sucCt=0; return 0; } //有按键按下 g_ucUKC=0; if(g_ucKeyMode){//一次模式 if(sucKey!=Key1){ sucKey=Key1; g_ucKeyValue=sucKey; g_ucKeyFree=0; g_ucUKC=0; return 1; } }else{//连续模式 if(sucKey!=Key1){//第1次按下 sucKey=Key1; g_ucKeyValue=sucKey; sucCt=50;//第1次和第2次之间的间隔计数器 g_ucUKC=0; g_ucKeyFree=0; return 1; } if(--sucCt==0){ sucCt=10; //第2次以后的间隔计数器 g_ucUKC=0; g_ucKeyFree=0; return 1; } } return 0; } void KeyCount(void) { if(g_ucDKC<255)g_ucDKC++; if(g_ucUKC<255)g_ucUKC++; } /******************************************************************************** * End of Module *******************************************************************************/