HAL库使用printf

用stm32cubemx配置好串口功能之后,想要使用printf函数进行打印输出的话,还需要自己添加一个重定向函数。

 1 #ifdef __GNUC__  
 2 /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf 
 3 set to 'Yes') calls __io_putchar() */  
 4 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)  
 5 #else  
 6 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)   
 7 #endif /* __GNUC__ */ 
 8 
 9 PUTCHAR_PROTOTYPE
10 {
11     /* Place your implementation of fputc here */
12     /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
13     HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); 
14 
15     return ch;
16 }

随便找个位置定义了就可以正常使用printf函数了。

posted @ 2021-04-18 14:52  Qianm  阅读(1185)  评论(0)    收藏  举报