软件知识储备01_STM32串口输出重定向的几种方式

环境配置说明:

PC:win10专业版 版本号2009
Keil:uversiobV5.21a
STM32CUBEMX:V6.5.0
测试用开发板:STM32F407VET6核心板
F4固件库:STM32Cube FW_F4 V1.27.0

重要说明

如果要使用printf函数来打印串口数据,1、串口配置好;2、keil需要设置使用MicroLIB;3、在“usart.h”中需要包含“#include “stdio.h””(注:有的串口函数可能是“uart.h”,同理)如下图所示。这个是大前提,如果这没设置,后面的配置就没用了。
在这里插入图片描述
然后需要在“USART.h”包含stdio.h头文件如下图
在这里插入图片描述

方式一:在“USART.C”添加下列这段代码即可

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */

  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);

  return ch;
}

方式二:在“USART.C”添加下列这段代码即可

int fputc(int ch, FILE *fp);
int fgetc(FILE *fp);	

int fputc(int ch, FILE *fp)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}
 
int fgetc(FILE *fp)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
  return ch;
}

其他方式:这个遇到了再补上来

如何修改串口

上面两段代码,都只要修改这句和就可以了
*HAL_UART_Transmit(&huart1, (uint8_t )&ch, 1, 0xffff);
将huart1换成你设置的串口就可以了。
另外需要说明的是stdio.h头文件要放在“USART.H”头文件中,不然其他地方使用会报警告甚至出错。

2023年7月29日更新
如果上面的代码添加到程序中去之后要报错,如下图所示,应该检查确认当前串口是UART还是USART;
在这里插入图片描述
红框圈出来的报错就是因为我当前的串口使用的是USART,所以改为红框下面的那句就行了,修改后如下所示:

在这里插入图片描述
确认是UART还是USART的方法很多,这里最简单的可以看这里红框圈出来这句话:
在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
STM32 HAL中,可以通过重定向串口输出来实现打印功能。具体步骤如下: 1. 首先,在工程中打开stm32f4xx_hal_uart.c文件,找到`HAL_UART_Transmit`函数。 2. 在该函数中,找到以下代码: ```c /* Send the string character by character */ while (*pData != '\0') { /* Wait for the UART Transmit Data Register to be empty */ while (!__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE)) { } /* Transmit one byte */ huart->Instance->DR = (uint8_t)(*pData & 0xFF); pData++; } ``` 将其替换为以下代码: ```c /* Send the string character by character */ while (*pData != '\0') { /* Wait for the UART Transmit Data Register to be empty */ while (!__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE)) { } /* Transmit one byte */ ITM_SendChar(*pData); pData++; } ``` 3. 接下来,在main.c文件中添加以下代码,以重定向printf函数: ```c #ifdef __GNUC__ /* With GCC, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } ``` 4. 最后,在main函数中添加以下代码,以启用重定向功能: ```c /* Enable USART2 interrupt */ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* Enable ITM printf redirection */ ITM_Init(); /* Redirect STDOUT to USART2 */ stdout = &huart2; ``` 通过以上步骤,你就可以在STM32 HAL中实现串口输出重定向了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mi_Story

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值