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

📄 serial-io.c

📁 cs8900 c51应用
💻 C
字号:
/*------------------------------------------------------------------------------
SIO.C:  Serial Communication Routines.

Copyright 1995-2002 KEIL Software, Inc.
------------------------------------------------------------------------------*/
/* Alexey Selischev (bmp@beep.ru), 27.01.2003 */

//#include <reg51.h>
#include <inet/arch/at89s8252/serial-io.h>
#include <inet/system.h>

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
#define TBUF_SIZE	256		/* DO NOT CHANGE */
#define RBUF_SIZE	256		/* DO NOT CHANGE */

static xdata UINT8 tbuf [TBUF_SIZE];
static xdata UINT8 rbuf [RBUF_SIZE];

static idata UINT8 t_in = 0;
static idata UINT8 t_out = 0;
static idata UINT8 t_disabled = 0;

static idata UINT8 r_in = 0;
static idata UINT8 r_out = 0;

UINT8 RxBufEmpty; /* Nothing to read flag T/F, used in the slip.c */

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
static void com_isr (void) interrupt 4 using 2
{
/*------------------------------------------------
Received data interrupt.
------------------------------------------------*/
	if (RI != 0)
	{
		RI = 0;
		if ((r_in + 1) != r_out)
		{
			rbuf [r_in++] = SBUF;
			RxBufEmpty = FALSE;
		}
	}
/*------------------------------------------------
Transmitted data interrupt.
------------------------------------------------*/
	if (TI != 0)
	{
		TI = 0;
		if (t_in != t_out)
			SBUF = tbuf [t_out++];
		else
			t_disabled = 1;
	}
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
void com_initialize (void)
{
	/*------------------------------------------------
	Setup TIMER2 to generate the proper baud rate.
	------------------------------------------------*/
//saa : removed	com_baudrate ();

	/*------------------------------------------------
	Clear com buffer indexes.
	------------------------------------------------*/

	t_in = 0;
	t_out = 0;
	t_disabled = 1;

	r_in = 0;
	r_out = 0;

	/*------------------------------------------------
	Setup serial port registers.
	------------------------------------------------*/
	/* Already done in the init.c      */
	
	/*  ... */
	ES = 1;				/* enable serial interrupts */
	PS = 1;				/* set serial interrupts to high priority */
}

//saa com_baudrate removed!!!

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
INT8 com_putchar (UINT8 c)
{
/*------------------------------------------------
If the buffer is full, return an error value.
------------------------------------------------*/
	if ((TBUF_SIZE - com_tbuflen ()) <= 2) 
		return (-1);

/*------------------------------------------------
Add the data to the transmit buffer.  If the
transmit interrupt is disabled, then enable it.
------------------------------------------------*/
	EA = 0;                         /* Disable Interrupts */

	tbuf [t_in++] = c;

	if (t_disabled)			/* if transmitter is disabled */
	{
		t_disabled = 0;
		TI = 1;			/* enable it */
	}

	EA = 1;                         /* Enable Interrupts */
	return (0);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
INT16 com_getchar (void)
{
	INT16 c;
	if (com_rbuflen () == 0) 
		return (-1);

	EA = 0;                         /* Disable Interrupts */
	c = rbuf [r_out++];
	EA = 1;                         /* Enable Interrupts */
	return (c);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
UINT8 com_rbuflen (void)
{
	return (r_in - r_out);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
UINT8 com_tbuflen (void)
{
	return (t_in - t_out);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/


UINT8 _getkey (void)
{
	INT16 k;
	do
	{
		k = com_getchar ();
	}
	while (k == -1);
	return ((UINT8) k);
}


//char putchar (char c)
UINT8 sendchar (UINT8 c)
{
//	volatile unsigned int i;
	while (com_putchar (c) != 0)
	{
//	for (i=0; i<1000; i++)
		{
		/*** DO NOTHING ***/
		}
	}
	return (c);
}

⌨️ 快捷键说明

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