博客
关于我
一文了解串口打印
阅读量:398 次
发布时间:2019-03-05

本文共 3329 字,大约阅读时间需要 11 分钟。

之前的文章《》介绍了串口驱动,串口在嵌入式领域不仅是一个通讯接口,还是一种调试工具,其好用程度不亚于硬件仿真。有些环境不方便连接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__ */

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

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);}

打印函数

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;}

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

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

 

 

 IAR配置

 

 

 打印效果

 

 

 代码和工程已经开源

代码和Keil IAR工程开源地址:

 

02、IDE打印

有些时候,我们需要只是在办公室调试,芯片没有额外的串口提供我们调试,使用IDE打印也不失为一条好办法。

2.1、IAR

代码如下

printf("\r\n======================================================================");  printf("\r\n=               (C) COPYRIGHT 2020                                   =");  printf("\r\n=                                                                    =");  printf("\r\n=                ST207 USART_Printf                                  =");  printf("\r\n=                                                                    =");  printf("\r\n=                                           By Firefly               =");  printf("\r\n======================================================================");  printf("\r\n\r\n");

IAR打印效果

 

 配置方法,打开TerminalIO:进入调试->view->Terminal I/O

 

 

2.2、Keil

目前我还没有找到解决办法。网上可以找到勾选Use Simulator,但这是模拟调试的,并不是硬件调试,不符合我的要求。

欢迎大家分享自己的办法,在评论区告诉大家。

代码和IAR工程开源地址:

 

点击查看本文所在的专辑,

 

转载地址:http://rhqzz.baihongyu.com/

你可能感兴趣的文章
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>