
void Init_COM()
{
        SFRPAGE = LEGACY_PAGE;

	SSTA0 = 0x1A;   // disable double baud and select timer 3 for baud rate
	SCON0 = 0x50;   // mode 1. enable receiver clear RI & TI.

	RI0 = 0;
 	TI0 = 0;     // to send first char ok

  	SFRPAGE = UART1_PAGE;
	RI1 = 0;    // uart1
	TI1 = 1;

  	SFRPAGE = LEGACY_PAGE;

	t_in = 0;     // transmi buffer
	t_out = 0;
	r_in = 0;     // receive buffer
	r_out = 0;
	ti_restart = 1;   // to send first character

	ES0 = 1;  // enable Serial Interrupt
	PS = 1;   // serial interrupt to high priority
	ET2 = 1;
	PT2 = 0;  // Timer 2 low Priority
	EA = 1;
}

serial Io routines as below  file sio.c

#include <string.h>
#include "sio.h"

#define TBUF_SIZE   128         /*** Must be one of these powers of 2 (2,4,8,16,32,64,128) ***/
#define RBUF_SIZE   128         /*** Must be one of these powers of 2 (2,4,8,16,32,64,128) ***/

#define TBUF_SPACE  xdata       /*** Memory space where the transmit buffer resides ***/
#define RBUF_SPACE  xdata       /*** Memory space where the receive buffer resides ***/

#define CTRL_SPACE xdata        /*** Memory space for the buffer indexes ***/

#if TBUF_SIZE < 2
#error TBUF_SIZE is too small.  It must be larger than 1.
#elif TBUF_SIZE > 128
#error TBUF_SIZE is too large.  It must be smaller than 129.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif

#if RBUF_SIZE < 2
#error RBUF_SIZE is too small.  It must be larger than 1.
#elif RBUF_SIZE > 128
#error RBUF_SIZE is too large.  It must be smaller than 129.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif

static TBUF_SPACE unsigned char tbuf [TBUF_SIZE];
static RBUF_SPACE unsigned char rbuf [RBUF_SIZE];

static CTRL_SPACE unsigned char t_in = 0;
static CTRL_SPACE unsigned char t_out = 0;

static CTRL_SPACE unsigned char r_in = 0;
static CTRL_SPACE unsigned char r_out = 0;

static bit ti_restart = 0;  /* NZ if TI=1 is required */
static unsigned char oldsfrPage = 0;

static void com_isr (void) interrupt 4
{
   oldsfrPage = SFRPAGE;

   SFRPAGE = LEGACY_PAGE;

//Received data interrupt.

	if (RI0 != 0)
	{
	   RI0 = 0;
	   if (((r_in - r_out) & ~(RBUF_SIZE-1)) == 0)
	   {
	   	rbuf [r_in & (RBUF_SIZE-1)] = SBUF0;
	    	r_in++;
	   }
	}	

// Transmitted data interrupt.

	if(TI0 != 0)
	{
	   TI0 = 0;
	   if(t_in != t_out)
	   {
	      SBUF0 = tbuf [t_out & (TBUF_SIZE-1)];
	      t_out++;
	      ti_restart = 0;
	   }
	   else
	   {
	       ti_restart = 1;
	   }
	}

  	SFRPAGE = oldsfrPage;
}

#pragma disable

char putchar1 (unsigned char c)
{
  SFRPAGE = LEGACY_PAGE;

//If the buffer is full, return an error value.

  if (com_tbuflen () >= TBUF_SIZE)
     return (-1);

//Add the data to the transmit buffer.  If the
//transmit interrupt is disabled, then enable it.

   tbuf [t_in & (TBUF_SIZE - 1)] = c;
   t_in++;

   if(ti_restart)
   {
      ti_restart = 0;
      TI0 = 1;               /* generate transmit interrupt */
   }
   return (0);
}

#pragma disable

int getc (void)
{
   if (com_rbuflen () == 0)
	return (-1);

   return (rbuf [(r_out++) & (RBUF_SIZE - 1)]);
}

#pragma disable

unsigned char com_rbuflen (void)
{
   return (r_in - r_out);
}

#pragma disable

unsigned char com_tbuflen (void)
{
    return (t_in - t_out);
}
