📄 gps.c
字号:
#include <string.h>
#include <stdlib.h>
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
#include "2410slib.h"
#include "option.h"
#include "GPS.h"
#include <stdio.h>
/*****************************************
函数名: GPS_Test
描 述: 设置串口初始参数,调用 GPS数据采集输出函数
返回值:void
参数: void
*****************************************/
void GPS_GGA(void)
{
int i, j, k;
char buffer[LOOP]; //reveive buffer
GGAINFO ggainfo;
char Degree[4];
char Minute[8];
char Hour[4];
char Minu[4];
char Second[4];
char Satnum[4];
int no = 0;
while(1)
{
i = 0;
for(i=0;i<LOOP;)
{
//UTRSTAT1 Receive buffer data ready [0] = 1 : The buffer register has a received data
while(rUTRSTAT1 & 0x1) //Receive data ready
{
if(i>(LOOP-1))
break;
buffer[i++] = RdURXH1();
}
}
for(j =0; j < LOOP; j++)
if((buffer[j]=='$')&(buffer[j+3]=='G')&(buffer[j+4]=='G')&(buffer[j+5]=='A'))
{
//UTRSTAT0 Receive buffer data ready [0] = 0 : Empty
//UTRSTAT0 Transmit buffer empty [1] = 1 : Empty
while(!(rUTRSTAT0 & 0x2)) //Wait until THR is empty.
Delay(10);
//Time:6-15
no = 0;
no = no + 6;
for(k=0;k<4;k++)
Hour[k]=0;
Hour[0] = buffer[no];
Hour[1] = buffer[no+1];
no += 2;
for(k=0;k<4;k++)
Minu[k] = 0;
Minu[0] = buffer[no];
Minu[0] = buffer[no+1];
no += 2;
for(k=0;k<4;k++)
Second[k] = 0;
Second[0] = buffer[no];
Second[1] = buffer[no];
ggainfo.hour = atoi(Hour);
ggainfo.min = atoi(Minu);
ggainfo.sec = atoi(Second);
ggainfo.secFrac = 0;
ggainfo.bjhour = ggainfo.hour + 8;
if(ggainfo.bjhour > 24)
ggainfo.bjhour -= 24;
//纬度 Latitude:17-25
no = 17;
for(k=0;k<4;k++)
Degree[k] = 0;
Degree[0] = buffer[no];
Degree[1] = buffer[no+1];
no += 2;
for(k=0;k<8;k++)
Minute[k] = 0;
for(k=0;k<7;k++)
Minute[k] = buffer[no+k];
ggainfo.latitude = (atof(Minute))/60.0 + atoi(Degree);
//南纬or北纬 NS:27
ggainfo.latNS= buffer[27];
//Longitude:29-38
no = 29;
for(k=0;k<4;k++)
Degree[k] = 0;
Degree[0] = buffer[no];
Degree[1] = buffer[no+1];
Degree[2] = buffer[no+2];
no += 3;
for(k=0;k<8;k++)
Minute[k] = 0;
for(k=0;k<7;k++)
Minute[k] = buffer[no+i];
ggainfo.longitude = (atof(Minute))/60.0 + atoi(Degree);
//东经or西经 :40
ggainfo.lgtEW = buffer[40];
//the number of satallite:44-45
no = 44;
for(k=0;k<4;k++)
Satnum[k] = 0;
Satnum[0] = buffer[no];
Satnum[1] = buffer[no+1];
ggainfo.satellites = atoi(Satnum);
Uart_Printf("afdasf\n");
//Uart_Printf("UTC Time: %2d:%2d:%2d\n", ggainfo.hour, ggainfo.min, ggainfo.sec);
//Uart_Printf("BJ Time: %2d:%2d:%2d\n",ggainfo.bjhour, ggainfo.min, ggainfo.sec);
//Uart_Printf("Latitude: %c wei %f\n", ggainfo.latNS, ggainfo.latitude);
//Uart_Printf("Longitude: %c jing %f\n", ggainfo.lgtEW, ggainfo.longitude);
//Uart_Printf("Satellites: %d\n", ggainfo.satellites);
}
}
}
void GPS_Test(void)
{
int pclk;
/*配置系统时钟*/
ChangeClockDivider(1,1); //1:2:4
//ChangeMPllValue(0xa1,0x3,0x1); //FCLK=202.8MHz
//Port_Init();
//Uart_Init(0,115200);
//Uart_Select(0);
pclk = PCLK;
//UFCON0 FIFO Enable [0 ] = 0 : Disable
//UFCON0 Rx FIFO Reset [1 ] = 0 : Normal
//UFCON0 Tx FIFO Reset [2 ] = 0 : Normal
//UFCON0 Rx FIFO Trigger Level [5:4] = 00: 4-byte
//UFCON0 Tx FIFO Trigger Level [7:6] = 00: Empty
rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable
//UFCON1 FIFO Enable [0 ] = 0 : Disable
//UFCON1 Rx FIFO Reset [1 ] = 0 : Normal
//UFCON1 Tx FIFO Reset [2 ] = 0 : Normal
//UFCON1 Rx FIFO Trigger Level [5:4] = 00: 4-byte
//UFCON1 Tx FIFO Trigger Level [7:6] = 00: Empty
rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable
//UMCON0 Request to Send [0 ] = 0 : 'H' level (Inactivate nRTS)
//UMCON0 Reserved [3:1] = 00: These bits must be 0's
//UMCON0 Auto Flow Control (AFC) [0 ] = 0 : Disable
//UMCON0 Reserved [7:6] = 00: Reserved
rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable
//UMCON0 Request to Send [0 ] = 0 : 'H' level (Inactivate nRTS)
//UMCON0 Reserved [3:1] = 00: These bits must be 0's
//UMCON0 Auto Flow Control (AFC) [0 ] = 0 : Disable
//UMCON0 Reserved [7:6] = 00: Reserved
rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable
//ULCON0 Word Length [1:0] = 11 : 8-bits
//ULCON0 Number of Stop Bit [ 2] = 0 : One stop bit per frame
//ULCON0 Parity Mode [5:3] = 0xx: No parity
//ULCON0 Infra-Red Mode [ 6] = 0 : Normal mode operation
rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
// [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0]
// Clock Sel, Tx Int, Rx Int, Rx Time Out, Rx err, Loop-back, Send break, Transmit Mode, Receive Mode
// 0 1 0 , 0 1 0 0 , 01 01
// PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling
rUCON0 = 0x05; // Control register
//UBRDIV0 Baud rate divisior register [15:0] = ( (int)(pclk/16./115200) -1 ) : Baud rate division value
rUBRDIV0=( (int)(pclk/16./115200) -1 ); //Baud rate divisior register 0
//ULCON0 Word Length [1:0] = 11 : 8-bits
//ULCON0 Number of Stop Bit [ 2] = 0 : One stop bit per frame
//ULCON0 Parity Mode [5:3] = 0xx: No parity
//ULCON0 Infra-Red Mode [ 6] = 0 : Normal mode operation
rULCON1 = 0x3;
// [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0]
// Clock Sel, Tx Int, Rx Int, Rx Time Out, Rx err, Loop-back, Send break, Transmit Mode, Receive Mode
// 0 1 0 , 0 1 0 0 , 01 01
// PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling
rUCON1 = 0x05;
//UBRDIV0 Baud rate divisior register [15:0] = ( (int)(pclk/16./9600) -1 ) : Baud rate division value
rUBRDIV1=( (int)(pclk/16./9600) -1 );
Uart_Printf("GPS TEST\n");
//GPS_GGA();
//GPS_Get_GPRMC();
GPS_Get_GPGGA();
}
//========================================
// 函数名: GPS_Get_GPRMC
// 描 述: GPS数据串口采集输出,获取GPRMC的信息
// 时间和经纬度信息。
// 返回值:void
// 参数 : void
//========================================
void GPS_Get_GPRMC(void)
{
char ch[LOOP];
int i = 0;
int j = 0;
int bb;
char Degree[4];
char Minute[2];
char Hour[2];
char Minu[4];
char Second[2];
char Satnum[4];
extern int k, h, m, s;
extern int lonz,lonx,latz,latx;
Uart_Printf("=====GPS TEST=====\n");
Uart_Printf("PRESS ANY KEY TO STOP.\n");
while(!Uart_GetKey())
{
i = 0;
for(i=0;i<LOOP;)
{
//UTRSTAT1 Receive buffer data ready [0] = 1 : The buffer register has a received data
while(rUTRSTAT1 & 0x1) //Receive data ready
{
if(i>(LOOP-1))
break;
ch[i++] = RdURXH1();
}
}
for(j =0; j < LOOP; j++)
if((ch[j]=='$')&(ch[j+5]=='C')&((j+70)<LOOP))
{
//UTRSTAT0 Receive buffer data ready [0] = 0 : Empty
//UTRSTAT0 Transmit buffer empty [1] = 1 : Empty
while(!(rUTRSTAT0 & 0x2)) //Wait until THR is empty.
Delay(10);
k=j;
h=(ch[k+7]-48)*10+(ch[k+8]-48); //string to int
m=(ch[k+9]-48)*10+(ch[k+10]-48);
s=(ch[k+11]-48)*10+(ch[k+12]-48);
lonz=(ch[k+16]-48)*1000+(ch[k+17]-48)*100+(ch[k+18]-48)*10+(ch[k+19]-48);
lonx=(ch[k+21]-48)*1000+(ch[k+22]-48)*100+(ch[k+23]-48)*10+(ch[k+24]-48);
latz=(ch[k+28]-48)*10000+(ch[k+29]-48)*1000+(ch[k+30]-48)*100+(ch[k+31]-48)*10+(ch[k+32]-48);
latx=(ch[k+34]-48)*1000+(ch[k+35]-48)*100+(ch[k+36]-48)*10+(ch[k+37]-48);
Uart_Printf("time: %2d:%2d:%2d\n", h+8,m,s);
//Uart_Printf("Latitude: %d.%d%d%c Longtitude: %d.%d%d%c\n",lonz/100,lonz%100,lonx,ch[k+26],latz/100,latz%100,latx,ch[k+39]);//时间校正为北京时间
}
}
Uart_Printf(" TEST FINISHED.\n");
}
//*********************************************************
//函数名:GPS_Get_GPGGA
//描述:获取GPGGA格式的信息,从中取得时间,经纬度,海拔高度,使用卫星数量等信息
//返回值:void
//参数:void
//*********************************************************
void GPS_Get_GPGGA(void)
{
char ch[LOOP];
char buf[15];
char *hour;
char *minutes;
char *seconds;
char *latitude;
char *longtitude;
int i = 0;
int j = 0;
int p;
extern int k, h, m, s;
extern int lonz,lonx,latz,latx;
Uart_Printf("=====GPS TEST=====\n");
Uart_Printf("PRESS ANY KEY TO STOP.\n");
while(!Uart_GetKey())
{
i = 0;
for(i=0;i<LOOP;)
{
//UTRSTAT1 Receive buffer data ready [0] = 1 : The buffer register has a received data
while(rUTRSTAT1 & 0x1) //Receive data ready
{
if(i>(LOOP-1))
break;
ch[i++] = RdURXH1();
}
}
for(j =0; j < LOOP; j++)
if((ch[j]=='$')&(ch[j+3]=='G')&(ch[j+4]=='G')&(ch[j+5]=='A'))
{
//UTRSTAT0 Receive buffer data ready [0] = 0 : Empty
//UTRSTAT0 Transmit buffer empty [1] = 1 : Empty
while(!(rUTRSTAT0 & 0x2)) //Wait until THR is empty.
Delay(10);
k=j;
h=(ch[k+7]-48)*10+(ch[k+8]-48); //string to int
m=(ch[k+9]-48)*10+(ch[k+10]-48);
s=(ch[k+11]-48)*10+(ch[k+12]-48);
for(p=0;p<2;p++)
{ hour[p]=ch[k+7+p];}
for(p=0;p<2;p++)
{ minutes[p]=ch[k+9+p];}
for(p=0;p<2;p++)
{ seconds[p]=ch[k+11+p];}
//sprintf(buf,"%d",s);
Uart_Printf("%s\n",buf);
lonz=(ch[k+16]-48)*1000+(ch[k+17]-48)*100+(ch[k+18]-48)*10+(ch[k+19]-48);
lonx=(ch[k+21]-48)*1000+(ch[k+22]-48)*100+(ch[k+23]-48)*10+(ch[k+24]-48);
latz=(ch[k+28]-48)*10000+(ch[k+29]-48)*1000+(ch[k+30]-48)*100+(ch[k+31]-48)*10+(ch[k+32]-48);
latx=(ch[k+34]-48)*1000+(ch[k+35]-48)*100+(ch[k+36]-48)*10+(ch[k+37]-48);
sprintf(buf,"%d",lonz/100);
Uart_Printf("%s\n",buf);
Uart_Printf("time: %2d:%2d:%2d\n", h+8,m,s);
Uart_Printf("Latitude: %d.%d%d%c Longtitude: %d.%d%d%c\n",lonz/100,lonz%100,lonx,ch[k+26],latz/100,latz%100,latx,ch[k+39]);//时间校正为北京时间
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -