STM32在IAR中如何使用printf函数

STM32使用printf函数给串口打印信息的执行步骤为: 
1.重定向printf函数  

给uart.c文件中增加如下函数:

int fputc(int ch, FILE *f)
{
  USART_SendData(USART2, (unsigned char) ch);// USART1 可以换成 USART2 等
  
  while (!(USART2->SR & USART_FLAG_TXE));
  
  return (ch);

}

2.增加头文件stdio

#include "stdio.h"

3.添加宏 

在IAR中使用printf应在Options->C/C++Compler->Preprocessor中的Defined中添加_DLIB_FILE_DESCRIPTOR。



以前在变成调试的时候,一直不知道\r和\n有什么区别,觉得差不多,今天使用的时候就被坑了一把。 

这里首先要理解:\n是换行,就是光标移动到下一行,\r是讲光标移动到本行的开始处。

printf("hello %d\r\n",NoPressKeyTime);这样就可以在串口软件上自动换行了。


猜你喜欢

转载自blog.csdn.net/wangkeyen/article/details/80536400