zoukankan      html  css  js  c++  java
  • 51单片机使用串口printf

    #define BIT(x)                  ((unsigned int)((unsigned int)1<<x))
    

    串口初始化

    /*----------------------------------------------------------------------------*/
    /*----------------------------------------------------------------------------*/
    void Uart0_Init(unsigned int baud)
    {
        unsigned long tmp;
        
    #if 0
        P2CR  &= ~BIT(0);    //Input Mode
        P2PCR |= BIT(0);    //Enable pull-up resistor
        P2    |= BIT(0);
        
        P2CR  |=   BIT(1);  //Output Mode
        P2PCR &= ~(BIT(1)); //Disable pull-up resistor
        P2    |=   BIT(1);
    #endif
        
        /* Config Uart0:Mode 1, 8-bits asynchronous. */
        SCON = 0x50;
        PCON |=0x80;            //Double baud rate
        tmp = 16000000/16/baud; //16000000UL*2/2/16/baud;
        tmp = 65536 - tmp;
        
        RCLK = 0;               //T4 create rxd baud rate
        TCLK = 0;               //T4 create txd baud rate
        
        _push_(INSCON);
        Select_Bank1();
        T4CLKS = 0;             //T4 Clock select = Sysclk = 16MHz;
        T4PS1 = 0;              //T4 Prescaler = 1/1;
        T4PS0 = 0;
        T4M1 = 0;               //T4 mode select bits = 01, T4 as Baud rate generator.
        T4M0 = 1;
        TL4 = (unsigned char)tmp;
        TH4 = (unsigned char)(tmp>>8);
        TR4 = 1;
        Select_Bank0();
        _pop_(INSCON);
        
        REN = 1;                //Enable Receive
        
        //ES0 = 1;              //Enable Uart0 Interrupt
        //TI = 1;               //printf need
    }

    printf重映射

    /*----------------------------------------------------------------------------*/
    //printf call
    /*----------------------------------------------------------------------------*/
    char putchar(char ch)
    {
        //ES=0;  //Disable Uart0 Interrupt
        TI = 0;
        SBUF = (unsigned char)ch;
        while (!TI);
        TI = 0;
        //ES=1;   //Enable Uart0 Interrupt
        return (ch);
    }
  • 相关阅读:
    SqlServer查询优化方法
    关于导入excel问题
    修改SQL数据库中表字段类型时,报“一个或多个对象访问此列”错误的解决方法
    软件架构之我见
    算法-插入排序
    算法-快速排序
    WCF系列 Restful WCF
    WCF系列 基础概念
    cocos2dx-是男人就坚持20s 练手项目
    nodejs 聊天室简单实现
  • 原文地址:https://www.cnblogs.com/eruca520/p/14229895.html
Copyright © 2011-2022 走看看