GeBalanceBot/Reference/MiniBalance_HARDWARE/KEY/key.c

39 lines
1.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "key.h"
/**************************************************************************
Function: Key initialization
Input : none
Output : none
函数功能:按键初始化
入口参数:无
返回 值:无
**************************************************************************/
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure); // 根据设定参数初始化GPIOA
}
/**************************************************************************
Function: Key scan
Input : none
Output : 0No action1click
函数功能:按键扫描
入口参数:无
返回 值:按键状态 0无动作 1单击
**************************************************************************/
u8 click(void)
{
static u8 flag_key = 1; // 按键松开标志
if (flag_key && KEY == 0) // 检测到按键按下
{
flag_key = 0;
return 1; // 按键按下
}
else if (1 == KEY)
flag_key = 1;
return 0; // 无按键按下
}