單片機解析GPS數(shù)據(jù)算法
硬件平臺:XC-GPS開發(fā)板+XC-STC單片機開發(fā)板
效果如下:
首先創(chuàng)建一個GPS數(shù)據(jù)結(jié)構(gòu)體:
- typedef data struct{
double latitude; //經(jīng)度 double longitude; //緯度 int latitude_Degree; //度 int latitude_Cent; //分 int latitude_Second; //秒 int longitude_Degree; //度 int longitude_Cent; //分 int longitude_Second; //秒 float speed; //速度 float direction; //航向 float height; //海拔高度 int satellite; U8 NS; U8 EW; DATE_TIME D; - }GPS_INFO;
時間結(jié)構(gòu)體:
- typedef struct{
int year; int month; int day; int hour; int minute; int second; - }DATE_TIME;
核心算法就是解析GPRMC數(shù)據(jù),得到經(jīng)緯度,日期時間,速度,航向:
- int
GPS_RMC_Parse(char *line, GPS_INFO *GPS) - {
U8 ch, status, tmp; float lati_cent_tmp, lati_second_tmp; float long_cent_tmp, long_second_tmp; float speed_tmp; char *buf = line; ch = buf[5]; status = buf[GetComma(2, buf)]; if (ch == C) //如果第五個字符是C,($GPRMC) { if (status == A) //如果數(shù)據(jù)有效,則分析 { GPS->NS = buf[GetComma(4, buf)]; GPS->EW = buf[GetComma(6, buf)]; GPS->latitude = Get_Double_Number(&buf[GetComma(3, buf)]); GPS->longitude = Get_Double_Number(&buf[GetComma(5, buf)]); GPS->latitude_Degree = (int)GPS->latitude / 100; //分離緯度 lati_cent_tmp = (GPS->latitude - GPS->latitude_Degree * 100); GPS->latitude_Cent = (int)lati_cent_tmp; lati_second_tmp = (lati_cent_tmp - GPS->latitude_Cent) * 60; GPS->latitude_Second = (int)lati_second_tmp; GPS->longitude_Degree = (int)GPS->longitude / 100; //分離經(jīng)度 long_cent_tmp = (GPS->longitude - GPS->longitude_Degree * 100); GPS->longitude_Cent = (int)long_cent_tmp; long_second_tmp = (long_cent_tmp - GPS->longitude_Cent) * 60; GPS->longitude_Second = (int)long_second_tmp; speed_tmp = Get_Float_Number(&buf[GetComma(7, buf)]); //速度(單位:海里/時) GPS->speed = speed_tmp * 1.85; //1海里=1.85公里 GPS->direction = Get_Float_Number(&buf[GetComma(8, 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); 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; UTC2BTC(&GPS->D); return 1; } } return 0; - }
line是串口接收的一行數(shù)據(jù)buf
GetComma函數(shù)作用是一行數(shù)據(jù)中第幾個逗號后面那個字符在這行數(shù)據(jù)中的位置
Get_Double_Number函數(shù)作用是把給定字符串第一個逗號之前的字符轉(zhuǎn)化成雙精度型,在這里就是把代表經(jīng)度和緯度的字符串轉(zhuǎn)換成數(shù)字,同樣的函數(shù)還有Get_Float_Number
UTC2BTC函數(shù)是將世界時間轉(zhuǎn)換成北京時間(相差8小時)
在LCD顯示程序中把GPS_INFO結(jié)構(gòu)體的已經(jīng)被賦值的變量顯示到屏上相應的位置即可
還有一個GPGGA信息段可以提供海拔高度和衛(wèi)星數(shù)量信息
- int
GPS_GGA_Parse(char *line, GPS_INFO *GPS) - {
U8 ch, status; char *buf = line; ch = buf[4]; status = buf[GetComma(2, buf)]; if (ch == G) //$GPGGA { if (status != ,) { GPS->height = Get_Float_Number(&buf[GetComma(9, buf)]); GPS->satellite = Get_Int_Number(&buf[GetComma(7, buf)]); return 1; } } return 0; - }
評論