关于STM32中printf函数的重定向问题

printf函数一般是打印到终端的,stm32芯片调试中经常需要用到串口来打印调试信息,那能不能用串口实现类似windows的Console中的printf呢? 答案是肯定的,那就是printf函数的重定向。 使用KEIL5对stm32的printf函数进行重定向,有两种方法:一种是使用微库,另一种是不使用微库。 方法1--使用微库: 1、使用微库,在KEIL5中点击options for target,在Target标签下有个Use MicroLIB---勾选,使用微库。 2、在串口文件中添加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include "stdio.h" #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE* f) #endif /* __GNUC__ */ #ifdef __cplusplus extern "C" { #endif //__cplusplus PUTCHAR_PROTOTYPE { while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) ; USART_SendData(USART2, (uint8_t)ch); return (ch); } #ifdef __cplusplus } #endif //__cplusplus 修改相应的串口号,初始化,就能使用printf了。 方法2--不使用微库(那么就要强调不使用半主机(no semihosting)模式) 1、使用微库(平台式keil-MDK),点击options for target,在Target标签下有个Use MicroLIB---取消勾选,不使用微库。 2、在串口文件中添加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE* f) #endif /* __GNUC__ */ #ifdef __cplusplus extern "C" { #endif //加入以下代码,支持printf函数,而不需要选择use MicroLIB #pragma import(__use_no_semihosting) //定义_sys_exit()以避免使用半主机模式 void _sys_exit(int x) { x = x; } void _ttywrch(int x) { x = x; } //标准库需要的支持函数 struct __FILE { int handle; }; FILE __stdout; //重定义fputc函数 PUTCHAR_PROTOTYPE { while((USART2->SR & 0X40) == 0);//循环发送,知道发送完毕 USART2->DR = (u8)ch; return ch; } #ifdef __cplusplus } #endif //__cplusplus 当然,头文件#include "stdio.h"别忘了加上。同样的,修改相应的串口号,初始化,就能使用printf了。 如果编译的时候出现FILE __stdout;编译不过,可以打开stdio.h文件,将typedef struct __FILE FILE; 修改为 1 2 3 4 typedef struct __FILE { }FILE;https://www.cnblogs.com/jasonchan1202/p/9673548.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信