HAL库下使用printf
1.包含头文件
#include "stdio.h"
2.重定义串口
int fputc(int ch, FILE *f) { while((USART1->SR & 0X40) == 0); //循环发送,直到发送完毕 USART1->DR = (uint8_t) ch; return ch; }
3.如果只完成前两步,使用仿真器可以运行,程序自己运行不起来。还需加入以下代码
#pragma import(__use_no_semihosting) void _sys_exit(int x) { x = x; } struct __FILE { int handle; /* Whatever you require here. If the only file you are using is */ /* standard output using printf() for debugging, no file handling */ /* is required. */ }; FILE __stdout; /* FILE is typedef’ d in stdio.h. */
经此设置可以避免使用LIB库