<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

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

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

          作者: 時間:2016-11-26 來源:網(wǎng)絡(luò) 收藏
          單片機(jī)解析GPS數(shù)據(jù)非常簡單,只要串口接收到一行字符串,傳到解析函數(shù)里,再把解析到的字符串經(jīng)過運(yùn)算轉(zhuǎn)成我們需要數(shù)據(jù)。
          硬件平臺:XC-GPS開發(fā)板+XC-STC單片機(jī)開發(fā)板
          效果如下:
          首先創(chuàng)建一個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;
          時間結(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)緯度,日期時間,速度,航向:
          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)//如果第五個字符是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)]);//速度(單位:海里/時)
          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);//時間
          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ù)中第幾個逗號后面那個字符在這行數(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)被賦值的變量顯示到屏上相應(yīng)的位置即可
          還有一個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. }



          評論


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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();