www.pudn.com > fskyiti.rar > fskyiti.c


//OK,LIMIN,2001.10.4 
 
#pragma ot (6) 
 
#include  
#include  
 
#define uchar unsigned char 
#define uint  unsigned int 
 
sbit  ch_rx   = P1^3 ; 
sbit  TX_EN   = P1^4 ; 
sbit  ch_tx   = P1^2 ; 
sbit  TX_ENABLE = P1^7; 
sbit  wdi = P1^5; 
 
//uchar ch_data[5]={0,0}; 
uchar receve_end_flag=0, rece_data=0, ch_counter=0,receive_byte_data=0; 
uint  time0_flag=0; 
bit   flag_send_over1=0, rece_flag=0, receive_byte_end_flag=0; 
bit   CH_TI=1; 
 
void dog()  
{ 
	wdi = ~wdi; 
} 
 
void ch_ser_short_delay() small 
{ 
	unsigned char data i; 
	for( i = 0 ; i < 12 ; i++ ) 
	{ 
		; 
	} 
} 
 
 
void ch_ser_delay() small 
{ 
	unsigned char data i; 
	for( i = 0 ; i < 25 ; i++ ) 
	{ 
		; 
	} 
} 
 
void modem_delay(unsigned char count) small 
{ 
	unsigned char data i,j; 
 	for(i = 0 ; i < count ; i++ ) 
	{ 
		for(j = 0; j < 255 ; j++ ) dog(); 
	} 
} 
 
