赞
踩
本博客记录本人学习使用按键和定时器实现软件消抖和长按功能的实现。代码部分仿照b站UP主01Studio的蓝桥杯教程。视频连接:【备战2024蓝桥杯 嵌入式组】CT117E-M4 新款开发板 3小时省赛模块 速成总结_哔哩哔哩_bilibili
目录
二、代码编写(这部分代码仿照b站UP主01Studio的蓝桥杯教程)
配置定时器,选择时钟源,配置预分频系数和溢出值,我这边将定时器4设置为10ms(80M/(79+1)/(9999+1))进入中断。再将定时器中断打开
视频链接:【备战2024蓝桥杯 嵌入式组】CT117E-M4 新款开发板 3小时省赛模块 速成总结_哔哩哔哩_bilibili
- #include "stdbool.h"
-
- struct keys
- {
- bool key_std;//用于存储按键状态
- unsigned char junk_std;//用于记录按键按下后进入中断次数
- bool single_flag;//单击按下标志位
- };
- struct keys key[4];//创建结构体数组,分别对应4个按键
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM4)//判断定时器4进入中断
- {
- key[0].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);//读取当前按键状态
- key[1].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);//读取当前按键状态
- key[2].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);//读取当前按键状态
- key[3].key_std=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);//读取当前按键状态
- for(unsigned char i=0;i<4;i++)//对4个按键进行判断
- {
- switch(key[i].junk_std)//按下按键后进入中断的次数并判断
- {
- case 0://当按键按下后,第一次不做任何操作,用于消抖10ms
- {
- if(key[i].key_std==0)
- {
- key[i].junk_std=1;
- }
- }break;
- case 1://第二次检测到按键按下后,将单击标志位single_flag设置为1
- {
- if(key[i].key_std==0)
- {
- key[i].junk_std=2;
- key[i].single_flag=1;
- }
- }break;
- case 2://直到按键松开才将计算值设置为0,重新开始判断,这样单击不受长按影响
- {
- if(key[i].key_std==1)
- {
- key[i].junk_std=0;
- }
- }break;
- }
- }
- }
- }
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM4_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim4);//开启定时器中断
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- for(unsigned char a=0;a<4;a++)
- {
- if(key[a].single_flag==1)
- {
- key[a].single_flag=0;//清除单击标志位
- LED(a);
- }
- }
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
长按功能是在单击的基础上进行修改添加。
- #include "stdbool.h"
- struct keys
- {
- bool key_std;//用于存储按键状态
- unsigned char junk_std;//用于记录按键按下后进入中断次数
- bool single_flag;//单击标志位
- unsigned int key_tim;//记录长按时间
- bool long_flag;//长按标志位
- };
- struct keys key[4];//创建结构体数组,分别对应4个按键
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM4)
- {
- key[0].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);//读取当前按键状态
- key[1].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);//读取当前按键状态
- key[2].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);//读取当前按键状态
- key[3].key_std=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);//读取当前按键状态
- for(unsigned char i=0;i<4;i++)//对4个按键进行判断
- {
- switch(key[i].junk_std)//按下按键后进入中断的次数并判断
- {
- case 0://当按键按下后,第一次不做任何操作,用于消抖10ms
- {
- if(key[i].key_std==0)
- {
- key[i].key_tim=0;//将长按时间记录值清零
- key[i].junk_std=1;
- }
- }break;
- case 1://第二次检测到按键按下后,将单击标志位single_flag设置为1
- {
- if(key[i].key_std==0)
- {
- key[i].junk_std=2;
- // key[i].single_flag=1;//这里不能直接判断为单击
- }
- }break;
- case 2://直到按键松开才将计算值设置为0,重新开始判断,这样单击不受长按影响
- {
- key[i].key_tim++;//按键长按时,时间记录值每10ms加1
- if(key[i].key_std==1)//按键松开后,若按下时间小于500ms,则为单击
- {
- key[i].junk_std=0;//重新开始一轮按键判断
- if(key[i].key_tim<50)
- {
- key[i].single_flag=1;//单击
- }
- }
- if(key[i].key_tim==50)//若按下时间等于500ms,则为长按
- {
- key[i].long_flag=1; //长按
- }
- }break;
- }
- }
- }
- }
为了方便用csdn查看,将所有代码写在main.c文件中。读者可自行分文件编写。
特别感谢b站up主01Studio分享的教程
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "gpio.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
-
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- #include "stdbool.h"
- struct keys
- {
- bool key_std;//用于存储按键状态
- unsigned char junk_std;//用于记录按键按下后进入中断次数
- bool single_flag;//单击标志位
- unsigned int key_tim;//记录长按时间
- bool long_flag;//长按标志位
- };
- struct keys key[4];//创建结构体数组,分别对应4个按键
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM4)
- {
- key[0].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);//读取当前按键状态
- key[1].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);//读取当前按键状态
- key[2].key_std=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);//读取当前按键状态
- key[3].key_std=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);//读取当前按键状态
- for(unsigned char i=0;i<4;i++)//对4个按键进行判断
- {
- switch(key[i].junk_std)//按下按键后进入中断的次数并判断
- {
- case 0://当按键按下后,第一次不做任何操作,用于消抖10ms
- {
- if(key[i].key_std==0)
- {
- key[i].key_tim=0;//将长按时间记录值清零
- key[i].junk_std=1;
- }
- }break;
- case 1://第二次检测到按键按下后,将单击标志位single_flag设置为1
- {
- if(key[i].key_std==0)
- {
- key[i].junk_std=2;
- // key[i].single_flag=1;
- }
- }break;
- case 2://直到按键松开才将计算值设置为0,重新开始判断,这样单击不受长按影响
- {
- key[i].key_tim++;//按键长按时,时间记录值每10ms加1
- if(key[i].key_std==1)//按键松开后,若按下时间小于500ms,则为单击
- {
- key[i].junk_std=0;//重新开始一轮按键判断
- if(key[i].key_tim<50)
- {
- key[i].single_flag=1;//单击
- }
- }
- if(key[i].key_tim==50)//若按下时间等于500ms,则为长按
- {
- key[i].long_flag=1; //长按
- }
- }break;
- }
- }
- }
- }
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- void LED_Init(void)
- {
- HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//将PC口全部设置为高电平
- HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//打开锁存器,使锁存器输出为PC8-PC15的电平
- HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关闭锁存器,使锁存器输出不受PC8-PC15的电平影响
- }
- void LED(unsigned char dat)
- {
- unsigned int data;
- data=dat<<8;
- HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);//将PC口全部设置为高电平
- HAL_GPIO_WritePin(GPIOC,data,GPIO_PIN_RESET);//将对应的IO口设置为低电平,从而使LED点亮
- HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);//打开锁存器,使锁存器输出为PC8-PC15的电平
- HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);//关闭锁存器,使锁存器输出不受PC8-PC15的电平影响
-
- }
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
-
- /* USER CODE END 0 */
-
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
-
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
-
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM4_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim4);//开启定时器中断
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- // LED(2);
- while (1)
- {
- for(unsigned char a=0;a<4;a++)
- {
- if(key[a].single_flag==1)
- {
- key[a].single_flag=0;
- LED(a);
- }
- if(key[a].long_flag==1)
- {
- key[a].long_flag=0;
- LED((a<<2)+4);
- }
- }
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
-
- /** Configure the main internal regulator output voltage
- */
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
-
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
- RCC_OscInitStruct.PLL.PLLN = 20;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
-
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /* USER CODE BEGIN 4 */
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。