新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)解析GPS數(shù)據(jù)算法

單片機(jī)解析GPS數(shù)據(jù)算法

作者: 時(shí)間:2016-11-26 來(lái)源:網(wǎng)絡(luò) 收藏
單片機(jī)解析GPS數(shù)據(jù)非常簡(jiǎn)單,只要串口接收到一行字符串,傳到解析函數(shù)里,再把解析到的字符串經(jīng)過(guò)運(yùn)算轉(zhuǎn)成我們需要數(shù)據(jù)。
硬件平臺(tái):XC-GPS開(kāi)發(fā)板+XC-STC單片機(jī)開(kāi)發(fā)板
效果如下:
首先創(chuàng)建一個(gè)GPS數(shù)據(jù)結(jié)構(gòu)體:
  1. typedef data struct{
  2. double latitude;//經(jīng)度
  3. double longitude;//緯度
  4. intlatitude_Degree;//度
  5. intlatitude_Cent;//分
  6. intlatitude_Second;//秒
  7. intlongitude_Degree;//度
  8. intlongitude_Cent;//分
  9. intlongitude_Second;//秒
  10. float speed;//速度
  11. float direction;//航向
  12. float height;//海拔高度
  13. intsatellite;
  14. U8 NS;
  15. U8 EW;
  16. DATE_TIME D;
  17. }GPS_INFO;
時(shí)間結(jié)構(gòu)體:
  1. typedef struct{
  2. intyear;
  3. intmonth;
  4. intday;
  5. inthour;
  6. intminute;
  7. intsecond;
  8. }DATE_TIME;
核心算法就是解析GPRMC數(shù)據(jù),得到經(jīng)緯度,日期時(shí)間,速度,航向:
  1. intGPS_RMC_Parse(char*line,GPS_INFO*GPS)
  2. {
  3. U8 ch,status,tmp;
  4. float lati_cent_tmp,lati_second_tmp;
  5. float long_cent_tmp,long_second_tmp;
  6. float speed_tmp;
  7. char*buf=line;
  8. ch=buf[5];
  9. status=buf[GetComma(2,buf)];
  10. if(ch==C)//如果第五個(gè)字符是C,($GPRMC)
  11. {
  12. if(status==A)//如果數(shù)據(jù)有效,則分析
  13. {
  14. GPS->NS=buf[GetComma(4,buf)];
  15. GPS->EW=buf[GetComma(6,buf)];
  16. GPS->latitude=Get_Double_Number(&buf[GetComma(3,buf)]);
  17. GPS->longitude=Get_Double_Number(&buf[GetComma(5,buf)]);
  18. GPS->latitude_Degree=(int)GPS->latitude/100;//分離緯度
  19. lati_cent_tmp=(GPS->latitude-GPS->latitude_Degree*100);
  20. GPS->latitude_Cent=(int)lati_cent_tmp;
  21. lati_second_tmp=(lati_cent_tmp-GPS->latitude_Cent)*60;
  22. GPS->latitude_Second=(int)lati_second_tmp;
  23. GPS->longitude_Degree=(int)GPS->longitude/100;//分離經(jīng)度
  24. long_cent_tmp=(GPS->longitude-GPS->longitude_Degree*100);
  25. GPS->longitude_Cent=(int)long_cent_tmp;
  26. long_second_tmp=(long_cent_tmp-GPS->longitude_Cent)*60;
  27. GPS->longitude_Second=(int)long_second_tmp;
  28. speed_tmp=Get_Float_Number(&buf[GetComma(7,buf)]);//速度(單位:海里/時(shí))
  29. GPS->speed=speed_tmp*1.85;//1海里=1.85公里
  30. GPS->direction=Get_Float_Number(&buf[GetComma(8,buf)]);//角度
  31. GPS->D.hour=(buf[7]-0)*10+(buf[8]-0);//時(shí)間
  32. GPS->D.minute=(buf[9]-0)*10+(buf[10]-0);
  33. GPS->D.second=(buf[11]-0)*10+(buf[12]-0);
  34. tmp=GetComma(9,buf);
  35. GPS->D.day=(buf[tmp+0]-0)*10+(buf[tmp+1]-0);//日期
  36. GPS->D.month=(buf[tmp+2]-0)*10+(buf[tmp+3]-0);
  37. GPS->D.year=(buf[tmp+4]-0)*10+(buf[tmp+5]-0)+2000;
  38. UTC2BTC(&GPS->D);
  39. return 1;
  40. }
  41. }
  42. return 0;
  43. }
line是串口接收的一行數(shù)據(jù)buf
GetComma函數(shù)作用是一行數(shù)據(jù)中第幾個(gè)逗號(hào)后面那個(gè)字符在這行數(shù)據(jù)中的位置
Get_Double_Number函數(shù)作用是把給定字符串第一個(gè)逗號(hào)之前的字符轉(zhuǎn)化成雙精度型,在這里就是把代表經(jīng)度和緯度的字符串轉(zhuǎn)換成數(shù)字,同樣的函數(shù)還有Get_Float_Number
UTC2BTC函數(shù)是將世界時(shí)間轉(zhuǎn)換成北京時(shí)間(相差8小時(shí))
在LCD顯示程序中把GPS_INFO結(jié)構(gòu)體的已經(jīng)被賦值的變量顯示到屏上相應(yīng)的位置即可
還有一個(gè)GPGGA信息段可以提供海拔高度和衛(wèi)星數(shù)量信息
  1. intGPS_GGA_Parse(char*line,GPS_INFO*GPS)
  2. {
  3. U8 ch,status;
  4. char*buf=line;
  5. ch=buf[4];
  6. status=buf[GetComma(2,buf)];
  7. if(ch==G)//$GPGGA
  8. {
  9. if(status!=,)
  10. {
  11. GPS->height=Get_Float_Number(&buf[GetComma(9,buf)]);
  12. GPS->satellite=Get_Int_Number(&buf[GetComma(7,buf)]);
  13. return 1;
  14. }
  15. }
  16. return 0;
  17. }



評(píng)論


技術(shù)專區(qū)

關(guān)閉