STM32使用串口重定向系统printf函数输出时出现一初始化或使用printf函数系统卡死的原因及解决办法

在STM32开发当中,会经常使用系统自带的printf函数来查看调试输出,通常是将系统的printf输出内容发往串口,具体实现如下:

#include <stdio.h> //定义标准库头文件
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
  USART_SendData(USART1, (unsigned char) ch);
  while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
  return (ch);
}

上面是使用了串口1 作为printf函数的输出,如果只是查看调试输出,只需要初始化部分功能:

void USART1_Init(void)
{
  USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO , ENABLE);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  // Configure the USART1_Rx as input floating
  GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING ;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Tx;
  
  /* Configure the USARTx */ 
  USART_Init(USART1, &USART_InitStructure);
  /* Enable the USARTx */
  USART_Cmd(USART1, ENABLE);
}

设置为输出 115200波特率 然后在主程序中调用

int main (void){//主程序
	USART1_Init();
	printf("USART1_Init OK");
	while(1);
}

然后编译
在这里插入图片描述
0错误0警告
马上下载到板子上,结果等半天串口输出还是没有,后面往程序前后加入了LED灯的状态改变,也是毫无变化,可以确定程序彻底死了。。。
然后找了半天错误才发现
在这里插入图片描述
是这个奥东西没选 导致引用了标准库的printf函数。。。
再次编译
在这里插入图片描述
结果出来的程序大小。。。小了接近一倍。。。
下载运行就一切OK了

以上编译软件:KEIL MDK525
MCU:STM32F103C8T6

使用STM32printf函数,可以通过重定向来将其输出重定向串口或其他输出设备上。这样可以方便地在开发过程中通过串口输出调试信息。 首先,需要在代码中定义一个文件描述符,并重写该文件描述符的_write函数。在这个函数中,可以将要输出的字符通过串口发送出去。 以下是一个使用USART1作为串口输出的示例代码: ```c #include "stdio.h" // 重写文件描述符的 _write 函数 int _write(int file, char *ptr, int len) { for (int i = 0; i < len; i++) { // 将字符发送到串口 USART_SendData(USART1, (uint8_t) *ptr++); // 等待发送完毕 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } return len; } int main(void) { // 初始化串口 USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); // 使用printf输出 printf("Hello, world!\n"); while (1) { // 其他操作 } } ``` 在这个例子中,我们将USART1作为串口输出设备,并将printf函数输出重定向到USART1。需要注意的是,在使用重定向后,printf函数会比较耗费资源,因此在实际应用中要谨慎使用
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值