发新帖本帖赏金 3.00元(功能说明)我要提问
返回列表
打印
[技术问答]

请教N76E003如何重定向printf()到UART1

[复制链接]
2405|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
245810|  楼主 | 2019-1-30 18:56 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
芯片:N76E003;
编译环境:keil5
使用printf()函数时默认UART0输出,请教如何重定向printf()到UART1?
沙发
gaoyang9992006| | 2019-1-30 19:24 | 只看该作者
你下载BSP没,其中一个例子就是的。


里面一共两个文件

其中一个就是重定向到串口1的

使用特权

评论回复

打赏榜单

245810 打赏了 3.00 元 2019-01-30
理由:完美解决

板凳
gaoyang9992006| | 2019-1-30 19:28 | 只看该作者
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Website: http://www.nuvoton.com
//  E-Mail : MicroC-8bit@nuvoton.com
//  Date   : Jan/21/2017
//***********************************************************************************************************

//***********************************************************************************************************
//  File Function: N76E003 GPIO demo code
//***********************************************************************************************************
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"


unsigned char temp _at_ 0x08;
unsigned char idata itemp _at_ 0x80;
unsigned char xdata xtemp _at_ 0x80;

/*==========================================================================*/
void main (void)
{
                InitialUART1_Timer3(115200);
                TI_1 = 1;
                while(1)
                {
                        printf("\n hello world");
                        Timer0_Delay1ms(300);
                }
}
/***********************************************************************/
/*  This file is part of the C51 Compiler package                      */
/*  Copyright KEIL ELEKTRONIK GmbH 1990 - 2002                         */
/***********************************************************************/
/*                                                                     */
/*  PUTCHAR.C:  This routine is the general character output of C51.   */
/*  You may add this file to a uVision2 project.                       */
/*                                                                     */
/*  To translate this file use C51 with the following invocation:      */
/*     C51 PUTCHAR.C <memory model>                                    */
/*                                                                     */
/*  To link the modified PUTCHAR.OBJ file to your application use the  */
/*  following Lx51 invocation:                                         */
/*     Lx51 <your object file list>, PUTCHAR.OBJ <controls>            */
/*                                                                     */
/***********************************************************************/

//#include <reg51.h>
#include "N76E003.h"

/****************************************************************************/
/* Define putchar send from UART1, printf function will send from P1.6(TXD_1)
/* NOTICE: Since UART1 pin is multi-function with OCD DATA/CLK pin.
/* Suggest download than use run with realchip but not OCD mode.
/****************************************************************************/
#if 1
char putchar (char c)                                //for UART1_printf
{
                while (!TI_1);  /* wait until transmitter ready */
                TI_1 = 0;
                SBUF_1 = c;      /* output character */
                return (c);
}
#else
/*
* putchar (mini version): outputs charcter only
*/
char putchar (char c)                          //for UART0_printf
{                               
  while (!TI);
  TI = 0;
  return (SBUF = c);
}

#endif


使用特权

评论回复
地板
gaoyang9992006| | 2019-1-30 19:29 | 只看该作者
主要就是通过putchar函数的实现,实现重定向。
可以看看开头那个定义三个变量是否根本程序有关,删除试试,我手里没这个板子,没法试

使用特权

评论回复
评论
245810 2019-1-30 20:09 回复TA
谢谢你!已按楼上方法解决,加上条件编译: /****************************************************************************/ /* Define putchar send from UART1, printf function will send from P1.6(TXD_1) /* NOTICE: Since UART1 pin is multi-function with OCD DATA/CLK pin. /* Suggest download than use run with realchip but not OCD mode. /****************************************************************************/ #if 1 char putchar (char c) //for UA...  
5
245810|  楼主 | 2019-1-30 20:11 | 只看该作者

使用特权

评论回复
6
734774645| | 2019-1-30 20:21 | 只看该作者
https://bbs.21ic.com/icview-2622086-1-1.html
楼主参加活动啊,哈哈,趁机把你的赏金赚回来。

使用特权

评论回复
7
heisexingqisi| | 2019-1-30 21:31 | 只看该作者
对,楼主下载BSP,去官方,这个版块上面哪个图片就是链接。

使用特权

评论回复
8
稳稳の幸福| | 2019-1-30 22:18 | 只看该作者
占楼主的光了,学到了,谢谢。

使用特权

评论回复
9
伏尔加的鱼| | 2019-2-2 10:37 | 只看该作者
keil安装目录的C51文件夹内有LIB文件夹,里面有个PUTCHAR.C文件,在这个文件里面修改就行了 这里面有putchar函数是给用户修改的  修改发送完成中断标识和数据缓冲寄存器即可,切记不要开串口中断,并且需要在初始化串口的时候将发送完成中断标识软件设置为1,因为在putchar函数内是先检测发送完成再发送数据的

使用特权

评论回复
10
伏尔加的鱼| | 2019-2-2 10:38 | 只看该作者
/*
* putchar (mini version): outputs charcter only
*/
char putchar (char c)  {
  while (!TI);
  TI = 0;
  return (SBUF = c);
}
//修改这里面的TI和SBUF即可

使用特权

评论回复
11
dongnanxibei| | 2019-4-9 23:03 | 只看该作者
我来学习,果然有牛人,吃透了例子。

使用特权

评论回复
12
wanduzi| | 2019-4-10 23:34 | 只看该作者
给力,回帖里大神也不少

使用特权

评论回复
发新帖 本帖赏金 3.00元(功能说明)我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

47

帖子

0

粉丝