一文了解串口打印,你知道吗?

商务办公
串口在嵌入式领域不仅是一个通讯接口,还是一种调试工具,其好用程度不亚于硬件仿真。

[[351379]]

串口在嵌入式领域不仅是一个通讯接口,还是一种调试工具,其好用程度不亚于硬件仿真。有些环境不方便连接Jlink进行硬件仿真,或者并不是必现的问题,我们需要定位出现问题的地方,可以选择保存log的方式,但是需要后续读取,且受到Flash大小的限制,如果可以放置一台计算机到现场,使用串口打印无疑是最好的办法,在C语言中 printf函数输出各种类型的数据,使用格式控制输出各种长度的字符,甚至输出各种各样的图案,需要将串口重定向到printf函数。

01硬件打印

在STM32的应用中,我们常常对printf进行重定向的方式来把打印信息printf到我们的串口助手。在MDK环境中,我们常常使用MicroLIB+fputc的方式实现串口打印功能,即:串口重映射

代码中记得添加一下头文件

#include < stdio.h >

兼容不同IDE的putchar重映射。

#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__ */ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

当然也需要配置下串口,不需要配置中断。

void UART_Init(void) 

  USART_InitTypeDef USART_InitStructure; 
  GPIO_InitTypeDef GPIO_InitStructure; 
 
  /* Enable GPIO clock */ 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
  /* Enable UART1 clock */ 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
  /* Connect PXx to USARTx_Tx*/ 
  GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1); 
   
  /* Connect PXx to USARTx_Rx*/ 
  GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1); 
   
  /* Configure USART Tx as alternate function  */ 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
   
  /* Configure USART Rx as alternate function  */ 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  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_Rx | USART_Mode_Tx; 
   
  /* USART configuration */ 
  USART_Init(USART1, &USART_InitStructure); 
   
  /* Enable USART */ 
  USART_Cmd(USART1, ENABLE); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

打印函数

PUTCHAR_PROTOTYPE 

  /* Place your implementation of fputc here */ 
  /* e.g. write a character to the USART */ 
  USART_SendData(USART1, (uint8_t) ch); 
 
  /* Loop until the end of transmission */ 
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) 
  {} 
 
  return ch; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

不同的IDE也要对应的的配置。

Keil配置,需要勾选MicroLIB选项。

 

IAR配置

 

打印效果

本文转载自微信公众号「知晓编程」,可以通过以下二维码关注。转载本文请联系知晓编程公众号。

 

责任编辑:武晓燕 来源: 知晓编程
相关推荐

2025-01-14 11:07:30

JenkinsWAR目录

2022-08-02 10:01:34

Import语句ES模块

2011-05-07 15:30:27

喷墨打印机技术优缺点

2020-08-27 07:34:50

Zookeeper数据结构

2023-04-26 10:21:04

2023-12-20 08:23:53

NIO组件非阻塞

2024-04-30 09:02:48

2023-12-12 08:41:01

2021-05-31 10:22:09

Go语言代码

2022-06-08 08:11:56

威胁建模网络安全网络攻击

2023-11-06 08:16:19

APM系统运维

2022-02-25 07:34:36

MQTT协议RabbitMQ

2022-11-11 19:09:13

架构

2023-11-20 08:18:49

Netty服务器

2023-04-26 15:43:24

容器编排容器编排工具

2024-04-07 00:00:00

ESlint命令变量

2024-05-28 09:12:10

2022-03-10 08:25:27

JavaScrip变量作用域

2019-12-12 09:23:29

Hello World操作系统函数库

2017-10-16 13:45:04

点赞
收藏

51CTO技术栈公众号