
void send_byte(unsigned char x)
{
	unsigned char i;

	RCAP2L = TL833;
	RCAP2H = TH833;
	TL2 = TL833;
	TH2 = TH833;
	
	dat = x;
	TX = 0;

        ???????????
	delayRS();

	for(i=0;i<8;i++)	
	{
	        TX = out;
		delayRS();
		dat = dat >> 1;			 
	}
	TX = 1;

        ?????????????????
	delayRS();
	
}
// RX function
void rec_byte(unsigned char *x)
{
	unsigned char i;
	bit t=1;
	//T2CON = 0x00;
	RCAP2L = TL416;
	RCAP2H = TH416;
	TL2 = TL416;
	TH2 = TH416;


	din = 0;

        waitagain:  //?????????

	while(RX == 1);
         
       // check in the middle of start bit!!!!!!!   
        delayRS() ;//half bit
        if (RX==1) goto waitagain; //????????


        ?????????????
	delayRS();delayRS();delayRS();	   // 1.5 bit delay

	for(i=0;i<8;i++)
	{
		in = RX;		   //sample bit
		din = din << 1;
		delayRS();delayRS();	   // 1 bit delay
	}
	*x = din;

        //?????????
	delayRS();
        //here You can check validity of stop bit!!!
}




void main()
{
	unsigned char i,send[]="SEND A CHARACTERn&#65533;15",rec[]="character recieved:";
	unsigned char input='0';
	
	TX = 1;
	T2CON = 0x00;
	RCAP2L = TL833;
	RCAP2H = TH833;
	TL2 = TL833;
	TH2 = TH833;
	while(1)
	{	
	while(send[i])
		{
			send_byte(send[i++]);
			//delay1us(400);
		}
		i=0;

		rec_byte(&input);

		while(rec[i])
		{
			send_byte(rec[i++]);
			delay1us(50);  //????????? too short pause
                                       // allowed, but  too short
                                       // make delayRS()
		}
		send_byte(input);		
		i=0;
	}	

}
 

