首页 > 代码库 > STM32.printf

STM32.printf

printf("\r\n this is a usart printf demo \r\n");

Use Micro LIB 需要勾选这个库

将串口定义成 printf 函数

#include <stdio.h>//重定向C库函数printf到USART1int fputc(int ch, FILE *f){        /* 发送一个字节数据到USART1 */        USART_SendData(USART1, (uint8_t) ch);                /*等待发送完毕  */        while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);                    return (ch);}///重定向C库函数scanf到USART1int fgetc(FILE *f){        /* 等待串口1输入数据 */        while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);        return (int)USART_ReceiveData(USART1);}

 

或者

#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__ */

 


 

STM32.printf