STM32 printf的重定义

想在while(1)里面用printf将数据输出到串口,因此对prinf函数的fputc函数进行重定义,如下:
int fputc(int ch, FILE *f)
{
#ifdef DBG_ITM
/* 将Printf内容发往ITM激励寄存器端口 */
if (DEMCR & TRCENA) {
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
#else
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
#endif
return (ch);
}
但是仅仅如此定义的话,printf的内容还是输出不到串口,恳请指教是否还有另外一些需要配置的?如果能帮助解决这问题,定高分奖赏,谢谢!

STM32 对printf的重定义参考代码如下:
#define STDIO_COM USARTx

int fputc(int ch, FILE *f) //将字符ch写到文件指针fp所指向的文件的当前写指针的位置
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(STDIO_COM, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(STDIO_COM, USART_FLAG_TC) == RESET)
{}

return ch; //返回结果
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-30
直接给你调试好的代码,再把微库给勾选上(microLIB)。这代码复制到你的串口驱动里 你就可以直接用printf了
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;

_sys_exit(int x)
{
x = x;
}
#endif

int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);

while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}

return ch;
}本回答被提问者和网友采纳
第2个回答  2019-01-18

你这个应该通过ITM来传输数据吧?可以参考这个链接:网页链接

第3个回答  2013-08-31
外设时钟开了么,引脚定义了么
相似回答