增加以下配置到工程里即可。
注意fputc里重定向的对象。
还有使用微库的方法,见 http://blog.sina.com.cn/s/blog_b4ce638e0102w9fw.html
//printf reconf
#pragma import(__use_no_semihosting)
/******************************************************************************
*标准库需要的支持函数
******************************************************************************/
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 is typedef’ d in stdio.h. */
FILE __stdout;
/// <summary>
/// 定义_sys_exit()以避免使用半主机模式
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (unsigned char) ch);
while (!(USART2->SR & USART_FLAG_TXE));
return (ch);
}