按键引脚设置为上拉输入
选择时钟源
配置定时器
80MHz ÷ 80 ÷ 10000 = 10ms
打开NVIC
key.h
#ifndef _KEY_H_
#define _KEY_H_
#include "main.h"
#include "stdbool.h"
struct keys
{
uint8_t stg;
uint16_t time;
bool flag;
bool keycode;
bool flag_long;
};
#endif
key.c
#include "key.h"
struct keys key[4];
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim ->Instance == TIM3)
{
key[0].keycode = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
key[1].keycode = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
key[2].keycode = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
key[3].keycode = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
for(uint8_t i = 0; i < 4; i++)
{
switch(key[i].stg)
{
case 0:
{
if(key[i].keycode == 0)
key[i].stg = 1;
}
break;
case 1:
{
if(key[i].keycode == 0)
{
key[i].stg = 2;
}
else
{
key[i].stg = 0;
}
}
break;
case 2:
{
if(key[i].keycode == 1)
{
key[i].stg = 0;
if(key[i].time < 100)
{
key[i].flag = 1;
}
}
else
{
key[i].time++;
if(key[i].time >= 100)
{
key[i].flag_long = 1;
}
}
}
break;
}
}
}
}
在main.c中调用Key的数据
HAL_TIM_Base_Start_IT(&htim3); //开启定时器
Comments NOTHING