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

📄 serial.c

📁 《增强型8051单片机实用开发技术》例程代码
💻 C
字号:
#include "includes.h"

INT8U xdata TxBuf[LenTxBuf],RxBuf[LenRxBuf];
INT8U xdata *inTxBuf,*outTxBuf,*inRxBuf,*outRxBuf;
INT8U xdata TIflag=1;

//********************************************************************************************//
/*
void SerialInit(void) reentrant//串口初始化
{    
    SCON=0x50;  //8位数据
    //SCON=0xD0;//9位数据
    TMOD&=0x0F;
    TMOD|=0x20;
    TL1=0xFD;
	TH1=0xFD;
    PCON=0x80;  //19200bps@11.059MHz,TL1=256-(2^SMOD/32*Fosc/12*1/Baudrate)
    //PCON=0x00;//9600bps@11.059MHz
    TR1=1;		//Start timer1
    ES=1;		//Enable serialport interrupt
}
*/
//********************************************************************************************//
void serial(void) reentrant//串口中断服务子程序
{
    INT8U *t;
    if(TI)
    {
        TI=0;
        if(inTxBuf==outTxBuf)
        {
            TIflag=1;
			return;
        }
        SBUF=*outTxBuf;
		outTxBuf++;
		if(outTxBuf==TxBuf+LenTxBuf) outTxBuf=TxBuf;     
    }
    if(RI)
    {
        RI=0;
        t=inRxBuf;
		t++;
		if(t==RxBuf+LenRxBuf) t=RxBuf;
		if(t==outRxBuf) return;
		*inRxBuf=SBUF;
		inRxBuf=t;
    }
}
//********************************************************************************************//
void InitSerialBuffer(void) reentrant//串口缓冲区初始化
{
        inTxBuf=TxBuf;outTxBuf=TxBuf;
        inRxBuf=RxBuf;outRxBuf=RxBuf;
        ES=1;
        //EA=1;
}
//********************************************************************************************//
void PrintChar(INT8U ch) reentrant//显示字符
{
    INT8U *t;

    OS_ENTER_CRITICAL();
    t=inTxBuf;
	t++;
    if(t==TxBuf+LenTxBuf) t=TxBuf;
    if(t==outTxBuf) 
	{
		OS_EXIT_CRITICAL();
		return;
	}                             //TxBuf Full
    *inTxBuf=ch;
    inTxBuf=t;
    OS_EXIT_CRITICAL(); 
    if( TIflag)
	{
        TIflag=0;
        TI=1;
    }	 
}

⌨️ 快捷键说明

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