GPS (time, longitude, latitude)

Dependencies:   mbed

Committer:
wasaco
Date:
Sat Apr 25 03:29:54 2015 +0000
Revision:
1:c9f46190f418
Parent:
0:03a95da99c41
GPS yomuyo!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wasaco 0:03a95da99c41 1 #include "mbed.h"
wasaco 0:03a95da99c41 2
wasaco 0:03a95da99c41 3 Serial pc(SERIAL_TX, SERIAL_RX); // tx, rx
wasaco 0:03a95da99c41 4 Serial gps(PA_9, PA_10);// tx, rx
wasaco 0:03a95da99c41 5
wasaco 0:03a95da99c41 6 int main() {
wasaco 0:03a95da99c41 7
wasaco 1:c9f46190f418 8 int i,rlock,stn;
wasaco 0:03a95da99c41 9 char gps_data[256];
wasaco 0:03a95da99c41 10 char ns,ew;
wasaco 0:03a95da99c41 11 float time,hokui,tokei;
wasaco 1:c9f46190f418 12 float g_hokui,g_tokei;
wasaco 1:c9f46190f418 13 float d_hokui,m_hokui,d_tokei,m_tokei;
wasaco 1:c9f46190f418 14 int h_time,m_time,s_time;
wasaco 0:03a95da99c41 15
wasaco 0:03a95da99c41 16 gps.baud(4800);
wasaco 0:03a95da99c41 17 pc.printf("gps start! \r\n");
wasaco 0:03a95da99c41 18
wasaco 0:03a95da99c41 19
wasaco 0:03a95da99c41 20 while (1) {
wasaco 0:03a95da99c41 21 i=0;
wasaco 0:03a95da99c41 22 while(gps.getc()!='$'){
wasaco 0:03a95da99c41 23 }
wasaco 0:03a95da99c41 24
wasaco 0:03a95da99c41 25 while( (gps_data[i]=gps.getc()) != '\r'){
wasaco 0:03a95da99c41 26 i++;
wasaco 0:03a95da99c41 27 if(i==256){
wasaco 0:03a95da99c41 28 pc.printf("*** Div Error! ***\r\n");
wasaco 0:03a95da99c41 29 i=255;
wasaco 0:03a95da99c41 30 break;
wasaco 0:03a95da99c41 31 }
wasaco 0:03a95da99c41 32 }
wasaco 0:03a95da99c41 33 gps_data[i]='\0';
wasaco 0:03a95da99c41 34
wasaco 1:c9f46190f418 35 /* test data
wasaco 1:c9f46190f418 36 ---- GPS 12:24:43 Lock(1),Stn(6) ----
wasaco 1:c9f46190f418 37 Hokui:35.031105
wasaco 1:c9f46190f418 38 Tokei:135.773605
wasaco 1:c9f46190f418 39
wasaco 1:c9f46190f418 40 ---- GPS 12:24:44 Lock(1),Stn(6) ----
wasaco 1:c9f46190f418 41 Hokui:35.031105
wasaco 1:c9f46190f418 42 Tokei:135.773605
wasaco 1:c9f46190f418 43 */
wasaco 1:c9f46190f418 44
wasaco 1:c9f46190f418 45 if( sscanf(gps_data, "GPGGA,%f,%f,%c,%f,%c,%d,%d",&time,&hokui,&ns,&tokei,&ew,&rlock,&stn) >= 1){
wasaco 0:03a95da99c41 46 if(rlock==1){
wasaco 1:c9f46190f418 47 //latitude
wasaco 1:c9f46190f418 48 d_hokui = int(hokui/100);
wasaco 1:c9f46190f418 49 m_hokui = (hokui - d_hokui*100)/60;
wasaco 1:c9f46190f418 50 g_hokui = d_hokui + m_hokui;
wasaco 1:c9f46190f418 51 //longitude
wasaco 1:c9f46190f418 52 d_tokei = int(tokei/100);
wasaco 1:c9f46190f418 53 m_tokei = (tokei - d_tokei*100)/60;
wasaco 1:c9f46190f418 54 g_tokei=d_tokei + m_tokei;
wasaco 1:c9f46190f418 55 // g_hokui=int(hokui/100)+(hokui-int(hokui/100))/60;
wasaco 1:c9f46190f418 56 // g_tokei=int(tokei/100)+(tokei-int(tokei/100))/60;
wasaco 1:c9f46190f418 57
wasaco 1:c9f46190f418 58 //time set
wasaco 1:c9f46190f418 59 h_time = int(time/10000);
wasaco 1:c9f46190f418 60 m_time = int((time - h_time*10000)/100);
wasaco 1:c9f46190f418 61 s_time = int(time - h_time*10000 - m_time*100);
wasaco 1:c9f46190f418 62 h_time=h_time+9;//UTC =>JST
wasaco 1:c9f46190f418 63
wasaco 1:c9f46190f418 64 pc.printf("---- GPS %2d:%2d:%2d ",h_time,m_time,s_time);
wasaco 1:c9f46190f418 65 pc.printf("Lock(%d),Stn(%d) ----\r\n",rlock,stn);
wasaco 1:c9f46190f418 66 // latitude
wasaco 1:c9f46190f418 67 pc.printf("Hokui:%4.6f\r\n",g_hokui);
wasaco 1:c9f46190f418 68 // longitude
wasaco 1:c9f46190f418 69 pc.printf("Tokei:%4.6f\r\n\n",g_tokei);
wasaco 1:c9f46190f418 70
wasaco 0:03a95da99c41 71
wasaco 0:03a95da99c41 72 }
wasaco 0:03a95da99c41 73 else{
wasaco 0:03a95da99c41 74 pc.printf("Status:unLock(%d)\r\n",rlock);
wasaco 0:03a95da99c41 75 for(i=0;i<40;i++){
wasaco 0:03a95da99c41 76 pc.printf("%c",gps_data[i]);
wasaco 0:03a95da99c41 77 }
wasaco 0:03a95da99c41 78 }
wasaco 0:03a95da99c41 79 }//if
wasaco 0:03a95da99c41 80 }//while
wasaco 0:03a95da99c41 81 }//main
wasaco 0:03a95da99c41 82