

//2017.11.21
//解析出一个目标包之后, 不要忘了清除长度! 否则只能最大长度取余数等于0之后才会开始下一次监听, 导致丢失若干数据包
//length = 0;



	//加载芯片底层代码
	#include "$chip$.txt"
	
	public void OS_run()
	{
		tick + 1;
		if( tick > 20 ) {
			tick = 0;
			ok = false;
			notok = true;
		}
	}
	
	//=================================================================================
	//发送一个字节
	public void print_byte( int8 data )
	{
		WAIT_SEND_OK();
		UDR = (uint)data;
	}
	
	bool GNGGA;
	bool GNZDA;
	
	//数据接收中断
	INTERRUPT_UART
	void RXC()
	{
		//必须在中断程序中读取 UDR,否则在退出后会重新进入中断
		uint8 d = UDR;
		buffer[length] = d;
		length + 1;
		length % max_length;
		
		if( length == 1 ) {
			if( buffer[0] != '$' ) {
				length = 0;
				return;
			}
		}
		//判断协议头是否合法
		if( length == 6 ) {
			GNGGA = buffer[1] == 'G' && buffer[2] == 'N' && buffer[3] == 'G' && buffer[4] == 'G' && buffer[5] == 'A';
			GNZDA = buffer[1] == 'G' && buffer[2] == 'N' && buffer[3] == 'Z' && buffer[4] == 'D' && buffer[5] == 'A';
			if( !GNGGA && !GNZDA ) {
				length = 0;
				return;
			}
		}
		//判断是否协议结束
		if( d != '*' ) {
			return;
		}
		//判断当前是否为时间信息
		if( GNZDA ) {
			TIME();
		}
		//判断当前是否为经纬度信息
		if( GNGGA ) {
			NE();
		}
	}
	//---------------------------------------------------
	void TIME()
	{
		if( buffer[13] != '.' ) {
			return;
		}
		int32 temp;
		
		Year = 0;
		temp = (int)(buffer[24] - '0'); temp * 1000; Year + temp;
		temp = (int)(buffer[25] - '0'); temp * 100; Year + temp;
		temp = (int)(buffer[26] - '0'); temp * 10; Year + temp;
		temp = (int)(buffer[27] - '0'); Year + temp;
		
		Mouth = 0;
		temp = (int)(buffer[21] - '0'); temp * 10; Mouth + temp;
		temp = (int)(buffer[22] - '0'); Mouth + temp;
		
		Day = 0;
		temp = (int)(buffer[18] - '0'); temp * 10; Day + temp;
		temp = (int)(buffer[19] - '0'); Day + temp;
		
		Hour = 0;
		temp = (int)(buffer[7] - '0'); temp * 10; Hour + temp;
		temp = (int)(buffer[8] - '0'); Hour + temp;
		
		Minute = 0;
		temp = (int)(buffer[9] - '0'); temp * 10; Minute + temp;
		temp = (int)(buffer[10] - '0'); Minute + temp;
		
		Second = 0;
		temp = (int)(buffer[11] - '0'); temp * 10; Second + temp;
		temp = (int)(buffer[12] - '0'); Second + temp;
		
		Hour + 8;
		if( Hour >= 24 ) {
			Hour - 24;
			Day + 1;
			if( Day > get_day( (int8)(int16)Mouth ) ) {
				Day = 1;
				Mouth + 1;
				if( Mouth > 12 ) {
					Mouth = 1;
					Year + 1;
				}
			}
		}
		
		//不要忘了清除长度! 
		length = 0;
		
		OS_EventFlag.0(bit) = 1;
	}
	//---------------------------------------------------
	void NE()
	{
		if( buffer[28] != 'N' || buffer[41] != 'E' ) {
			return;
		}
		tick = 0;
		ok = true;
		notok = false;
		
		for( int8 i = 0; i < 9; i + 1 ) {
			Nbuffer[i] = buffer[i + 18];
		}
		for( int8 i = 0; i < 10; i + 1 ) {
			Ebuffer[i] = buffer[i + 30];
		}
		Nbuffer[9] = 0;
		Ebuffer[10] = 0;
		
		int32 temp;
		int32 temp2;
		
		N = 0;
		temp = (int)(buffer[18] - '0'); temp * 10000000; N + temp;
		temp = (int)(buffer[19] - '0'); temp * 1000000; N + temp;
		
		temp2 = 0;
		temp = (int)(buffer[20] - '0'); temp * 100000; temp2 + temp;
		temp = (int)(buffer[21] - '0'); temp * 10000; temp2 + temp;
		
		temp = (int)(buffer[23] - '0'); temp * 1000; temp2 + temp;
		temp = (int)(buffer[24] - '0'); temp * 100; temp2 + temp;
		temp = (int)(buffer[25] - '0'); temp * 10; temp2 + temp;
		temp = (int)(buffer[26] - '0'); temp2 + temp;
		temp2 * 5;
		temp2 / 3;
		N + temp2;
		
		E = 0;
		temp = (int)(buffer[30] - '0'); temp * 100000000; E + temp;
		temp = (int)(buffer[31] - '0'); temp * 10000000; E + temp;
		temp = (int)(buffer[32] - '0'); temp * 1000000; E + temp;
		
		temp2 = 0;
		temp = (int)(buffer[33] - '0'); temp * 100000; temp2 + temp;
		temp = (int)(buffer[34] - '0'); temp * 10000; temp2 + temp;
		
		temp = (int)(buffer[36] - '0'); temp * 1000; temp2 + temp;
		temp = (int)(buffer[37] - '0'); temp * 100; temp2 + temp;
		temp = (int)(buffer[38] - '0'); temp * 10; temp2 + temp;
		temp = (int)(buffer[39] - '0'); temp2 + temp;
		temp2 * 5;
		temp2 / 3;
		E + temp2;
		
		//不要忘了清除长度! 
		length = 0;
		
		OS_EventFlag.0(bit) = 1;
	}
	
	//计算指定月份的天数
	int8 get_day( int8 m )
	{
		switch( m ) {
			== 1:	return 31;
			== 2://2月闰年为29天
				if(Year%4==0) {
            			return 29;
       			}
				else {
            			return 28;
				}
			== 3:	return 31;
			== 4:	return 30;
			== 5:	return 31;
			== 6:	return 30;
			== 7:	return 31;
			== 8:	return 31;
			== 9:	return 30;
			== 10:return 31;
			== 11:return 30;
			== 12:return 31;
			default: return 0;
		}
	}
	
	
	
	

