STM32
直播中

刘慧

8年用户 1844经验值
私信 关注
[问答]

STM32调试之串口发送信息

如何用printf()函数实现串口打印信息呢?

回帖(1)

成贯征

2021-10-21 15:26:18
  在使用STM32调试时,经常使用串口发送信息,为了方便调试与串口发送信息,用printf()函数实现通过串口打印信息。
  1.添加包含printf()函数的头文件:#include “stdio.h”
  1-1,在Uart1.c文件中要添加#include “stdio.h”
  
  1-2,在main.c文件中也要添加#include “stdio.h”
  
  2.重写 stdio.h 头文件中的 int fputc(int ch, FILE *f) 函数
  // 发送数据int fputc(int ch, FILE *f)
  {
  USART_SendData(USART1, (unsigned char) ch);
  // USART1 ???? USART2 ?
  while (!(USART1-》SR & USART_FLAG_TXE));
  return (ch);
  }
  // 接收数据int GetKey (void)
  {
  while (!(USART1-》SR & USART_FLAG_RXNE));
  return ((int)(USART1-》DR & 0x1FF));
  }
  3,使用 printf 函数输出
  (1)、打印字符串:printf(“hello world!n”);
  (2)、打印数字或变量:printf(“a = %dn”, a);
举报

更多回帖

发帖
×
20
完善资料,
赚取积分