serial.c

来自「51开发板的源程序」· C语言 代码 · 共 86 行

C
86
字号
#include "c51base.h" 


/***************串口初始化*****************/

static uchar idata receive_buf[MAX_BUF_LEN];
static uchar idata buf_head,buf_tail;
static uchar  is_serial_get;

static uchar PopBuf(void)
{
	uchar temp;
	if(buf_head == buf_tail)
		temp = 0xff;
	else
	{
		temp=receive_buf[buf_head++];
		if(buf_head>=MAX_BUF_LEN)
			buf_head=0;
	}
	if(buf_head == buf_tail)
		is_serial_get = 0;
	return temp;
}

static void PushBuf(unsigned char temp)
{
	receive_buf[buf_tail++]=temp;
    if(buf_tail>=MAX_BUF_LEN)
        buf_tail=0;
}

static void SerialBufClear()
{
	buf_head = 0;
	buf_tail= 0;
	is_serial_get = 0;
}

void SerialInter() interrupt 4
{
	uchar serial_value;
	if(RI)
	{
		RI = 0;
		serial_value = SBUF;
		PushBuf(serial_value);
		is_serial_get = 1;
	}
}

void CommInit()
{
	TMOD = 0x21;
	TH1 = 0xfb;//11.059MHz,19200kbps
	SCON=0X50;
	PCON=0X80; 
	TR1=1;//start timer 1
	TI=1;//get ready for "printf",
    RI=0;//get ready for "getchar"
	SerialBufClear();
	ES = 1;
}


bit SerialCheck()
{
	return is_serial_get;
}

uchar ReadSerialBuf(uint time_out)
{
	uchar idata value,i;
	while(time_out--)
	{
		WDTReset_SM89516();
		for(i=0;i<=112;i++)
			;
		value = PopBuf();
		if(value!=0xff)
			return value;
	}
	return 0xff;
}

⌨️ 快捷键说明

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