⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps.c

📁 GPS开发
💻 C
字号:
//======================================================
// 文件名称:	GPS.c
// 功能描述:	GPS模块的初始化,GPS信息的解析
// 维护记录:	2000-12-5	v1.0
//======================================================
#include "gps.h"
#include "spce061a.h"
#include <string.h>

static int GetComma(int num,char* str);
static void UTC2BTC(date_time *GPS);
static double Get_Double_Number(char *s);
static void   Get_Char(char *s);

char buf_t[128];
//=============================================================
//语法格式:	void GPS_Init(void)
//实现功能:	GPS模块初始化
//参数:		无
//返回值:		无
//说明:			模块初始化成功后,GPS模组上的指示灯开始闪烁
//=============================================================
void Init_GPS(void)
{
	int i;
	*P_IOB_Dir |= 0x0100;
	*P_IOB_Attrib |= 0x0100;
	*P_IOB_Buffer= *P_IOB_Buffer && (~0x0100);
	for(i = 0;i < 1000;i ++);
	*P_IOB_Buffer= *P_IOB_Buffer || 0x0100;
}

//======================================//
// 语法格式:int GPS_Parse(char *line, GPS_INFO *GPS)  
// 实现功能:把gps模块的信息解析为可识别的数据
// 参    数:存放原始信息字符数组、存储可识别数据的结构体
// 返 回 值:布尔量
// 说明:
//0      7  0   4 6   0     6 8 0        90         0  3      0        9  	
//$GPRMC,091400,A,3958.9870,N,11620.3278,E,000.0,000.0,120302,005.6,W*62	
//       7 
//$GPGGA,091400,3958.9870,N,11620.3278,E,1,03,1.9,114.2,M,-8.3,M,,*5E
//======================================//
int GPS_Parse(char *line, GPS_INFO *GPS)  
{
	int tmp;
	char b,c;
	char* buf = line;
	b = buf[4];
	c = buf[5];

	if(c == 'C'){																//"GPRMC"
		GPS->D.hour   = (buf[7] - '0') * 10 + (buf[8] - '0');					//时间
		GPS->D.minute = (buf[9] - '0') * 10 + (buf[10] - '0');
		GPS->D.second = (buf[11] - '0') * 10 + (buf[12] - '0');
		tmp = GetComma(9, buf);
		GPS->D.day    = (buf[tmp + 0] - '0') * 10 + (buf[tmp + 1] - '0');		//日期
		GPS->D.month  = (buf[tmp + 2] - '0') * 10 + (buf[tmp + 3] - '0');
		GPS->D.year   = (buf[tmp + 4] - '0') * 10 + (buf[tmp + 5] - '0')+2000;
	 	Get_Char(&buf[7]);
	    
	    strcpy(GPS->D.time_c, buf_t); 
		//------------------------------
		GPS->status	   =buf[GetComma(2, buf)];									//有效标志位
		GPS->latitude  =Get_Double_Number(&buf[GetComma(3, buf)]);				//纬度
		GPS->NS        =buf[GetComma(4, buf)];
		Get_Char(&buf[GetComma(3, buf)]);
        
       	GPS->latitude_Degree=(int)GPS->latitude / 100;       					//分离纬度
		GPS->latitude_Cent = GPS->latitude - GPS->latitude_Degree * 100;
		
		GPS->longitude  =Get_Double_Number(&buf[GetComma( 5, buf)]);			//经度
		GPS->EW       =buf[GetComma(6, buf)];
		Get_Char(&buf[GetComma(5, buf)]);
		
		GPS->longitude_Degree=(int)GPS->longitude / 100;      
		GPS->longitude_Cent = GPS->longitude - GPS->longitude_Degree * 100;
		
		UTC2BTC(&GPS->D);														//转化为北京时间
	}

	if(c == 'G'){                        										//$GPGGA,091400,3958.9870,N,11620.3278,E,1,03,1.9,114.2,M,-8.3,M,,*5E		
		        GPS->high     = Get_Double_Number(&buf[GetComma(9, buf)]);		//时间
		        GPS->D.hour   = (buf[7] - '0') * 10 + (buf[8] - '0');
				GPS->D.minute = (buf[9] - '0') * 10 + (buf[10] - '0');
				GPS->D.second = (buf[11] - '0') * 10 + (buf[12] -'0');
				UTC2BTC(&GPS->D);
	          }	
	return 0;
}

