之前的文章《STM32 串口詳解》介紹了串口驅動,串口在嵌入式領域不僅是一個通訊介面,還是一種除錯工具,其好用程度不亞于硬體仿真,有些環境不方便連接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工程開源地址:
https://github.com/strongercjd/STM32F207VCT6
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工程開源地址:
https://github.com/strongercjd/STM32F207VCT6
點擊查看本文所在的專輯,STM32F207教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/266566.html
標籤:其他
上一篇:LCD的DIP是什么
下一篇:求助,這樣功能怎么實作?
