STC15W单片机解析GPS数据1

上回提到的程序问题如下:

STC15W单片机解析GPS数据1_第1张图片

修改后的程序:

STC15W单片机解析GPS数据1_第2张图片

我用的GPS模块是泥人电子的,如图:

STC15W单片机解析GPS数据1_第3张图片

 详细的模块介绍请查看用户手册http://pan.baidu.com/s/1bo9ME2F

串口接收到的数据:

$GNGGA,091012.000,3145.614703,N,11955.717186,E,2,9,1.11,19.327,M,6.667,M,,*4E
$GPGSA,A,3,28,02,04,193,03,09,,,,,,,1.76,1.11,1.36*3A
$GLGSA,A,3,72,65,76,,,,,,,,,,1.76,1.11,1.36*1C
$GPGSV,4,1,14,28,68,192,29,17,61,357,,50,52,166,35,19,46,324,14*73
$GPGSV,4,2,14,193,43,136,37,03,41,055,22,06,40,266,18,22,18,044,*40
$GPGSV,4,3,14,02,13,256,20,01,11,054,,04,09,110,23,09,06,143,30*7E
$GPGSV,4,4,14,30,03,197,23,24,01,305,*73
$GLGSV,3,1,09,87,45,344,,77,42,250,,72,39,109,41,71,26,049,20*6D
$GLGSV,3,2,09,88,26,298,,78,25,314,,86,23,058,,65,21,157,26*61
$GLGSV,3,3,09,76,16,196,32*55
$GNRMC,091012.000,A,3145.614703,N,11955.717186,E,0.00,278.47,010520,,,D*75
$GNVTG,278.47,T,,M,0.00,N,0.00,K,D*28

现在的问题是我只关心GNGGA的数据,其它数据我不需要,如果全部接收将占用单片机大量内存,以上为例全部接收需要800个字节的RAM。而我关心的数据只要80多个字节。所以我的想法是在串口中断接收时就对GPS模块发来的数据进行筛选,接收程序如下。

//GPS接受到的数据
//$GNGGA,115700.000,3145.613605,N,11955.723543,E,2,11,0.99,31.426,M,6.667,M,,*79
//$GPGSA,A,3,06,05,17,19,09,193,02,,,,,,1.31,0.99,0.86*32
//$GLGSA,A,3,66,88,67,87,,,,,,,,,1.31,0.99,0.86*1D
//$GPGSV,3,1,11,193,70,110,24,06,57,031,19,19,56,135,41,02,53,328,17*4D
//$GPGSV,3,2,11,50,52,166,36,05,44,244,27,09,36,068,16,17,36,140,40*7B
//$GPGSV,3,3,11,12,30,281,18,25,13,308,,04,11,042,*47
//$GLGSV,2,1,08,66,60,192,38,88,59,056,25,65,48,036,,81,36,335,*62
//$GLGSV,2,2,08,87,31,104,38,67,09,204,21,72,07,029,,79,05,278,*6C
//$GNRMC,115700.000,A,3145.613605,N,11955.723543,E,0.00,341.10,010520,,,D*7F
//$GNVTG,341.10,T,,M,0.00,N,0.00,K,D*21
/********************* UART2中断函数************************/
void UART2_int (void) interrupt UART2_VECTOR
{
	if(RI2)
	{
		CLR_RI2();
		if(COM2.B_RX_OK == 0)
		{
			if(COM2.RX_Cnt >= COM_RX2_Lenth)	COM2.RX_Cnt = 0;
			temp=S2BUF;
			RX2_Buffer[COM2.RX_Cnt++] = temp;
			//关注的数据如下,通过判断字符串的第2个和第5个字符来筛选除数据
			//$GNGGA,115700.000,3145.613605,N,11955.723543,E,2,11,0.99,31.426,M,6.667,M,,*79			
			if((RX2_Buffer[2]=='N')&&(RX2_Buffer[5]=='A'))//
			{
				RcvStart=1;//表明接收到$GNGGA开始的字符串
			}
	
			if((RcvStart==1)&&(temp=='\n'))//已经完整接收了$GNGGA开始的字符串
			{
				RcvStart=0;
				S2CON &= ~(1<<4);	//禁止串口2接收
				RcvFlag=1;//接收完成$GNGGA字符串,通知串口1发送数据
			}
			COM2.RX_TimeOut = TimeOutSet2;
		}
	}
	if(TI2)
	{
		CLR_TI2();
		if(COM2.TX_read != COM2.TX_write)
		{
		 	S2BUF = TX2_Buffer[COM2.TX_read];
			if(++COM2.TX_read >= COM_TX2_Lenth)		COM2.TX_read = 0;
		}
		else	COM2.B_TX_busy = 0;
	}

}

 运行效果

STC15W单片机解析GPS数据1_第4张图片

整个程序框架选用STC官网的串口例程 STC15官网例程文件

STC15W单片机解析GPS数据1_第5张图片

我的GPS接收程序,对官方例程进行部分修改,有兴趣的可以对照官网例程查看。

另外 float_to_string好像有问题,我没有修改,请有能力的修改下并及时与大家分享!

/* 浮点数转换为字符串,包括整数转换为字符串
*  intgr指定整数位个数,dec指定小数位个数
*  自动去除前面的0,小数点后面的0不会舍去
*/
 unsigned char float_to_string(double value, char* pdest, unsigned int intgr, unsigned int dec)

你可能感兴趣的:(单片机,STC15,GPS)