- uint8_t Key_State[4];
-
- /**
- *@brief 功能按键扫描函数,放在1-10ms定时器即可
- 松手计时时间@Key_RTim 和 长按触发时间@Key_LTim
- 根据具体定时器中断时间调整,一般松手时间为100-250ms
- *@param Key_RTim Key_LTim Key_PTim
- *@retval 无
- */
- void Key_Scan(void)
- {
- static uint8_t Key_PressTime[4]; //按下计时
- static uint8_t Key_ReleaseTime[4]; //松手计时
- static uint8_t Key_PressCount[4]; //按下计次
- static uint8_t Key_PressFlag; //按下标志
-
- uint8_t i;
- for(i = 0;i < 4 ;i++) //循环扫描P32—P35口
- {
- if(~KEY_PORT & (0x04 << i))
- {
- if(~Key_PressFlag & (0x01 << i))
- {
- Key_PressFlag |= (0x01 << i);
- Key_PressCount[i]++;
- Key_ReleaseTime[i] = Key_RTim;
- }
- if(Key_PressTime[i] < 255)
- {
- Key_PressTime[i]++;
- }
- if(Key_PressTime[i] == Key_LTim)
- {
- Key_PressCount[i] = 0;
- Key_ReleaseTime[i] = 0;
- Key_State[i] = Key_Long;
- }
-
- }
- else
- {
- Key_PressFlag &= ~(0x01 << i);
- Key_PressTime[i] = 0;
- if(Key_ReleaseTime[i] > 0)
- {
- Key_ReleaseTime[i]--;
- }
- else
- {
- if(Key_PressCount[i])
- {
- switch (Key_PressCount[i])
- {
- case 1: Key_State[i] = Key_Click; break;
- case 2: Key_State[i] = Key_Double; break;
- default: Key_PressCount[i] = 0; break;
- }
- Key_PressCount[i] = 0;
- }
- }
- }
- }
- }
-
- /**
- *@brief 功能按键状态查询函数,可查询按键的单击双击三击长按状态
- *@param Key_0 Key_1 Key_2 Key_3
- *@retval Key_Click Key_Double Key_Long
- */
- uint8_t Key_GetState(uint8_t Key_x)
- {
- uint8_t Key_Temp[4];
- Key_Temp[Key_x] = Key_State[Key_x];
- Key_State[Key_x] = 0;
- return Key_Temp[Key_x];
- }
复制代码
|