【HC32F460开发板测评】04.独立按键的识别与处理
[复制链接]
本篇章主要掌握GPIO作为输入引脚时当作按键的应用;实验的功能是配置4个独立按键为输入检测,当按键按下时对应的LED灯点亮,当按键松开时对应的LED灯熄灭;KEY0~3分别对应LED0~3。
硬件原理图:
从原理图上我们可以看出,当按键没有按下时,按键默认处于高电平的状态,当按键被按下后,按键处于低电平状态,我们知晓了这个特性,就可以来编写按键检测程序了。
软件实现:
按键头文件:
#ifndef __KEY_H__
#define __KEY_H__
#ifdef __cplusplus
extern "C" {
#endif
#undef EXTERN
#ifdef __KEY_C__
#define EXTERN
#else
#define EXTERN extern
#endif
#include "config.h"
#define KEY0_PORT (PortD)
#define KEY0_PIN (Pin03)
#define KEY1_PORT (PortD)
#define KEY1_PIN (Pin04)
#define KEY2_PORT (PortD)
#define KEY2_PIN (Pin05)
#define KEY3_PORT (PortD)
#define KEY3_PIN (Pin06)
#define KEY0_READ() PORT_GetBit(KEY0_PORT, KEY0_PIN)
#define KEY1_READ() PORT_GetBit(KEY1_PORT, KEY1_PIN)
#define KEY2_READ() PORT_GetBit(KEY2_PORT, KEY2_PIN)
#define KEY3_READ() PORT_GetBit(KEY3_PORT, KEY3_PIN)
EXTERN void KEY_Init(void);
EXTERN void KEY_Scan(void);
#ifdef __cplusplus
}
#endif
#endif
按键源程序:
/*******************************************************************************
* @file KEY.c
* @author xld0932
* @version V1.00
* @date 31-Mar-2021
* @brief ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __KEY_C__
/* Includes ------------------------------------------------------------------*/
#include "KEY.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void KEY_Init(void)
{
stc_port_init_t stcPortInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
/* KEY0 Port/Pin initialization */
PORT_Init(KEY0_PORT, KEY0_PIN, &stcPortInit);
/* KEY1 Port/Pin initialization */
PORT_Init(KEY1_PORT, KEY1_PIN, &stcPortInit);
/* KEY2 Port/Pin initialization */
PORT_Init(KEY2_PORT, KEY2_PIN, &stcPortInit);
/* KEY3 Port/Pin initialization */
PORT_Init(KEY3_PORT, KEY3_PIN, &stcPortInit);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t KEY_Read(void)
{
uint8_t Value = 0;
if(KEY0_READ() == Reset) Value |= 0x01;
if(KEY1_READ() == Reset) Value |= 0x02;
if(KEY2_READ() == Reset) Value |= 0x04;
if(KEY3_READ() == Reset) Value |= 0x08;
return Value;
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_PressedHandlere(uint8_t Value)
{
if(Value & 0x01) LED0_ON();
if(Value & 0x02) LED1_ON();
if(Value & 0x04) LED2_ON();
if(Value & 0x08) LED3_ON();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_ReleaseHandlere(uint8_t Value)
{
if(Value & 0x01) LED0_OFF();
if(Value & 0x02) LED1_OFF();
if(Value & 0x04) LED2_OFF();
if(Value & 0x08) LED3_OFF();
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_Scan(void)
{
static uint8_t KeyState = 0;
static uint8_t KeyCount = 0;
static uint8_t KeyValue = 0;
uint8_t Value = KEY_Read();
if(KeyState == 0)
{
if(Value != 0)
{
if(KeyCount++ >= 5)
{
KeyState = 1;
KeyCount = 0;
KeyValue = Value;
KEY_PressedHandlere(KeyValue);
}
}
else
{
KeyCount = 0;
}
}
else
{
if(Value != KeyValue)
{
if(KeyCount++ >= 5)
{
KeyState = 0;
KeyCount = 0;
KEY_ReleaseHandlere(KeyValue);
KeyValue = 0;
}
}
else
{
KeyCount = 0;
}
}
}
/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/
主程序调用按键扫描:
/*******************************************************************************
* @file main.c
* @author xld0932
* @version V1.00
* @date 31-Mar-2021
* @brief ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __MAIN_C__
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void InitSystem(void)
{
shellPortInit();
KEY_Init();
LED_Init();
printf("\r\n");
printf("\r\nHC32F460PETP %s %s\r\n", __DATE__, __TIME__);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
int main(void)
{
InitSystem();
while(1)
{
KEY_Scan(); Ddl_Delay1ms(10);
}
}
/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/
运行效果:
工程代码:
Project_KEY.zip
(826.31 KB, 下载次数: 29)
|