STM32cubeMX学习记录2-USART1-printf

一、新建工程-选择单片机型号(参考上篇文章LED)

二、设置RCC、SYS、USART1

三、检查时钟

四、引脚设定(不用修改)

五、项目名称,存储地址,MDK等设置

六、生成项目

七、生成后打开程序,编译一次,打开options,勾选上use Micro lib

八、main.c文件中中添加语句printf(" this is a usart1 test! ");和printf(" test is ok! "); HAL_Delay(1000);注意添加位置。

九、最重要的一步,在uart.c文件中要添加printf语句,否则不能串口不能打印

#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to Yes) calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ /** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */ HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);

return ch; }

最重要!在usart.h文件中,要引用头文件#include <stdio.h>,否则还是会报错。

十、编译下载打开串口,注意串口要选择115200。

经验分享 程序员 微信小程序 职场和发展