[应用相关] 当通过printf()处理信息,如何知道哪一UART传输数据?

[复制链接]
923|3
 楼主| 734774645 发表于 2016-2-2 13:23 | 显示全部楼层 |阅读模式

在用户开发应用程序的过程中,往往需要通过使用printf()处理信息,如果NuMicro®系列支持的UART不只一组,我们可以通过定义 ”retarget.c”中的DEBUG_PORT,来决定printf()是使用哪一组UART传输数据。初始设置为UART0。
用户可以更改DEBUG_PORT的定义改变printf()的传输端口,以符合自己的系统构架。
zchong 发表于 2016-2-13 08:18 | 显示全部楼层
取决于你的底层驱动
 楼主| 734774645 发表于 2016-2-14 18:29 | 显示全部楼层
UART4_Printf_Scanf.zip (1.12 KB, 下载次数: 1)



 楼主| 734774645 发表于 2016-2-14 18:30 | 显示全部楼层
  1. #include "stm32f4xx.h"
  2. #include "printf.h"

  3. void Printf_Configuration(void)
  4. {
  5.                 GPIO_InitTypeDef GPIO_InitStructure;
  6.                 USART_InitTypeDef USART_InitStructure;
  7.        
  8.                 //PB6->TX  PB7->Rx
  9.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  10.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  11.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  13.    
  14.     GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4);
  15.     GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4);
  16.        
  17.                 USART_InitStructure.USART_BaudRate = 19200;
  18.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  19.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  20.     USART_InitStructure.USART_Parity = USART_Parity_No;
  21.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  22.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  23.     USART_Init(UART4,&USART_InitStructure);   
  24.     USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
  25.     USART_Cmd(UART4,ENABLE);
  26. }

  27. int fputc(int ch, FILE *f)
  28. {       
  29.                 while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
  30.                 USART_SendData(UART4, (uint8_t) ch);
  31.                 while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
  32.                 return ch;
  33. }

  34. int fgetc(FILE *f)
  35. {
  36.     int ch;
  37.     while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);
  38.     ch = USART_ReceiveData(UART4);
  39.     while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
  40.     USART_SendData(UART4, (uint8_t) ch);
  41.     return ch;
  42. }




  1. #ifndef __PRINTF_H__
  2. #define __PRINTF_H__

  3. #include <stdio.h>

  4. void Printf_Configuration(void);

  5. #endif









您需要登录后才可以回帖 登录 | 注册

本版积分规则

211

主题

3588

帖子

15

粉丝
快速回复 在线客服 返回列表 返回顶部