蓝色系网站首页,网站设计模板代码,wordpress和dz哪个好,安装wordpress前出现500 错误1、电路图 将4个按键的引脚设置为input#xff0c;并将初始状态设置为Pull-up#xff08;上拉输入#xff09; 为解决按键抖动的问题#xff0c;我们使用定时器中断进行消抖
打开TIM3时钟并设置参数#xff0c;中断间隔10ms#xff0c;当计数达到10000时溢出。80M/80/10…1、电路图 将4个按键的引脚设置为input并将初始状态设置为Pull-up上拉输入 为解决按键抖动的问题我们使用定时器中断进行消抖
打开TIM3时钟并设置参数中断间隔10ms当计数达到10000时溢出。80M/80/100001001/1000.01s10ms 1、Prescaler (PSC - 16 bits value): 预分频器用于分频器计数器时钟。设置为 80-1 表示时钟频率将被分频 80。 2、Counter Mode: 计数器模式这里设置为 Up意味着计数器将从 0 开始向上计数直到达到自动重装载寄存器的值。 3、Dithering: 抖动功能用于减少电磁干扰EMI。这里设置为 Disable表示禁用抖动。 4、Counter Period (AutoReload Register ...): 计数器周期即自动重装载寄存器的值。设置为 10000-1 表示计数器将从 0 计数到 9999然后重置为 0。 5、Internal Clock Division (CKD): 内部时钟分频这里设置为 No Division表示内部时钟没有被进一步分频。 6、auto-reload preload: 自动重装载预加载这里设置为 Disable表示在更新事件后立即加载新的周期值而不是在下一个更新事件之前。 7、Master/Slave Mode (MSM bit): 主/从模式这里设置为 Disable表示定时器工作在独立模式不作为从属定时器。 8、Trigger Event Selection TRGO: 触发事件选择这里设置为 Reset (UG bit from TIMx_EGR)意味着当更新事件Update Event发生时TRGO触发输出将被重置。 打开定时器中断 2、代码
2.1 单击长按
1internet.c
#include interrupt.hstruct keys key[4]{0};void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim-InstanceTIM3){key[0].key_staHAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[1].key_staHAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[2].key_staHAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[3].key_staHAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);for(uchar i0;i4;i) //遍历4个按键{//break跳出switch循环后再进行4次for循环循环结束后在10ms后中断再进入循环。消抖用的一般都是延时但该程序已将定时器配置为10mswitch(key[i].judge_sta){case 0:{if(key[i].key_sta0) //如果key[i].key_sta0按键按下但不能肯定进入第二步{key[i].judge_sta1;key[i].key_time0;}break; }case 1:{if(key[i].key_sta0) //如果10ms后判断还是0则确认是按下{key[i].judge_sta2; //进入第三步判断松手}else{key[i].judge_sta0;}break; }case 2:{if(key[i].key_sta1) //如果是1则说明按键被松开{key[i].judge_sta0; //回到初始状态if(key[i].key_time70){key[i].single_flag1;//标志位置1}}else{key[i].key_time;if(key[i].key_time70) //按键按下超过700ms则判断为长按因为10ms执行一次所以设置为70{key[i].long_flag1;}} }break;}}}
}
2internet.h
#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_#include main.h
#include stdbool.hstruct keys
{uchar judge_sta;bool key_sta;bool single_flag;bool long_flag;uint key_time;
};void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
#endif
3main.c
/* 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 */
#include lcd.h
#include interrupt.h
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
void key_pro(void);
extern struct keys key[4];/* 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 *//* 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 */HAL_TIM_Base_Start_IT(htim3);LCD_Init();//LCD ij ʼ LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){key_pro(); /* 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 */
void key_pro(void)
{if(key[0].single_flag1){LCD_DisplayStringLine(Line0, (uchar *)single_flag );key[0].single_flag0;}if(key[0].long_flag1){LCD_DisplayStringLine(Line0, (uchar *)long_flag );key[0].long_flag0;}if(key[0].double_flag1){LCD_DisplayStringLine(Line0, (uchar *)double_flag );key[0].double_flag0;}
}
/* 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 */2.2 单击长按双击
1internet.c
#include interrupt.hstruct keys key[4]{0};void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim-InstanceTIM3){key[0].key_staHAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[1].key_staHAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[2].key_staHAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[3].key_staHAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);for(int i0;i4;i) //遍历4个按键{switch(key[i].judge_sta){case 0: //按下检测{if(key[i].key_sta0) {key[i].judge_sta1;key[i].key_time0;}}break;case 1: //消抖检测{if(key[i].key_sta0) //如果按键按下{key[i].judge_sta2; }else{key[i].judge_sta0;}}break;case 2: //按键操作检测{if(key[i].key_sta1 key[i].key_time70) //如果按键松手并且没有长按{if(key[i].double_click_en0) //按键第一次按下{key[i].double_click_en1; key[i].double_click_time0; //松手时间清零准备计时}else //click_en1按键第二次按下{key[i].double_flag1; key[i].double_click_en0; //click_en清零}key[i].judge_sta0;}else if(key[i].key_sta1 key[i].key_time70)//如果按键松手并且有长按不执行操作{key[i].judge_sta0;}else //如果按键没有松手判断是否为长按{if(key[i].key_time70)//按下700ms为长按{key[i].long_flag1;}key[i].key_time; }}break;}if(key[i].double_click_en1) //按键第一次按下后{key[i].double_click_time;if(key[i].double_click_time30)//双击时间间隔为300ms{key[i].single_flag1;key[i].double_click_en0;}}}}
}
2internet.h
#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_#include main.h
#include stdbool.hstruct keys
{uchar judge_sta;bool key_sta;bool single_flag;bool double_flag;bool long_flag;uint key_time;
uint double_click_time;
uint double_click_en;
};void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
#endif
3main.c
/* 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 */
#include lcd.h
#include interrupt.h
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
void key_pro(void);extern struct keys key[4];/* 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 *//* 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 */HAL_TIM_Base_Start_IT(htim3);LCD_Init();//LCD ij ʼ LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){key_pro(); /* 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 */
void key_pro(void)
{if(key[0].single_flag1){LCD_DisplayStringLine(Line0, (uchar *)single_flag );key[0].single_flag0;}if(key[0].long_flag1){LCD_DisplayStringLine(Line0, (uchar *)long_flag );key[0].long_flag0;}if(key[0].double_flag1){LCD_DisplayStringLine(Line0, (uchar *)double_flag );key[0].double_flag0;}
}
/* 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 */