高中生
最后登录1970-1-1
在线时间 小时
注册时间2021-6-9
|
#include "./key/bsp_key.h"
#include "bsp_led.h"
/**
* @brief 配置按键用到的I/O口
* @param 无
* @retval 无
*/
void Key_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*开启按键端口的时钟*/
RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK|KEY3_GPIO_CLK,ENABLE);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
// 设置按键的引脚为浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//使用结构体初始化按键
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN;
//设置按键的引脚为浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//使用结构体初始化按键
GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);
//选择按键的引脚
GPIO_InitStructure.GPIO_Pin = KEY3_GPIO_PIN;
//设置按键的引脚为浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//使用结构体初始化按键
GPIO_Init(KEY3_GPIO_PORT, &GPIO_InitStructure);
}
/*状态机按键,实现短按、长按、双击*/
typedef enum
{
KEY_STATE_RELEASE = 0,
KEY_STATE_PRESSED,
}Touch_State;
uint8_t KEY_self = 0;
//uint8_t KEY_longflag = 0;
//uint8_t KEY_longcnt = 0;
//uint8_t KEY_doubleflag = 0;
//uint8_t KEY_doublecnt = 0;
//按键状态机
uint8_t tough_detect(void)
{
static Touch_State touch_state = KEY_STATE_RELEASE;
static uint8_t KEY_longflag=0;
static uint8_t KEY_longcnt=0;
uint8_t result;
if(KEY_longflag==1)
KEY_longcnt++;
switch(touch_state)
{
case KEY_STATE_RELEASE:
if(GPIO_ReadInputDataBit(KEY1_GPIO_PORT,KEY1_GPIO_PIN)==Bit_SET)
{
KEY_longflag = 1;
touch_state = KEY_STATE_PRESSED;
result = KEY_NOT_PRESSED;
}
else
{
touch_state = KEY_STATE_RELEASE;
result = KEY_NOT_PRESSED;
}
break;
case KEY_STATE_PRESSED:
if(GPIO_ReadInputDataBit(KEY1_GPIO_PORT,KEY1_GPIO_PIN)==Bit_RESET)
{
if(KEY_longcnt<=100)
{
touch_state = KEY_STATE_RELEASE;
result = KEY_SHORT_PRESSED;
}
else
{
touch_state = KEY_STATE_RELEASE;
result = KEY_LONG_PRESSED;
}
KEY_longflag = 0;
KEY_longcnt = 0;
}
else
{
touch_state = KEY_STATE_PRESSED;
result = KEY_NOT_PRESSED;
}
break;
}
return result;
}
注释这句 ,if(KEY_longflag==1)
KEY_longcnt++;
短按就有用,不注释就出了问题,不知道哪里出了问题。
|
|