整个过程
main.c中的代码
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "./LED/bsp_led.h"
#include "./KEY/bsp_key.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* Initialize all configured peripherals */MX_GPIO_Init();/* USER CODE BEGIN 2 */LED_GPIO_Init();KEY_GPIO_Init();/* USER CODE END 2 */
/* USER CODE BEGIN WHILE */while (1){
/* USER CODE END WHILE */if(Key_Scan(GPIOA, GPIO_PIN_0) == KEY_ON){
LED_B_Toggle;}if(Key_Scan(GPIOC, GPIO_PIN_13) == KEY_ON){
LED_G_Toggle;}/* USER CODE BEGIN 3 */}
bsp.key.c
#include "./KEY/bsp_key.h"
#include "stm32f1xx.h"void KEY_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();GPIO_InitTypeDef KEY_GPIO_Init;KEY_GPIO_Init.Mode = GPIO_MODE_INPUT;KEY_GPIO_Init.Pin = GPIO_PIN_0 ;KEY_GPIO_Init.Pull = GPIO_NOPULL;KEY_GPIO_Init.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOA,&KEY_GPIO_Init);KEY_GPIO_Init.Pin = GPIO_PIN_13 ;HAL_GPIO_Init(GPIOC,&KEY_GPIO_Init);
}
uint8_t Key_Scan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin){
if(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_SET){
while(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_SET);return KEY_ON;}else{
return KEY_OFF;}}
bsp.key.h
#ifndef _BSP_KEY_H_
#define _BSP_KEY_H_
#define KEY_ON 1
#define KEY_OFF 0
#include "stm32f1xx.h"/* 初试化KEY对应的引脚 */
void KEY_GPIO_Init(void);uint8_t Key_Scan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);#endif