//======================================//
// 语法格式: static double Str_To_Double(char *buf)
// 实现功能: 把一个字符串转化成浮点数
// 参    数:字符串
// 返 回 值:转化后双精度值
//======================================//
static double Str_To_Double(char *buf)
{
	double rev = 0;
	double data;
	int integer = 1;
	char *str = buf;
	int i;
	while(*str != '\0')
	{
		switch(*str)
		{
			case '0':
				data = 0;
				break;
			case '1':
				data = 1;
				break;
			case '2':
				data = 2;
				break;		
			case '3':
				data = 3;
				break;
			case '4':
				data = 4;
				break;
			case '5':
				data = 5;
				break;
			case '6':
				data = 6;
				break;
			case '7':
				data = 7;
				break;
			case '8':
				data = 8;
				break;
			case '9':
				data = 9;
				break;
			case '.':
				data = '.';
				break;
		}
		if(data == '.')
		{
			integer = 0;
			i = 1;
			str ++;
			continue;
		}
		if( integer == 1 )
		{
			rev = rev * 10 + data;
		}
		else														//小数部分
		{
			rev = rev + data / (10 * i);
			i = i * 10 ;
		}
		str ++;
	}                                                                //while
	return rev;
}
												
//======================================//
// 语法格式: static double Get_Double_Number(char *s)
// 实现功能: 把给定字符串第一个逗号之前的字符转化成双精度型
// 参    数:字符串
// 返 回 值:转化后双精度值
//======================================//
static double Get_Double_Number(char *s)
{
	char buf[128];
	int i;
	double rev;
	i=GetComma(1, s);
	i = i - 1;
	strncpy(buf, s, i);
	buf[i] = 0;
	rev=Str_To_Double(buf);
	return rev;	
}

//======================================//
// 语法格式:Get_Char()
// 实现功能:得到形参第一个逗号前的字符串
// 参    数:需要查找的字符串
// 返 回 值:得到的字符串
//======================================//

static void Get_Char(char *s)
{
	
	int i;
	i = GetComma(1, s);
	i = i - 1;
	if(i > 9)i = 9;
	strncpy(buf_t, s, i);
	buf_t[i] = '\n';
}

//======================================//
// 函数名称:GetComma()
// 实现功能:计算字符串中各个逗号的位置
// 参    数: 查找的逗号是第几个的个数,需要查找的字符串
// 返 回 值:0
//======================================//
static int GetComma(int num,char *str)
{
	int i,j = 0;
	int len=strlen(str);
	for(i = 0;i < len;i ++)
	{
		if(str[i] == ',')j ++;
		if(j == num)return i + 1;	
	}
	return 0;	
}

//======================================//
// 语法格式:void UTC2BTC(data_time *GPS)
// 实现功能:转化时间为北京时区的时间
// 参    数:存放时间的结构体
// 返 回 值:无
//======================================//
static void UTC2BTC(date_time *GPS)
{
		GPS->second ++;  
		if(GPS->second > 59){
			GPS->second = 0;
			GPS->minute ++;
			if(GPS->minute > 59){
				GPS->minute = 0;
				GPS->hour ++;
			}
		}	
                                                       //***********转化为北京时间******************
         GPS->hour = GPS->hour + 8;
		if(GPS->hour > 23)
		{
			GPS->hour -= 24;
			GPS->day += 1;
			if(GPS->month == 2 ||
			   		GPS->month == 4 ||
			   		GPS->month == 6 ||
			   		GPS->month == 9 ||
			   		GPS->month == 11 ){
				if(GPS->day > 30){
			   		GPS->day = 1;
					GPS->month++;
				}
			}
			else{
				if(GPS->day > 31){	
			   		GPS->day = 1;
					GPS->month ++;
				}
			}
			if(GPS->year % 4 == 0 ){//
		   		if(GPS->day > 29 && GPS->month == 2){		
		   			GPS->day = 1;
					GPS->month ++;
				}
			}
			else{
		   		if(GPS->day > 28 &&GPS->month == 2){
		   			GPS->day = 1;
					GPS->month ++;
				}
			}
			if(GPS->month > 12){
				GPS->month -= 12;
				GPS->year ++;
			}		
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -