按键检测,就是按键作为输入,一般51的做法是IO口接上拉按键控制接地.
至于stm32cube中不需接上拉电阻,内置上啦就成了.
端口设置为GPIO_Input,上拉模式.
记得不是GPIO_OUTPUT模式啊,那个可不行
至于定时器设置为10ms定时就可以了,有人说设置为20ms最好,自己看了
我用定时器3,设置定时时间10ms
定时完成回调函数里面就定义一个标志,表示定时时间到
主程序中处理按键检测
我这里演示了3个按键的检测,用灯指示输出
随便写了个程序.
#include "stm32f4xx_hal.h"#include "tim.h"#include "gpio.h"/ Private variables ---------------------------------------------------------// USER CODE BEGIN 0 /uint8_t update_flag = 0;uint8_t key[2],c_time,key_v,key_s=0x07;uint8_t get_value(void);void key_scan(void);/ USER CODE END 0 // Private function prototypes -----------------------------------------------/void SystemClock_Config(void);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(); / Configure the system clock / SystemClock_Config(); / System interrupt init/ / Sets the priority grouping field / HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); / Initialize all configured peripherals / MX_GPIO_Init(); MX_TIM3_Init(); / USER CODE BEGIN 2 /// uint8_t ReadData,Trg,Cont;// uint8_t value;// uint8_t value_pre = 0x07; uint8_t key_flag; HAL_TIM_Base_Start_IT (&htim3); / USER CODE END 2 / / USER CODE BEGIN 3 / / Infinite loop / while (1) {// if(update_flag)// {c_time++ ;update_flag=0;}// if(c_time >=100) {HAL_GPIO_TogglePin( GPIOF, GPIO_PIN_6 );c_time= 0;}// if(update_flag) { key_scan (); update_flag = 0; } if(key_v != key_s) { key_s = key_v; key_flag = 1; } if(key_flag) { key_flag = 0;// HAL_GPIO_TogglePin( GPIOF, GPIO_PIN_6 ); switch(key_s) { case 0x00 : HAL_GPIO_TogglePin( GPIOB, GPIO_PIN_11 ); HAL_GPIO_TogglePin( GPIOB, GPIO_PIN_12 );break; case 0x01 : HAL_GPIO_TogglePin( GPIOB, GPIO_PIN_12 ); break; case 0x03 : HAL_GPIO_TogglePin( GPIOB, GPIO_PIN_11 ); break; case 0x07 : HAL_GPIO_TogglePin( GPIOF, GPIO_PIN_6 ); break; default : break; } } } / USER CODE END 3 /}/** System Clock Configuration*/void SystemClock_Config(void){ RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; __PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); 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 = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = 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_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);}/ USER CODE BEGIN 4 /void key_scan(void){ key[ c_time ] = get_value (); c_time ++; if(c_time <= 1) return ; c_time = 0; if(key[1]==key[0]) key_v = key[0];}uint8_t get_value(void){ uint8_t PIN_value; PIN_value = (HAL_GPIO_ReadPin (GPIOB,GPIO_PIN_13)<<2) + (HAL_GPIO_ReadPin (GPIOB,GPIO_PIN_14)<<1) + HAL_GPIO_ReadPin (GPIOB,GPIO_PIN_15);// PIN_value = HAL_GPIO_ReadPin (GPIOB,GPIO_PIN_13); return PIN_value;}void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){ update_flag = 1; }/ USER CODE END 4 /
|