自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

商務(wù)辦公
串口在嵌入式領(lǐng)域不僅是一個(gè)通訊接口,還是一種調(diào)試工具,其好用程度不亞于硬件仿真。

[[351379]]

串口在嵌入式領(lǐng)域不僅是一個(gè)通訊接口,還是一種調(diào)試工具,其好用程度不亞于硬件仿真。有些環(huán)境不方便連接Jlink進(jìn)行硬件仿真,或者并不是必現(xiàn)的問題,我們需要定位出現(xiàn)問題的地方,可以選擇保存log的方式,但是需要后續(xù)讀取,且受到Flash大小的限制,如果可以放置一臺計(jì)算機(jī)到現(xiàn)場,使用串口打印無疑是最好的辦法,在C語言中 printf函數(shù)輸出各種類型的數(shù)據(jù),使用格式控制輸出各種長度的字符,甚至輸出各種各樣的圖案,需要將串口重定向到printf函數(shù)。

01硬件打印

在STM32的應(yīng)用中,我們常常對printf進(jìn)行重定向的方式來把打印信息printf到我們的串口助手。在MDK環(huán)境中,我們常常使用MicroLIB+fputc的方式實(shí)現(xiàn)串口打印功能,即:串口重映射

代碼中記得添加一下頭文件

#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__ */ 

當(dāng)然也需要配置下串口,不需要配置中斷。

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

打印函數(shù)

  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也要對應(yīng)的的配置。

Keil配置,需要勾選MicroLIB選項(xiàng)。

 

IAR配置

 

打印效果

本文轉(zhuǎn)載自微信公眾號「知曉編程」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系知曉編程公眾號。

 

責(zé)任編輯:武曉燕 來源: 知曉編程
相關(guān)推薦

2025-01-14 11:07:30

JenkinsWAR目錄

2022-08-02 10:01:34

Import語句ES模塊

2011-05-07 15:30:27

噴墨打印機(jī)技術(shù)優(yōu)缺點(diǎn)

2020-08-27 07:34:50

Zookeeper數(shù)據(jù)結(jié)構(gòu)

2021-05-31 10:22:09

Go語言代碼

2024-05-28 09:12:10

2024-04-07 00:00:00

ESlint命令變量

2023-12-12 08:41:01

2023-12-20 08:23:53

NIO組件非阻塞

2024-04-30 09:02:48

2023-04-26 10:21:04

2023-11-20 08:18:49

Netty服務(wù)器

2023-04-26 15:43:24

容器編排容器編排工具

2022-02-25 07:34:36

MQTT協(xié)議RabbitMQ

2023-11-06 08:16:19

APM系統(tǒng)運(yùn)維

2022-06-08 08:11:56

威脅建模網(wǎng)絡(luò)安全網(wǎng)絡(luò)攻擊

2022-11-11 19:09:13

架構(gòu)

2021-10-14 06:52:47

算法校驗(yàn)碼結(jié)構(gòu)

2022-11-04 14:16:05

2024-09-18 07:00:00

消息隊(duì)列中間件消息隊(duì)列
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號