printf函数,包含在头文件stdio.h中
但是要正常使用它,必须重载一个字节输出函数。下面给段代码:
#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 */
LL_USART_ClearFlag_TC(USART2);
LL_USART_TransmitData8 (USART2ch);
while(0 == LL_USART_IsActiveFlag_TC(USART2)) {};
return ch;
}
不同的编译环境,字节输出函数名不同,有的叫__io_putchar,有的叫fputc。这里用宏来进行控制。
这段代码,我让字节通过串口2输出。以后调用printf函数,输出内容都是通过串口2送出的。
您也可以改为其它串口。如果愿意,您也可以在fputc函数里把它显示在LCD屏上(控制好显示坐标),或者别的输出方式。