⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 使用于克隆器
💻 C
字号:
/******************************************************************************
 Copyright (c) 2003 MStar Semiconductor, Inc.
 All rights reserved.

 [Module Name]: Uart.c
 [Date]:        04-Nov-2003
 [Comment]:
   Uart subroutines.
 [Reversion History]:
*******************************************************************************/

#define _UART_C_

// System
#include <intrins.h>

// Common
#include "define.h"
#include "mcu.h"
#include "global.h"
#include "userdef.h"
// External

// Internal
#include "uart.h"


//////////////////////////////////////////////////////////////
// Put character to uart
//
// Arguments: ucVal - output character
//////////////////////////////////////////////////////////////
#if (_DEBUG_RW_REG_EN_||_DEBUG_PRINT_EN_)
void putchar(BYTE ucVal)
{
    //EA = 0; // disable all interrupt
    ES = 0;

    SBUF = ucVal; // transfer to uart
    // wait transfer completing
    while (1)
    {
        if (TI)
            break;
    } // check flag
    TI = 0; // clear flag
    //ES = 1;
    ES = 1;                      // enable uart interrupt
    //EA = 1; // release all interrupt
}
#endif

//////////////////////////////////////////////////////////////////////////////
// Put string to uart.
//
// Arguments: pFmt - string address
//////////////////////////////////////////////////////////////////////////////
#if (_DEBUG_PRINT_EN_)
void putstr(BYTE code* pFmt)
{
    BYTE ucBff; // character buffer

    while (1)
    {
        ucBff = *pFmt; // get a character
        if (ucBff == _EOS_) // check end of string
            break;

        putchar(ucBff); // put a character

        pFmt++; // next
    } // while
}
#else
void putstr(BYTE code* pFmt)
{
    pFmt = pFmt;
}
#endif

//////////////////////////////////////////////////////////////////////////////
// Put string to uart with variable argument
//
// Arguments: pFmt - string address
//            wVal - print variable
//////////////////////////////////////////////////////////////////////////////
#if (_DEBUG_PRINT_EN_)
void printf(BYTE code* pFmt, WORD wVal)
{
    BYTE ucBff, ucDisp;
    BOOL bNotZero = FALSE, bHex = FALSE;
    WORD wDivider = 10000;

    while (ucBff = *(pFmt++))
    {
        if (ucBff == '%') // check special case
        {
            switch (*(pFmt++)) // check next character
            {
                case 'x': // hexadecimal number
                case 'X':
                    wDivider = 0x1000;
                    bHex = TRUE;
                case 'd': // decimal number
                case 'i':
                    if (wVal)
                    {
                        while (wDivider)
                        {
                            ucDisp = wVal / wDivider;
                            wVal -= ucDisp * wDivider;
                            if (ucDisp)
                                bNotZero = TRUE;

                            if (bNotZero)
                            {
                                if (ucDisp > 9)
                                    putchar(ucDisp + 55);
                                else
                                    putchar(ucDisp + 0x30);
                            }

                            if (bHex)
                                wDivider /= 0x10;
                            else
                                wDivider /= 10;
                        }
                    }
                    else
                        putchar('0');
                    break;
            } // switch
        }
        else // general
            putchar(ucBff); // put a character
    } // while
}
#else
void printf(BYTE code* pFmt, WORD wVal)
{
    pFmt = pFmt;
    wVal = wVal;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -