www.pudn.com > 52885.rar > 5288130CXTEST.c


/*********************************************************************************** 
 *Filename:     testv0x.c                         Vision 1.0                                 * 
 *********************************************************************************** 
 *           Copyright (c) 2001, by DaTang Telecom Technology Co., LT              * 
 *                                All rights reserved                              * 
 *********************************************************************************** 
 *           Compiler    : mf51 C51                                                * 
 *           Author       : Shu Jing                                             * 
 *           Created on  : 2002-4-10                                               * 
 *********************************************************************************** 
 *Description: GNSS97II                                  
 *Note                                                                             * 
 *Update:   2002-4-31  
 * upgrade: 2002-5-24 (add  the power up fuction) 
 * upgrade: 2002-9-5   (modify the  async fuction for  interrupt and process module 
 * upgrade: 2003-2-11 updata version 1.0 
 **********************************************************************************/ 
 
 
 #pragma large DB OE 
 
 #include   
 #include  
 #include  
 #include  
 #include  
 #include 
 #include 
  
 #define uchar unsigned char 
 #define uint unsigned int 
 #define END_CHAR 0x03 
 #define EOT 0x04 
 #define ENQ 0x05 
 #define ACK 0x06 
 #define NAK 0x15 
 #define BUF_LEN 0x5ff 
 #define ADDRESS_BOARD 0x05 
 #define OneSecond 100 
  
 #define MSG 0x80	/*	MESSAGE RECEICED 	*/ 
 #define MSG_NO 0x0F	/*	NO MESSAGE RECEIVED	*/ 
 #define MSG_ER 0xFF	/*	ERROR MESSAGE RECEIVED	*/ 
  
 #define address_l 0x8000 
 #define address_m 0x8001 
 #define address_h 0x8002 
 #define EXT_STATUS_BYTE1 XBYTE[0x8024] 
 #define EXT_STATUS_BYTE2 XBYTE[0x8025] 
  
 #define modulus 100 
 #define TIME_500Ms 50 
 #define ONE_SECOND_T 0x10 
  
 sbit com_f=P1^0; 
 sbit runled=P1^5; 
 sbit fpga_reset=P3^5; 
 sbit led_green=P1^1; 
 sbit led_red=P1^4; 
 bit mask_c; 
  
  
 uchar code EOT_BUF[]={EOT,0x84,0x03}; 
 uchar code ENQ_BUF[]={ENQ,0x85,0x03}; 
 uchar code ACK_BUF[]={ACK,0x86,0x03}; 
 uchar code NAK_BUF[]={NAK,0x95,0x03}; 
  
  
 uchar xdata rb_2m_alm1,rb_2m_alm1_ph,rb_2m_alm2_ph,rb_2m_alm1_old,rb_2m_alm1_old_ph,rb_2m_alm2_old_ph; 
 uchar xdata rb_2m_alm2,rb_2m_alm2_old; 
 uchar xdata rb_alm1_remote,rb_alm2_remote,alm_status_byte1,alm_status_byte2; 
 uchar xdata tc_rb_2m_alm1_flag,tc_rb_2m_alm2_flag,flag_report_alm_cleared_rb1,flag_report_alm_cleared_rb2; 
 uchar xdata tc_rb_2m_alm1,tc_rb_2m_alm2; 
  
 uchar xdata *send_hed; 
 uchar xdata *send_end; 
 uchar xdata *send_hed1,*send_end1; 
 uchar xdata *index_sp,*index_ss,*index_se,*index_so,*index_sp1,*index_ss1,*index_se1,*index_so1; 
 uchar xdata send_buf2[130]; 
 uchar xdata in_buf[750]; 
 uchar xdata res_buf[30]; 
 uchar xdata send_buf[BUF_LEN]; 
 uchar xdata send_buf1[800]; 
 uchar xdata msg_buf[40]; 
 uchar code END_STRING[3]={';',0x03,0x00}; 
 uchar Atag_c[8],CTAG[6]; 
 long int atag; 
  
 uchar data R_verify,S_verify,R_index,ACK_R,ACK_S,R_msg_typ; 
 uchar flag_verify; //identify whether the  verify byte has been transmited 
 uchar flag_address;//identigy whether the source address has been transmited 
 uchar flag_command;//identigy whether to transmit the command byte 
 uchar *index_sc; 
 uchar flag_serial1; 
 uchar transmitover,error_t,FLAG_RECV,address_source,msg_length,time_20s,time_1s,time_100s; 
 uchar com_fault,com_fault_old,rbclk_condition,flag_powerup; 
  
  
 uchar byte_l[12],alm_ref[12],flag_phase_available[12],flag_interrupt_available; 
 uchar flag_1s,flag_10s,flag_100s,flag_report_100s,flag_phase_original[12],flag_respective_test_activity[12]; 
 uchar time_counter,time_counter_500ms,counter_s,counter_phase_quantity,counter_sample[12],counter_fpga,flag_rbalm,flag_reset,flag_restart,flag_test_activity; 
 uchar n,x; 
  
 uchar tie[12][8],mtie_1s_a[12][9],mtie_10s_a[12][9],mtie_100s_a[12][9],tie100s_ave_a[12][9],tie_max_a[12][9],tie_min_a[12][9]; 
  
 uint temp_d[12], byte_m[12]; 
  
 long int phase_original[12],mtie1s[12],mtie10s[12],mtie100s[12],phase_intrinsical[12],byte_h[12]; 
 long int tie_sample[12],tie_1s[12],tie100s_sum[12],tie_max[12],tie_min[12],tie100s_ave[12]; 
  
void push_buf() 
{ 
	uint i; 
	i=0; 
	index_so=index_se; 
	do 
	{ 
		index_se++; 
		if (index_se>send_end) 
		index_se=send_hed; 
		 
		if(index_se==index_ss) 
		{ 
			index_se=index_so; 
			break; 
		} 
		 
		*index_se=in_buf[i]; 
		i++; 
	} 
	while(*index_se!=END_CHAR); 
} 
 
/*void push_buf1() 
{ 
	n=0; 
	do 
	{ 
		index_se1++; 
		if(index_se1==index_ss1) 
		index_se1--; 
		if (index_se1>send_end1) 
		index_se1=send_hed1; 
		*index_se1=send_buf2[n]; 
		n++; 
	} 
	while(*index_se1!=END_CHAR); 
}*/ 
 
 
void get_buf() 
{ 
	uchar i=0xFF; 
	do 
	{ 
		i++; 
		msg_buf[i]=res_buf[i]; 
   	} 
   	while(msg_buf[i]!=END_CHAR); 
} 
 
void strncpy1(unsigned char * a,unsigned char * b,unsigned char k) 
{ 
	int m; 
	for (m=0;m=2) 
	{ 
		flag_1s=1; 
		time_counter=0; 
		counter_s++; 
	} 
	 
	//EX0=0; 
	for(n=0;n<12;n++)//for cycle 
	{ 
		 
		/*if(counter_phase_quantity==0) 
		break;*/ 
		 
		if(flag_phase_available[n])//singal is available 
		{ 
			counter_sample[n]++; 
			//counter_phase_quantity--; 
			flag_phase_available[n]=0; 
			//temp_d[n]=byte_m[n]*0x100; 
			phase_original[n]=(byte_l[n]+(byte_m[n]*0x100)+(byte_h[n]*0x10000)); 
			//phase_original[n]=temp_d[n];//+byte_m[n]*0x100+byte_l[n]; 
			 
			if(flag_restart)//test restart 
			{ 
				//flag_restart=0; 
				phase_intrinsical[n]=phase_original[n]; 
				flag_phase_original[n]=0; 
				flag_reset=flag_test_activity=flag_respective_test_activity[n]=1; 
			} 
			 
			else if(flag_phase_original[n])//first data is  intrinsical phase interval 
			{ 
				phase_intrinsical[n]=phase_original[n]; 
				flag_phase_original[n]=0; 
				flag_respective_test_activity[n]=1; 
			} 
			 
			else//not initialization 
			{ 
				tie_sample[n]=(phase_original[n]-phase_intrinsical[n])+tie_sample[n]; 
				 
			}//not initialization 
		}//singal is available 
		 
		if(flag_1s) 
		{ 
			tie_1s[n]=(tie_sample[n]*16)/counter_sample[n]; 
			tie_sample[n]=counter_sample[n]=0; 
			//counter_sample[n]=0; 
			//temp_d[n]++; 
			if(tie_1s[n]tie_max[n]) 
			tie_max[n]=tie_1s[n]; 
			 
			tie100s_sum[n]+=tie_1s[n]; 
			 
			if(counter_s==1) 
			{ 
				mtie1s[n]=tie_max[n]-tie_min[n]; 
			} 
			 
			if(counter_s==10) 
			{ 
				mtie10s[n]=tie_max[n]-tie_min[n]; 
				 
			} 
			if(counter_s==100) 
			{ 
				mtie100s[n]=tie_max[n]-tie_min[n]; 
				tie100s_ave[n]=(tie100s_sum[n]/100); 
			} 
		} 
			 
		/*if((n==11)&&(counter_phase_quantity)) 
		{ 
			n=0xff; 
		}*/ 
		 
	}//for cycle 
	 
	if(flag_1s)//integrated 1s txd2 msg process and 100s msg process 
	{ 
		flag_1s=0; 
		atag++; 
		//if(temp_d[5]!=atag) 
		//temp_d[5]=atag; 
		if(atag>999999) 
		atag=0; 
		 
		Atag_c[5]=(atag)%10+0x30; 
		Atag_c[4]=(atag/10)%10+0x30; 
		Atag_c[3]=(atag/100)%10+0x30; 
		Atag_c[2]=(atag/1000)%10+0x30; 
		Atag_c[1]=(atag/10000)%10+0x30; 
		Atag_c[0]=(atag/100000)%10+0x30; 
		 
		p=send_buf2+7; 
		 
		strncpy1(p,Atag_c,8); 
		p=p+8; 
		 
		 
		for(n=0;n<12;n++)//for cycle 
		{ 
			if(alm_ref[n]==0)//ref signal available 
			{ 
				if(tie_1s[n]>=0) 
				tie[n][0]=0x2b; 
				else 
				tie[n][0]=0x2d; 
				 
				tie[n][1]=(tie_1s[n]/1000000)%10+0x30; 
				tie[n][2]=(tie_1s[n]/100000)%10+0x30; 
				tie[n][3]=(tie_1s[n]/10000)%10+0x30; 
				tie[n][4]=(tie_1s[n]/1000)%10+0x30; 
				tie[n][5]=(tie_1s[n]/100)%10+0x30; 
				tie[n][6]=(tie_1s[n]/10)%10+0x30; 
				tie[n][7]=tie_1s[n]%10+0x30; 
				 
				strncpy1(p,tie[n],8); 
				p=p+8; 
				 
			}//ref signal available 
			*p=','; 
			p++; 
			//tie_1s[n]=0; 
		}//for cycle 
		 
		p--; 
		strncpy1(p,";\x0d\x0a\x03",4); 
		 
		n=0; 
		index_so1=index_se1; 
		do 
		{ 
			index_se1++; 
			if (index_se1>send_end1) 
			index_se1=send_hed1; 
			 
			if(index_se1==index_ss1) 
			{ 
				index_se1=index_so1; 
				break; 
			} 
			 
			*index_se1=send_buf2[n]; 
			n++; 
		} 
		while(*index_se1!=END_CHAR); 
		 
		//push_buf1(); 
		 
		//send to com 
		//SBUF1=send_buf2[0]; 
		if(flag_serial1==0) 
		{ 
			index_ss1=index_sp1; 
			index_sp1++; 
	 		if(index_sp1>send_end1) 
	 		index_sp1=send_hed1; 
			 
			SBUF1=*index_sp1; 
			flag_serial1++; 
		} 
		else 
		flag_serial1++; 
		 
		 
		 
	/*if(flag_1s)//integrated 1s txd2 msg process and 100s msg process 
	{ 
		flag_1s=0; 
		atag++; 
		//if(temp_d[5]!=atag) 
		//temp_d[5]=atag; 
		if(atag>999999) 
		atag=0; 
		 
		Atag_c[5]=(atag)%10+0x30; 
		Atag_c[4]=(atag/10)%10+0x30; 
		Atag_c[3]=(atag/100)%10+0x30; 
		Atag_c[2]=(atag/1000)%10+0x30; 
		Atag_c[1]=(atag/10000)%10+0x30; 
		Atag_c[0]=(atag/100000)%10+0x30; 
		 
		p=send_buf2+7; 
		 
		strncpy1(p,Atag_c,8); 
		p=p+8; 
		 
		 
		for(n=0;n<12;n++)//for cycle 
		{ 
			if(alm_ref[n]==0)//ref signal available 
			{ 
				if(phase_original[n]>=0) 
				tie[n][0]=0x2b; 
				else 
				tie[n][0]=0x2d; 
				 
				tie[n][1]=(phase_original[n]/1000000)%10+0x30; 
				tie[n][2]=(phase_original[n]/100000)%10+0x30; 
				tie[n][3]=(phase_original[n]/10000)%10+0x30; 
				tie[n][4]=(phase_original[n]/1000)%10+0x30; 
				tie[n][5]=(phase_original[n]/100)%10+0x30; 
				tie[n][6]=(phase_original[n]/10)%10+0x30; 
				tie[n][7]=phase_original[n]%10+0x30; 
				 
				strncpy1(p,tie[n],8); 
				p=p+8; 
				 
			}//ref signal available 
			*p=','; 
			p++; 
			tie_sample[n]=tie_1s[n]=0; 
		}//for cycle 
		 
		p--; 
		strncpy1(p,";\x0d\x0a\x03",4); 
		 
		//send to com 
		SBUF1=send_buf2[0];*/ 
		 
		 
		if(counter_s==100)//100s msg process 
		{ 
			counter_s=0; 
			/*report 100s msg*/ 
			strncpy1(in_buf,"R-IN-PFM",8); 
			p=in_buf+8; 
			for(n=0;n<12;n++) 
			{ 
				*p=':'; 
				p++; 
				 
				if(alm_ref[n]==0)//ref signal available 
				{ 
					strncpy1(p,"INP-",4); 
					p+=4; 
					if(n<9) 
					{ 
						*p=n+0x31; 
						p++; 
					} 
					else 
					{ 
						*p=0x31; 
						p++; 
						*p=n+0x27; 
						p++; 
					} 
					 
					if(mtie1s[n]>99999)//6 bit available 
					{ 
						mtie_1s_a[n][2]=(mtie1s[n]/1000000)%10+0x30; 
						mtie_1s_a[n][3]=(mtie1s[n]/100000)%10+0x30; 
						mtie_1s_a[n][4]=(mtie1s[n]/10000)%10+0x30; 
						mtie_1s_a[n][5]=(mtie1s[n]/1000)%10+0x30; 
						mtie_1s_a[n][6]=(mtie1s[n]/100)%10+0x30; 
						mtie_1s_a[n][7]=(mtie1s[n]/10)%10+0x30; 
						mtie_1s_a[n][8]=mtie1s[n]%10+0x30; 
						l=9; 
					}//6 bit available  
					else if(mtie1s[n]>999) 
					{ 
						mtie_1s_a[n][2]=(mtie1s[n]/10000)%10+0x30; 
						mtie_1s_a[n][3]=(mtie1s[n]/1000)%10+0x30; 
						mtie_1s_a[n][4]=(mtie1s[n]/100)%10+0x30; 
						mtie_1s_a[n][5]=(mtie1s[n]/10)%10+0x30; 
						mtie_1s_a[n][6]=mtie1s[n]%10+0x30; 
						l=7; 
					} 
					else 
					{ 
						mtie_1s_a[n][2]=(mtie1s[n]/100)%10+0x30; 
						mtie_1s_a[n][3]=(mtie1s[n]/10)%10+0x30; 
						mtie_1s_a[n][4]=mtie1s[n]%10+0x30; 
						l=5; 
					} 
					strncpy1(p,mtie_1s_a[n],l); 
					p=p+l; 
					 
					if(mtie10s[n]>99999)//mtie10s transform 
					{ 
						mtie_10s_a[n][2]=(mtie10s[n]/1000000)%10+0x30; 
						mtie_10s_a[n][3]=(mtie10s[n]/100000)%10+0x30; 
						mtie_10s_a[n][4]=(mtie10s[n]/10000)%10+0x30; 
						mtie_10s_a[n][5]=(mtie10s[n]/1000)%10+0x30; 
						mtie_10s_a[n][6]=(mtie10s[n]/100)%10+0x30; 
						mtie_10s_a[n][7]=(mtie10s[n]/10)%10+0x30; 
						mtie_10s_a[n][8]=mtie10s[n]%10+0x30; 
						l=9; 
					} 
					else if(mtie10s[n]>999) 
					{ 
						mtie_10s_a[n][2]=(mtie10s[n]/10000)%10+0x30; 
						mtie_10s_a[n][3]=(mtie10s[n]/1000)%10+0x30; 
						mtie_10s_a[n][4]=(mtie10s[n]/100)%10+0x30; 
						mtie_10s_a[n][5]=(mtie10s[n]/10)%10+0x30; 
						mtie_10s_a[n][6]=mtie10s[n]%10+0x30; 
						l=7; 
					} 
					else 
					{ 
						mtie_10s_a[n][2]=(mtie10s[n]/100)%10+0x30; 
						mtie_10s_a[n][3]=(mtie10s[n]/10)%10+0x30; 
						mtie_10s_a[n][4]=mtie10s[n]%10+0x30; 
						l=5; 
					} 
					strncpy1(p,mtie_10s_a[n],l); 
					p=p+l; 
					 
					if(mtie100s[n]>99999)//100s mtie transform 
					{ 
						mtie_100s_a[n][2]=(mtie100s[n]/1000000)%10+0x30; 
						mtie_100s_a[n][3]=(mtie100s[n]/100000)%10+0x30; 
						mtie_100s_a[n][4]=(mtie100s[n]/10000)%10+0x30; 
						mtie_100s_a[n][5]=(mtie100s[n]/1000)%10+0x30; 
						mtie_100s_a[n][6]=(mtie100s[n]/100)%10+0x30; 
						mtie_100s_a[n][7]=(mtie100s[n]/10)%10+0x30; 
						mtie_100s_a[n][8]=mtie100s[n]%10+0x30; 
						l=9; 
					} 
					else if(mtie100s[n]>999) 
					{ 
						mtie_100s_a[n][2]=(mtie100s[n]/10000)%10+0x30; 
						mtie_100s_a[n][3]=(mtie100s[n]/1000)%10+0x30; 
						mtie_100s_a[n][4]=(mtie100s[n]/100)%10+0x30; 
						mtie_100s_a[n][5]=(mtie100s[n]/10)%10+0x30; 
						mtie_100s_a[n][6]=mtie100s[n]%10+0x30; 
						l=7; 
					} 
					else 
					{ 
						mtie_100s_a[n][2]=(mtie100s[n]/100)%10+0x30; 
						mtie_100s_a[n][3]=(mtie100s[n]/10)%10+0x30; 
						mtie_100s_a[n][4]=mtie100s[n]%10+0x30; 
						l=5; 
					} 
					strncpy1(p,mtie_100s_a[n],l); 
					p=p+l; 
					 
					 
					if(tie100s_ave[n]>=0)//100s tie average transform 
					tie100s_ave_a[n][1]=0x2b;//'+' 
					else 
					tie100s_ave_a[n][1]=0x2d;//'-' 
					 
					if((tie100s_ave[n]>99999)||(tie100s_ave[n]<(-99999))) 
					{ 
						tie100s_ave_a[n][2]=(tie100s_ave[n]/1000000)%10+0x30; 
						tie100s_ave_a[n][3]=(tie100s_ave[n]/100000)%10+0x30; 
						tie100s_ave_a[n][4]=(tie100s_ave[n]/10000)%10+0x30; 
						tie100s_ave_a[n][5]=(tie100s_ave[n]/1000)%10+0x30; 
						tie100s_ave_a[n][6]=(tie100s_ave[n]/100)%10+0x30; 
						tie100s_ave_a[n][7]=(tie100s_ave[n]/10)%10+0x30; 
						tie100s_ave_a[n][8]=tie100s_ave[n]%10+0x30; 
						l=9; 
					} 
					else if((tie100s_ave[n]>999)||(tie100s_ave[n]<(-999))) 
					{ 
						tie100s_ave_a[n][2]=(tie100s_ave[n]/10000)%10+0x30; 
						tie100s_ave_a[n][3]=(tie100s_ave[n]/1000)%10+0x30; 
						tie100s_ave_a[n][4]=(tie100s_ave[n]/100)%10+0x30; 
						tie100s_ave_a[n][5]=(tie100s_ave[n]/10)%10+0x30; 
						tie100s_ave_a[n][6]=tie100s_ave[n]%10+0x30; 
						l=7; 
					} 
					else 
					{ 
						tie100s_ave_a[n][2]=(tie100s_ave[n]/100)%10+0x30; 
						tie100s_ave_a[n][3]=(tie100s_ave[n]/10)%10+0x30; 
						tie100s_ave_a[n][4]=tie100s_ave[n]%10+0x30; 
						l=5; 
					} 
					strncpy1(p,tie100s_ave_a[n],l); 
					p=p+l; 
					 
					 
					if(tie_max[n]>=0)//100s maximum tie transform 
					tie_max_a[n][1]=0x2b;//'+' 
					else 
					tie_max_a[n][1]=0x2d;//'-' 
					 
					if((tie_max[n]>99999)||(tie_max[n]<(-99999))) 
					{ 
						tie_max_a[n][2]=(tie_max[n]/1000000)%10+0x30; 
						tie_max_a[n][3]=(tie_max[n]/100000)%10+0x30; 
						tie_max_a[n][4]=(tie_max[n]/10000)%10+0x30; 
						tie_max_a[n][5]=(tie_max[n]/1000)%10+0x30; 
						tie_max_a[n][6]=(tie_max[n]/100)%10+0x30; 
						tie_max_a[n][7]=(tie_max[n]/10)%10+0x30; 
						tie_max_a[n][8]=tie_max[n]%10+0x30; 
						l=9; 
					} 
					else if((tie_max[n]>999)||(tie_max[n]<(-999))) 
					{ 
						tie_max_a[n][2]=(tie_max[n]/10000)%10+0x30; 
						tie_max_a[n][3]=(tie_max[n]/1000)%10+0x30; 
						tie_max_a[n][4]=(tie_max[n]/100)%10+0x30; 
						tie_max_a[n][5]=(tie_max[n]/10)%10+0x30; 
						tie_max_a[n][6]=tie_max[n]%10+0x30; 
						l=7; 
					} 
					else 
					{ 
						tie_max_a[n][2]=(tie_max[n]/100)%10+0x30; 
						tie_max_a[n][3]=(tie_max[n]/10)%10+0x30; 
						tie_max_a[n][4]=tie_max[n]%10+0x30; 
						l=5; 
					} 
					strncpy1(p,tie_max_a[n],l); 
					p=p+l; 
					 
					 
					if(tie_min[n]>=0)//100s minimum tie transform 
					tie_min_a[n][1]=0x2b;//'+' 
					else 
					tie_min_a[n][1]=0x2d;//'-' 
					 
					if((tie_min[n]>99999)||(tie_min[n]<(-99999))) 
					{ 
						tie_min_a[n][2]=(tie_min[n]/1000000)%10+0x30; 
						tie_min_a[n][3]=(tie_min[n]/100000)%10+0x30; 
						tie_min_a[n][4]=(tie_min[n]/10000)%10+0x30; 
						tie_min_a[n][5]=(tie_min[n]/1000)%10+0x30; 
						tie_min_a[n][6]=(tie_min[n]/100)%10+0x30; 
						tie_min_a[n][7]=(tie_min[n]/10)%10+0x30; 
						tie_min_a[n][8]=tie_min[n]%10+0x30; 
						l=9; 
					} 
					else if((tie_min[n]>999)||(tie_min[n]<(-999))) 
					{ 
						tie_min_a[n][2]=(tie_min[n]/10000)%10+0x30; 
						tie_min_a[n][3]=(tie_min[n]/1000)%10+0x30; 
						tie_min_a[n][4]=(tie_min[n]/100)%10+0x30; 
						tie_min_a[n][5]=(tie_min[n]/10)%10+0x30; 
						tie_min_a[n][6]=tie_min[n]%10+0x30; 
						l=7; 
					} 
					else 
					{ 
						tie_min_a[n][2]=(tie_min[n]/100)%10+0x30; 
						tie_min_a[n][3]=(tie_min[n]/10)%10+0x30; 
						tie_min_a[n][4]=tie_min[n]%10+0x30; 
						l=5; 
					} 
					strncpy1(p,tie_min_a[n],l); 
					p=p+l; 
					 
					tie100s_sum[n]=0;//clear the 100s data 
					tie_max[n]=tie_min[n]=tie_1s[n]; 
					 
				}//ref signal available 
			}//for cycle 
			 
			strncpy1(p,END_STRING,2); 
			push_buf(); 
			 
		}//100s msg process 
		//EX0=1; 
	}/*integrated 1s txd2 msg process and 100s msg process*/ 
} 
 
void TEST_RESET() 
{ 
	/*disable the ext int*/ 
	EX0=0; 
	fpga_reset=0;/*reset the fpga*/ 
	 
	atag=time_counter=counter_s=flag_1s=flag_10s=flag_100s=flag_test_activity=counter_phase_quantity=0; 
	 
	for(n=0;n<12;n++) 
	{ 
		mtie1s[n]=mtie10s[n]=mtie100s[n]=phase_intrinsical[n]=0; 
		tie_sample[n]=tie_1s[n]=tie100s_sum[n]=tie_max[n]=tie_min[n]=tie100s_ave[n]=0; 
		counter_sample[n]=0; 
	} 
	 
	flag_restart=1; 
	time_counter_500ms=TIME_500Ms; 
	 
	/*enable ext int*/ 
	 
} 
 
void alm_monitor()//report alm change command 5 
{ 
	uchar *p,flag,alarm[3]; 
	 
	alarm[0]=':'; 
	 
	p=in_buf; 
	flag=1; 
	 
	alm_status_byte1=EXT_STATUS_BYTE1; 
	alm_status_byte2=EXT_STATUS_BYTE2; 
	 
	for(n=0;n<8;n++)//for cycle 
	{ 
		if((alm_status_byte1&(0x01<rb_2m_alm1_old_ph)//rb 2Mhz locate alm1 occur 
	{ 
		tc_rb_2m_alm1_flag=tc_rb_2m_alm1=0;//timecounter shut 
		rb_2m_alm1_old_ph=rb_2m_alm1_ph; 
		rb_2m_alm1=rb_2m_alm1_ph; 
		 
		if(rb_2m_alm1!=rb_2m_alm1_old) 
		{ 
			rb_2m_alm1_old=rb_2m_alm1; 
			 
			if(flag) 
			{ 
				strncpy1(p,"RALM:3",6); 
				p=p+6; 
				flag=0; 
			} 
			 
			alarm[1]=0x60; 
			alarm[2]=0x6D; 
			strncpy1(p,alarm,3); 
			p=p+3; 
		}//report the msg 
	} 
	else if(rb_2m_alm1_phrb_2m_alm2_old_ph)//rb 2Mhz locate alm2 occur 
	{ 
		tc_rb_2m_alm2_flag=tc_rb_2m_alm2=0;//timecounter shut 
		rb_2m_alm2_old_ph=rb_2m_alm2_ph; 
		rb_2m_alm2=rb_2m_alm2_ph; 
		 
		if(rb_2m_alm2!=rb_2m_alm2_old) 
		{ 
			rb_2m_alm2_old=rb_2m_alm2; 
			 
			if(flag) 
			{ 
				strncpy1(p,"RALM:3",6); 
				p=p+6; 
				flag=0; 
			} 
			 
			alarm[1]=0x61; 
			alarm[2]=0x6D; 
			strncpy1(p,alarm,3); 
			p=p+3; 
		}//report the msg 
	} 
	else if(rb_2m_alm2_phsend_end) 
	 			index_sp=send_hed; 
	 			if(*index_sp!=END_CHAR) 
	 			{ 
	 				SBUF=*index_sp; 
	 				S_verify^=*index_sp; 
	 			} 
	 			else 
	 			{ 
	 				if(flag_verify) 
	 				{ 
		 				SBUF=END_CHAR; 
		 				flag_verify=0; 
		 				transmitover=0;//over 
	 				} 
	 				else 
		 			{ 
		 				S_verify|=0x80; 
		 				SBUF=S_verify; 
		 				flag_verify=1; 
		 				index_sp--; 
		 			} 
	 			} 
	 		}//stransmit data frame 
 		}//indentify stransmit mission complete 
 		else transmitover=0;//over 
 	}//TI interrupt 
 	return; 
} 
 
void serial1() interrupt 7 using 3 
{ 
	if(TI_1) 
	{ 
		TI_1=0; 
		 
		index_sp1++; 
 		if(index_sp1>send_end1) 
 		index_sp1=send_hed1; 
 		 
 		if(*index_sp1!=END_CHAR) 
 		SBUF1=*index_sp1; 
		else if(index_sp1!=index_se1)//???? 
		{ 
			index_ss1=index_sp1; 
			index_sp1++; 
 			if(index_sp1>send_end1) 
 			index_sp1=send_hed1; 
 			SBUF1=*index_sp1; 
 			flag_serial1--; 
		} 
		else 
		{ 
			index_ss1=index_sp1; 
			flag_serial1=0; 
		} 
		 
		 
		 
		/*if(*index_s2!=END_CHAR) 
		{ 
			SBUF1=*index_s2; 
			index_s2++; 
		} 
		else 
		index_s2=&send_buf2[1];*/ 
	} 
} 
 
 
void MSG_ANALYSE() 
{ 
	uchar *p; 
	switch(R_msg_typ) 
	{ 
		case EOT: 
		error_t=0; 
		SM2=1; 
		break; 
		 
		case ENQ: 
		error_t=0; 
		time_20s=0; 
		com_fault=0; 
		index_ss=index_sp;//POINT TO NEW FRAME 
		if(index_sp!=index_se) 
		send(); 
		else send_c(EOT_BUF); 
		break; 
		 
		case ACK: 
		error_t=0; 
		index_ss=index_sp;//POINT TO NEW FRAME 
		ACK_R++; 
		if(index_sp==index_se) 
		send_c(EOT_BUF); 
		else send(); 
		break; 
		 
		case NAK: 
		error_t++;//index_sp=index_ss; 
		if(error_t<=2) 
		{ 
			index_sp=index_ss;//RESENT THIS FRAME   
			send(); 
		} 
		else 
		{ 
			send_c(EOT_BUF); 
			index_ss=index_sp;//CANCEL THIS FRAME 
			SM2=1; 
			error_t=0; 
		} 
		break; 
		 
		case MSG_ER: 
		if(msg_length>10) 
		send_c(NAK_BUF); 
		break; 
		 
		case MSG: 
		error_t=0; 
		get_buf(); 
		send_c(ACK_BUF); 
		ACK_S++; 
		 
		if(strncmp((msg_buf+4),"TEST-BEG",8)==0) 
		{ 
			p=msg_buf+0x0d; 
			strncpy1(CTAG,p,6); 
			 
			strncpy1(in_buf,"ACST:",5); 
			strncpy1(in_buf+5,CTAG,6); 
			strncpy1(in_buf+11," COMPLD;\x03",9); 
			push_buf(); 
			TEST_RESET(); 
		} 
		 
		break; 
	} 
} 
 
void ALM_INDICATE() 
{ 
	if(com_fault>com_fault_old) 
	{ 
		com_fault_old=com_fault; 
		com_f=0; 
	} 
	else if(com_fault=0x0f) 
		{ 
			tc_rb_2m_alm1_flag=tc_rb_2m_alm1=rb_2m_alm1=0; 
			flag_report_alm_cleared_rb1=1;//report the alm clear msg 
		} 
		 
		if(tc_rb_2m_alm2>=0x0f) 
		{ 
			tc_rb_2m_alm2_flag=tc_rb_2m_alm2=rb_2m_alm2=0; 
			flag_report_alm_cleared_rb2=1;//report the alm clear msg 
		} 
	}//one second 
	 
	if(flag_rbalm==0)//not all rb ref clk is invalid 
	{ 
		if(fpga_reset==0) 
		{ 
			if(counter_fpga) 
			{ 
				fpga_reset=1; 
				counter_fpga=0; 
				EX0=1; 
			} 
			else 
			counter_fpga++; 
		} 
	}//not all rb ref clk is invalid 
	 
	 
	 
} 
 
 
void init_80320() 
{ 
	EA=0; 
	SCON = 0xF0;////serial prot 0 baud rate for modem is timer1 or 2 equation//pase 
	SCON1 = 0x60;//serial prot 1 baud rate is timer1 equation 
	TI=0;//serial prot 0 transmitter interrupt clear 
	TI_1=0;//serial prot 1 transmitter interrupt clear 
	//T2MOD=0x02; 
	TMOD=0x21;//pase 
	TCON=0x51;//pase 
	T2CON=0x34; 
	TR2=1;//enable timer2 
	 
	TCLK=1; 
	RCLK=1;//serial prot 0 baud rate is timer2 equation 
	 
	T2=RCAP2 = 0xFFfc; //determaine timer2  baud rate for serial prot 0 
	 
	//CKCON|=0x10;//osc fre divided by 4   86400 
	TL1=0x0;//determaine timer1  baud rate for serial prot 1 9600 
	TH1=0xFd; 
	//````TL1=TH1=0xF7;//determaine timer1  baud rate for serial prot 1 9600 
	TR1=1;//enable timer1 
	 
	TL0=0x6f; 
	TH0=0x42; 
	TR0=1;//enable timer0 
	 
	EX0=1; 
	IT0=1; 
	//EX1=1;//enable EX pin interrupt 
	//IT0=0;//EX pin interrupt level triggered 
	//IT1=0; 
	ET0=1;//enable timer0 interrupt 
	ES=1; 
	ES1=1;//enable serial port interrupt 
	IE=0x53; 
	EWDI=0; 
	EWT=0; 
	/*PX0=1; 
	PS=1;// 
	PS1=1;*/ 
	IP=0x51; 
	//EXIF&=0xef; 
	  
	//EIE=0x01; 
//CKCON=0xc1; 
//WT=1; 
//WT=1; 
	EA =1;// interrupt enable 
} 
 
/*void ini_timer() 
{ 
 	IE=0; 
  	EA=0; 
 	TMOD=0x21; 
 	TCON=0xFF; 
  	TL0=TH0=0x00; //timer0 is uesed for times control 
  	TL1=TH1=0xff; //when serial 0 and 1 work at mode 3,baud rate equal to 86400Hz by timer1 
  	EA=1;                  //TR0=TR1=0; 
  	PCON&=0x3F; 
  	CKCON|=0x10; 
  	CKCON&=0xf7; 
  	SCON=0xF0; 
  	SCON1=0xF0;  	 
  	ES=1; 
  	ES1=1; 
  	ET0=1;       //allow timer0 interrupt 
}*/ 
void delay() 
{ 
	int j; 
	for(j=0;j<1000;j++) 
	{ 
		if(j==999) 
		runled=!runled; 
	} 
} 
 
 
void main() 
{ 
	for(n=0;n<200;n++)//power up wait the fpga download process 
	{ 
		delay(); 
	} 
	 
	ini_pointer(); 
	ini_variable(); 
	 
	init_80320(); 
	 
	/*while(10) 
	{ 
		byte_l[9]=XBYTE[27+address_l]; 
		byte_m[9]=XBYTE[27+address_m]; 
		byte_h[9]=XBYTE[27+address_h]; 
		n=0; 
	}*/ 
	/*while(flag_powerup) 
	{ 
		n=0; 
	}*/ 
		 
	while(1) 
	{ 
		/*temp_d[0]++; 
		if(temp_d[0]==10) 
		temp_d[0]=0;*/ 
		if (FLAG_RECV) 
		{ 
			FLAG_RECV=0; 
			MSG_ANALYSE(); 
		} 
		 
		alm_monitor();//report alm change command 5 
		ALM_INDICATE(); 
		 
		if(flag_interrupt_available) 
		{ 
			flag_interrupt_available=0; 
			PHASE_ORIGINAL_PROCESS(); 
		} 
		 
		REVT_RESET();//report command 6 
		 
		if(flag_report_100s) 
		{ 
			flag_report_100s=0; 
			R_TT_ALM();//100s alm status report command 2 
		} 
	}// 
}