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

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

[[351379]]

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

01硬件打印

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

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

#include < stdio.h >

兼容不同IDE的putchar重映射。

  1. #ifdef __GNUC__ 
  2.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf 
  3.      set to 'Yes') calls __io_putchar() */ 
  4.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) 
  5. #else 
  6.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) 
  7. #endif /* __GNUC__ */ 

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

  1. void UART_Init(void) 
  2.   USART_InitTypeDef USART_InitStructure; 
  3.   GPIO_InitTypeDef GPIO_InitStructure; 
  4.  
  5.   /* Enable GPIO clock */ 
  6.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
  7.   /* Enable UART1 clock */ 
  8.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
  9.   /* Connect PXx to USARTx_Tx*/ 
  10.   GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1); 
  11.    
  12.   /* Connect PXx to USARTx_Rx*/ 
  13.   GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1); 
  14.    
  15.   /* Configure USART Tx as alternate function  */ 
  16.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  17.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  18.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  19.    
  20.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
  21.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  22.   GPIO_Init(GPIOA, &GPIO_InitStructure); 
  23.    
  24.   /* Configure USART Rx as alternate function  */ 
  25.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  26.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
  27.   GPIO_Init(GPIOA, &GPIO_InitStructure); 
  28.    
  29.   USART_InitStructure.USART_BaudRate = 115200; 
  30.   USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
  31.   USART_InitStructure.USART_StopBits = USART_StopBits_1; 
  32.   USART_InitStructure.USART_Parity = USART_Parity_No; 
  33.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
  34.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 
  35.    
  36.   /* USART configuration */ 
  37.   USART_Init(USART1, &USART_InitStructure); 
  38.    
  39.   /* Enable USART */ 
  40.   USART_Cmd(USART1, ENABLE); 

打印函数

  1. PUTCHAR_PROTOTYPE 
  2.   /* Place your implementation of fputc here */ 
  3.   /* e.g. write a character to the USART */ 
  4.   USART_SendData(USART1, (uint8_t) ch); 
  5.  
  6.   /* Loop until the end of transmission */ 
  7.   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) 
  8.   {} 
  9.  
  10.   return ch; 

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

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

 

IAR配置

 

打印效果

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

 

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

2022-08-02 10:01:34

Import语句ES模块

2011-05-07 15:30:27

喷墨打印机技术优缺点

2021-05-31 10:22:09

Go语言代码

2020-08-27 07:34:50

Zookeeper数据结构

2024-04-07 00:00:00

ESlint命令变量

2023-12-20 08:23:53

NIO组件非阻塞

2023-04-26 10:21:04

2023-12-12 08:41:01

2023-11-20 08:18:49

Netty服务器

2023-04-26 15:43:24

容器编排容器编排工具

2020-02-20 08:30:49

OSPF网络协议路由协议

2022-11-04 14:16:05

2022-12-02 14:12:52

新能源汽车海尔

2021-04-20 23:16:06

SparkSQL语法

2021-10-14 06:52:47

算法校验码结构

2023-03-21 07:39:51

CentOS挂载硬盘

2024-01-15 12:16:37

2022-11-28 00:04:17

2022-09-29 15:32:58

云计算计算模式

2023-01-13 17:02:10

操作系统鸿蒙
点赞
收藏

51CTO技术栈公众号