void ch_ser_send(unsigned char buf) small 
{ 
 
	unsigned char data i; 
	//TR2 = 0; 
	CH_TI = 0; 
//	ch_rte = 0; 
//	modem_delay(10); 
 
	ch_tx = 0; 
	ch_tx = 0; 
	ch_ser_delay(); 
	for( i = 0 ; i < 8 ;i++ ) 
	{ 
		buf = buf>>1; 
		//bitbuf = CY; 
		ch_tx = CY;	 
		ch_tx = CY;	 
		ch_ser_delay(); 
	} 
	 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_tx = 1; 
	ch_ser_delay(); 
 
//	ch_rte = 1; 
    CH_TI = 1; 
//	TR2 = 1; 
} 
 
 
 
 
 
 
void main() 
{ 
	    ch_rx = 1; 
		TX_EN = 0; 
        TX_ENABLE = 1; 
 
	IT1 = 0; 
//	EA=1; 
//	EX1 = 1; 
//    EX0 = 0; 
	    SCON = 0x50;		//11.0592M,1200.8.1.N  p61 
		TMOD = 0x21;		//P42 
		PCON = PCON & 0x7f; 
		TH1  =  0xfd;     //P61 
		TL1  =  0xfd; 
		TR1  =  1;			//P53.56.68.69.88 
			 
		TI  =  0; 
		RI  =  0; 
		 
		EX0 =  0;			//禁止外部中断0的中断 
		EX1 =  0;//1;			//禁止外部中断1的中断 
	    ES  =  1;			//开放串行口中断 
		EA  =  1;			//开放CPU总中断 
 
 
		TH0 = 0xD8; 
	    TL0 = 0xF0; 
	    ET0 = 0; 
	    TR0 = 0; 
         
		ch_rx = 1; 
		TX_EN = 0; 
        TX_ENABLE = 1; 
		EX1 = 1; 
 
aa:	  	if(TX_ENABLE) 
		{ 
			EX1 = 0; 
			ES=1; 
			TX_EN=1; 
			modem_delay(2); 
            ch_ser_send(0xff); 
			modem_delay(1); 
			//ch_ser_send(0xaa); 
			//modem_delay(1); 
            ch_ser_send(0xf0); 
			modem_delay(1); 
			ch_ser_send(0xf0); 
			modem_delay(1); 
			ch_ser_send(0xf0); 
			modem_delay(1); 
			while(TX_ENABLE) 
			{ 
			  if(rece_flag) 
			  {             
                ch_ser_send(rece_data); 
			    rece_flag=0; 
			    rece_data=0; 
			  } 
			} 
			modem_delay(1); 
			ch_ser_send(0x0f); 
			modem_delay(1); 
			ch_ser_send(0x0f); 
			modem_delay(1); 
			TX_EN = 0; 
			EX1 = 1; 
		} 
	 
 
	  if(receive_byte_end_flag)                    // 
	  { 
	   receive_byte_end_flag=0; 
	   if( receive_byte_data!=0xf0)  
	   {   
	    receive_byte_data=0;  ch_counter=0;   
       }else 
	   {  
		receive_byte_data=0; ch_counter++; 
		TR0=1;  ET0=1; TH0=0XD8; TL0=0XF0; time0_flag=0; 
	   } 
       if(ch_counter==3) 
	   { 
		while(receve_end_flag<2)                          //包尾标记,用于判断是否结束本包的接收和转发。 
		{ 
		EX1=1; 
		if(time0_flag>=50)                                 //字节之间超时548ms,结束本包接受; 
		{ 
		//TR0=0;  ET0=0; TH0=0XD8;  TL0=0XF0; time0_flag=0; 
		goto yy; 
		} 
		 
		if(receive_byte_end_flag) 
		{ 
		receive_byte_end_flag=0; 
                         //ch_data[ch_counter]=receive_byte_data; 
		if(receive_byte_data==0x0f) { receve_end_flag++;  goto bb; }else{ receve_end_flag=0; } 
		SBUF =receive_byte_data; 
	    while(!flag_send_over1) {};	 
		modem_delay(0); 
		flag_send_over1=0; 
			              
bb:		receive_byte_data=0; 
		} 
		} 
yy:     TR0=0;  ET0=0; TH0=0XD8;  TL0=0XF0; time0_flag=0;      //关定时器; 
        receve_end_flag = 0; 
		ch_counter = 0; 
	   } 
	   //TR0=0;  ET0=0; TH0=0XD8;  TL0=0XF0; time0_flag=0; 
       //receve_end_flag = 0; 
	   //ch_counter = 0; 
	   EX1 = 1; 
		   
	  } 
 
	  if(time0_flag>=1)                                       //收到第一个包头后10.8ms内没有收到下一个包头则本包无效; 
	  { 
	   TR0=0;  ET0=0; TH0=0XD8;  TL0=0XF0; time0_flag=0; 
	   receive_byte_data=0;  ch_counter=0; 
      } 
	 	 
	  goto aa; 
} 
	 
 
void int1() interrupt 2 using 1 
{ 
	unsigned char data i,ch_sbuf,x; 
	unsigned char idata j=1,numb_0=0,numb_1=0; 
	//////////////////////////////////////////// 
	// learn the comm 
	/////////////////////////////////////////// 
    EX1=0; 
	ch_sbuf = 0; 
	ch_ser_short_delay(); 
	if(ch_rx ) { EX1=1; return;    }; 
	ch_ser_short_delay(); 
	ch_ser_short_delay(); 
	 
	for( i = 0 ; i < 8 ; i++ ) 
	{ 
		ch_sbuf = ch_sbuf>>1; 
		if(ch_rx) 
		{ 
			ch_sbuf = ch_sbuf|0x80; 
		}else 
		{ 
			ch_sbuf = ch_sbuf|0x00; 
		} 
		ch_ser_delay();	 
	} 
	 
	 
	 
	 
	//receive_byte_data = ch_sbuf;   
	ch_rx = 1; 
	//ch_ser_count++; 
	//modem_delay(1); 
 
	   for(i=0;i<8;i++) 
	   {        
		    dog(); 
			if((ch_sbuf&j)==0)  
			{ 
				numb_0++; 
			}else 
			{ 
                numb_1++; 
			} 
			j=j<<1; 
	   } 
	   if(numb_0!=numb_1)  
	   { 
        EX1=1; 
		receive_byte_data=0; 
		receive_byte_end_flag=0; 
	   }else 
	   { 
		   EX1=0; 
		receive_byte_end_flag=1; 
        receive_byte_data=ch_sbuf; 
	   } 
       /*if(receive_byte_data==0xf0) 
	   { 
		   receive_byte_data=0xf0; 
	   } 
*/ 
       numb_0=0; numb_1=0; 
	   
	 
} 
	 
 
void  int_t0() interrupt 1   using 1 
{  
	time0_flag++; 
    TH0=0xD8;TL0=0xF0; 
} 
 
void serial_port() interrupt 4 using 1 
{ 
    ES=0; 
	if(TI==1) { TI=0; flag_send_over1=1; ES=1; return; };					//发送中断标志位 
	RI=0; 
    rece_data = SBUF;  
	rece_flag = 1; 
    ES=1; 
}