注意:
1.板子上UART2_TX与UART2_RX丝印层位置错误;
2.波特率默认9600,UART2_RX需要悬空
3.天线最好在室外
源代码在demo/gps文件夹下
串口1波特率115200,输出print NMEA infomation数据
串口2波特率9600,输出gps原始数据
1.程序入口
2.gps_MainTask()
3.gps_testTask()
void gps_testTask(void *pData)
{
GPS_Info_t* gpsInfo = Gps_GetInfo();
uint8_t buffer[300];
//wait for gprs register complete
//The process of GPRS registration network may cause the power supply voltage of GPS to drop,
//which resulting in GPS restart.
while(!flag)
{
Trace(1,"wait for gprs regiter complete");
OS_Sleep(2000);
}
//open GPS hardware(UART2 open either)
GPS_Init();
GPS_Open(NULL);
//wait for gps start up, or gps will not response command
while(gpsInfo->rmc.latitude.value == 0)
OS_Sleep(1000);
// set gps nmea output interval
for(uint8_t i = 0;i<5;++i)
{
bool ret = GPS_SetOutputInterval(10000);
Trace(1,"set gps ret:%d",ret);
if(ret)
break;
OS_Sleep(1000);
}
// if(!GPS_ClearInfoInFlash())
// Trace(1,"erase gps fail");
// if(!GPS_SetQzssOutput(false))
// Trace(1,"enable qzss nmea output fail");
// if(!GPS_SetSearchMode(true,false,true,false))
// Trace(1,"set search mode fail");
// if(!GPS_SetSBASEnable(true))
// Trace(1,"enable sbas fail");
if(!GPS_GetVersion(buffer,150))
Trace(1,"get gps firmware version fail");
else
Trace(1,"gps firmware version:%s",buffer);
// if(!GPS_SetFixMode(GPS_FIX_MODE_LOW_SPEED))
// Trace(1,"set fix mode fail");
if(!GPS_SetOutputInterval(1000))
Trace(1,"set nmea output interval fail");
Trace(1,"init ok");
while(1)
{
if(isGpsOn)
{
//show fix info
uint8_t isFixed = gpsInfo->gsa[0].fix_type > gpsInfo->gsa[1].fix_type ?gpsInfo->gsa[0].fix_type:gpsInfo->gsa[1].fix_type;
char* isFixedStr;
if(isFixed == 2)
isFixedStr = "2D fix";
else if(isFixed == 3)
{
if(gpsInfo->gga.fix_quality == 1)
isFixedStr = "3D fix";
else if(gpsInfo->gga.fix_quality == 2)
isFixedStr = "3D/DGPS fix";
}
else
isFixedStr = "no fix";
//convert unit ddmm.mmmm to degree(°)
int temp = (int)(gpsInfo->rmc.latitude.value/gpsInfo->rmc.latitude.scale/100);
double latitude = temp+(double)(gpsInfo->rmc.latitude.value - temp*gpsInfo->rmc.latitude.scale*100)/gpsInfo->rmc.latitude.scale/60.0;
temp = (int)(gpsInfo->rmc.longitude.value/gpsInfo->rmc.longitude.scale/100);
double longitude = temp+(double)(gpsInfo->rmc.longitude.value - temp*gpsInfo->rmc.longitude.scale*100)/gpsInfo->rmc.longitude.scale/60.0;
//you can copy ` latitude,longitude ` to http://www.gpsspg.com/maps.htm check location on map
snprintf(buffer,sizeof(buffer),"GPS fix mode:%d, BDS fix mode:%d, fix quality:%d, satellites tracked:%d, gps sates total:%d, is fixed:%s, coordinate:WGS84, Latitude:%f, Longitude:%f, unit:degree,altitude:%f",gpsInfo->gsa[0].fix_type, gpsInfo->gsa[1].fix_type,
gpsInfo->gga.fix_quality,gpsInfo->gga.satellites_tracked, gpsInfo->gsv[0].total_sats, isFixedStr, latitude,longitude,gpsInfo->gga.altitude);
//show in tracer
Trace(2,buffer);
//send to UART1
UART_Write(UART1,buffer,strlen(buffer));
UART_Write(UART1,"\r\n\r\n",4);
}
OS_Sleep(5000);
}
}
4.主要API
4.1. GPS_Open
bool GPS_Open(UART_Callback_t gpsReceivedCallback);
功能
开启GPS电源,GPS进入工作状态
参数
NULL
,则收到串口2收到GPS数据后不会产生回调,而是将GPS的数据以事件的方式发送给主任务;若不为NULL
,则不会产生事件,设置的串口回调函数会被调用,不要在处理函数中消耗太多时间。建议使用事件的方式,参考GPS例程返回值
4.2. GPS_Close
bool GPS_Close();
功能
关闭GPS电源
参数
无
返回值
是否成功关闭GPS