在使用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 = %d\n", a);
参考:https://blog.csdn.net/qq_26904271/article/details/80113740
https://blog.csdn.net/u014449366/article/details/52718429
http://c.biancheng.net/view/159.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)