根据上个文章修改

//在结构体中添加两个变量
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)
    {
        //需要显示的内容
    }
}


 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