根据上个文章修改
//在结构体中添加两个变量
struct keys
{
uchar judge_sta;
bool key_sta; //按键状态
bool single_flag; //短按键
bool long_flag; //长按键
uint key_time; //计时
};
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM4)
{
key[0].key_sta = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
key[1].key_sta = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
key[2].key_sta = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
key[3].key_sta = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
for(int i= 0; i<4 ; i++)
{
switch(key[i].judge_sta)
{
case 0:
{
if(key[i].key_sta == 0)
{
key[i].judge_sta = 1;
key[i].key_time = 0;
}
}
break;
case 1:
{
if(key[i].key_sta == 0)
{
key[i].judge_sta = 2;
}else{
key[i].judge_sta = 0;
}
}
break;
case 2:
{
if(key[i].key_sta == 1) //松开按键
{
key[i].judge_sta = 0;
if(key[i].key_time < 70)//加判断是因为防止长按键松开时触发短按键
{
key[i].single_flag = 1;
}
}else{ //没送按键
key[i].key_time++;
if(key[i].key_time >= 70)
key[i].long_flag = 1;
}
}
break;
}
}
}
}
//定义全局变量
uchar view = 0;
void key_proc(void)//按键函数
{
if(key[0].single_flag == 1)
{
view = !view;
LCD_Clear(Black); //切换界面清屏
key[0].single_flag = 0;
}
if(key[1].single_flag == 1)
{
//相应操作
key[1].single_flag = 0;
}
...
}
void disp_proc(void)//界面显示函数
{
if(view == o) //默认界面
{
//需要显示的内容
}
if(view == 1)
{
//需要显示的内容
}
}