STM32 series - learn to press and hold the button in the easiest way

Table of contents

foreword

1. Long press principle

2. Cubmx configuration

3. Keil5 write code


foreword

This tutorial is based on stm32f103c8t6 minimum system board, hal library development.

The operation is simple, and the explanation is straightforward and clear, aiming to save everyone from detours.

1. Long press principle

(1) To use a timer, set the timer period and turn on the timer interrupt.

(2) Enter the timer interrupt callback function every once in a while to judge whether the button is pressed, if pressed, count++, convert the count into the desired time, for example: set the timer period to 0.1s, press and hold the button for 3s to click Light up an LED. In this case, every 0.1s, count++, 3s=0.1s*30, you need to enter the interrupt at least 30 times, if count>30, the LED will be on, otherwise the LED will not be on.

2. Cubmx configuration

Here, timer 1 is used as the timer interrupt, the clock source uses the internal clock, and the timer period is set to (720*1000)/72000000=0.01s. Timing 3s, then 0.01*300;

 Check Interrupted update.

 The configuration of the buttons is configured according to the actual situation, and the GPIO mode is configured as the input mode. This is to press the input low level, and the default is high level, so I pull up by default.

3. Keil5 write code

main.c file.

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/*
	每10ms进入一次该函数,检查一下引脚状态
	可以用定时器,也可以用查询
*/
void Key_scan(void)
{
	//1 表示高电平,0表示低电平。低电平时按下	
	uint8_t  val = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13);
	static uint16_t count=0;
	if(!val)//按键按下 记录按下的时间
	{
		count++;
	}
	else //按键松开 计数值清零并返回按下的时间值
	{
		if(count>300)//按下经过3s  300*0.01s=3s
		{
    		//长按3s
			key_flag = 2;
		}
		count=0;
	}
}

/**
 * @function:void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
 * @description: 定时器中断回调函数,0.01S中断一次
 * @param {TIM_HandleTypeDef *htim} 
 * @return {*}
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim==&htim1)
	{
		Key_scan();
	}
}
/* USER CODE END 0 */
/* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim1);//开启TIM1的定时器中断
/* USER CODE END 2 */
/* USER CODE BEGIN 3 */
	  if(key_flag == 2)//长按3s了
	  {
		  LED2_on();//放自己想要的操作
//	  key_flag = 0;
	  }
	  else//没有长按3s
	  {
		  LED2_off();//放自己想要的操作
	  }
  }
  /* USER CODE END 3 */

Note that key_flag=2 after long pressing for 3 seconds, and note that when there are other functions needed, key_flag should be cleared at the appropriate place, not at the place noted above, then the LED will turn on and then off, because if there Cleared to zero, every time the key_flag is judged in a loop, it will not be 2, and the LED will be off.

Code words are not easy, I hope friends who like it don't forget to like + bookmark + follow , your affirmation is the driving force for my creation.

欢迎大家积极交流,本文未经允许谢绝转载!!!

Guess you like

Origin blog.csdn.net/weixin_62261692/article/details/129962330