作者:小枣年糕  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
// @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 USART

USART_SendData(USART1, (uint8_t) ch);

// Loop until the end of transmission

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

{}

return ch;

}

这一段函数就是把printf函数输出到串口,需要将fputc输出给串口,也就是重新定义。



下面就说一下使用printf需要做哪些配置。




有两种配置方法:



一、对工程属性进行配置,详细步骤如下



1、首先要在你的main 文件中 包含“stdio.h” (标准输入输出头文件)。



2、在main文件中重定义<fputc>函数 如下:



/**

* @brief Retargets the C library printf function to the USART.

* @param None

* @retval None printf 重定向到串口

*/

PUTCHAR_PROTOTYPE

{

/* Place your implementation of fputc here */

/* e.g. write a character to the USART */

USART_SendData(EVAL_COM1, (uint8_t) ch);



/* Loop until the end of transmission */

while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)

{}



return ch;

}


3、在工程属性的 “Target" -> "Code Generation" 选项中勾选 "Use MicroLIB"”



MicroLIB 是缺省C的备份库,关于它可以到网上查找详细资料。



至此完成配置,在工程中可以随意使用printf向串口发送数据了。

retarget.c 一般工程都会带有的重定向函数文件。