配置STM32CubeMX
配置KEY
配置USART1
配置RCC
Project Manager
Toolchain 选择 MDK-ARM
Code Generator 配置如下
GENERATE CODE 即可
配置Keil5
魔术棒配置
– Target – 勾选 Use MicroLIB
– Debug – Flash Download – 勾选Reset and Run
基础代码
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch, FILE *f)
{HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);return ch;
}uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin){if (HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET ) {while (HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET);return RESET;} elsereturn SET;}
/* USER CODE END 0 */
/* Infinite loop *//* USER CODE BEGIN WHILE */while (1){if(Key_Scan(KEY_GPIO_Port, KEY_Pin) == RESET){printf("key pressed\r\n");}/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}
编译,下载程序,就能看到按下按钮串口打印输出的现象了