IT资讯

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

作者:admin 2021-04-11 我要评论

串口在嵌入式领域不仅是一个通讯接口,还是一种调试工具,其好用程度不亚于硬件仿真。有些环境不方便连接Jlink进行硬件仿真,或者并不是必现的问题,我们需要定...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

串口在嵌入式领域不仅是一个通讯接口,还是一种调试工具,其好用程度不亚于硬件仿真。有些环境不方便连接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配置

打印效果

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


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/zWZpuRkIigNQjKeDX6hOaA

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 20款小众宝藏APP,工作、生活全不误,

    20款小众宝藏APP,工作、生活全不误,

  • 谷歌开发 MicroDroid,用于虚拟机的精

    谷歌开发 MicroDroid,用于虚拟机的精

  • 微信 8.0 专属红包灰度测试:可指定发

    微信 8.0 专属红包灰度测试:可指定发

  • 如何隐藏你的热更新 Bundle 文件?

    如何隐藏你的热更新 Bundle 文件?

腾讯云代理商
海外云服务器