Toolchain 选择 MDK-ARM
Code Generator 配置如下
GENERATE CODE 即可
– 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;
} else
return 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 */
}
编译,下载程序,就能看到按下按钮串口打印输出的现象了