前言
按键,一般有单击和长按两种考法,我们已经将单击的写法掌握之后就要开始学习长按的写法。
KEY(long)
原理图,配置元素,配置步骤,生成工程这四步与【STM32G431RBTx】备战蓝桥杯嵌入式→基本模块→KEY→单击一致,我们只需要对上篇文章中的interrupt.c,interrupt.h,main.c文件修改即可。
1.测试代码
interrupt.h:
- #ifndef __INTERRUPT_H__
- #define __INTERRUPT_H__
- #include "main.h"
- #include "stdbool.h" //如果需要添加bool形就要加入这个头文件
- struct keys
- {
- unsigned char judge_sta;
- bool key_sta;
- bool single_flag;
- unsigned key_time; //按键持续按下的时间
- bool long_flag; //长按响应标志
- };
- #endif
复制代码
interrupt.c
- #include "interrupt.h"
- struct keys key[4] = {0, 0, 0, 0, 0};
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance == TIM3)
- {
- 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(unsigned char 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; //松开按键后让其进入case0,防止长按松开之后仍在响应
- if(key[i].key_time <= 70) //只有按下小于700ms才算一次单击
- {
- key[i].single_flag = 1;
- }
- }
- else
- {
- key[i].key_time++; //如果按下按键不松,按键时间开始增加
- if(key[i].key_time > 70) //一旦超过700ms,长按开始响应
- {
- key[i].long_flag = 1;
- }
- }
- }
- break;
- }
- }
- }
- }
复制代码
main.c中main函数部分:
- 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_TIM3_Init();
- /* USER CODE BEGIN 2 */
- LCD_Init();
- LCD_Clear(Black);
- LCD_SetBackColor(Black);
- LCD_SetTextColor(White);
- HAL_TIM_Base_Start_IT(&htim3);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(key[0].single_flag) //key0短按响应
- {
- LCD_DisplayStringLine(Line5, " key0down ");
- key[0].single_flag = 0;
- }
- if(key[0].long_flag) //key0长按响应(一直响应)
- {
- LCD_DisplayStringLine(Line5, " key0long ");
- key[0].long_flag = 0;
- }
- if(key[1].single_flag) //key1短按响应
- {
- j = 0;
- char text[30];
- sprintf(text, " key1down%d ", j);
- LCD_DisplayStringLine(Line6,(unsigned char *)text);
- key[1].single_flag = 0;
- }
- if(key[1].long_flag) //key1长按响应(一直响应)
- {
- j++;
- char text[30];
- sprintf(text, " key1long%d", j);
- LCD_DisplayStringLine(Line6,(unsigned char *)text);
- key[1].long_flag = 0;
- }
- }
- /* USER CODE END 3 */
- }
复制代码
2.测试效果
KEY(double)
由于蓝桥杯至今没有考过双击,所以这里只把代码放到这里,下面的代码是将单,双,长合在一起的,如果要原始工程可以私信给我。
interrupt.h:- #ifndef __INTERRUPT_H_
- #define __INTERRUPT_H_
- #include "main.h"
- #include "stdbool.h"
- struct keys
- {
- unsigned char judge_sta;
- bool key_sta;
- bool single_key_flag;
- bool long_flag;
- unsigned int key_time;
- bool double_click_timerEN;
- unsigned int double_click_time;
- bool double_key_flag;
- bool long_key_flag;
- };
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
- #endif
复制代码
interrupt.c:
- #include "interrupt.h"
- struct keys key[4] = {0, 0, 0};
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance == TIM3)
- {
- 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].key_time < 70)
- {
- if(key[i].double_click_timerEN == 0)
- {
- key[i].double_click_timerEN = 1;
- key[i].double_click_time = 0;
- }
- else
- {
- key[i].double_key_flag = 1;
- key[i].double_click_timerEN = 0;
- }
- key[i].judge_sta = 0;
- }
- else if(key[i].key_sta == 1 && key[i].key_time >= 70) key[i].judge_sta = 0;
- else
- {
- if(key[i].key_time >= 70) key[i].long_key_flag = 1;
- key[i].key_time++;
- }
- break;
- }
- }
- if(key[i].double_click_timerEN == 1)
- {
- key[i].double_click_time++;
- if(key[i].double_click_time >= 20)
- {
- key[i].single_key_flag = 1;
- key[i].double_click_timerEN = 0;
- }
- }
- }
- }
- }
复制代码
main.c:
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "led.h"
- #include "lcd.h"
- #include "stdio.h"
- #include "interrupt.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- extern struct keys key[];
- /* 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_TIM3_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
-
- HAL_TIM_Base_Start_IT(&htim3);
- LED_Disp(0x00); //这里是把灯全部灭掉
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(key[0].single_key_flag == 1)
- {
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
- key[0].single_key_flag = 0;
- }
- if(key[0].long_key_flag == 1)
- {
- LED_Disp(0xff);
- key[0].long_key_flag = 0;
- }
- if(key[0].double_key_flag == 1)
- {
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_15);
- key[0].double_key_flag = 0;
- }
- if(key[1].single_key_flag == 1)
- {
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);
- key[1].single_key_flag = 0;
- }
- if(key[1].long_key_flag == 1)
- {
- LED_Disp(0x00);
- key[1].long_key_flag = 0;
- }
- if(key[1].double_key_flag == 1)
- {
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_14);
- key[1].double_key_flag = 0;
- }
-
- }
- /* 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 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
led.h:
- #ifndef __LED_H__
- #define __LED_H__
- #include "main.h"
- void LED_Disp(unsigned char dsLED);
- #endif
复制代码
led.c- #include "led.h"
- void LED_Disp(unsigned char dsLED)
- {
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOC, dsLED << 8, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
- }
复制代码
————————————————
版权声明:火花页.
|