打印
[应用相关]

当通过printf()处理信息,如何知道哪一UART传输数据?

[复制链接]
716|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)



使用特权

评论回复
地板
734774645|  楼主 | 2016-2-14 18:30 | 只看该作者
#include "stm32f4xx.h"
#include "printf.h"

void Printf_Configuration(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;
       
                //PB6->TX  PB7->Rx
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
   
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4);
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4);
       
                USART_InitStructure.USART_BaudRate = 19200;
    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_Init(UART4,&USART_InitStructure);   
    USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
    USART_Cmd(UART4,ENABLE);
}

int fputc(int ch, FILE *f)
{       
                while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
                USART_SendData(UART4, (uint8_t) ch);
                while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
                return ch;
}

int fgetc(FILE *f)
{
    int ch;
    while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);
    ch = USART_ReceiveData(UART4);
    while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
    USART_SendData(UART4, (uint8_t) ch);
    return ch;
}




#ifndef __PRINTF_H__
#define __PRINTF_H__

#include <stdio.h>

void Printf_Configuration(void);

#endif









使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

190

主题

3425

帖子

14

粉丝