stm32 printf 串口输出

在使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值