📄 test_gps.cbackup2
字号:
/****************************************************************************
【文 件 名 称】TEST_GPS.C
【功 能 描 述】三星S3C2410A板demo程序代码
【程 序 版 本】4.0
【创建人及创建日期】icesoar//2003年11月19日19:26
【修改人及修改日期】icesoar//2004-12-8 17:25
****************************************************************************/
//***************************************************************************
#include "def.h"
#include "2410addr.h"
#include "config.h"
#include "board.h"
#include "utils.h"
#include "GPS.h"
extern char *strncpy(char * /*s1*/, const char * /*s2*/, unsigned int num /*n*/) ;
extern double atof(const char * /*nptr*/) ;
//***************************************************************************
GPS_INFO Gps_Infor;
char GPS_BUF[1024];
//***************************************************************************
/****************************************************************************
【功能说明】得到指定序号的逗号位置
****************************************************************************/
static int Get_Comma_Position( int num,char *str )
{
int i,j = 0 ;
int len = strlen(str) ;
for( i = 0; i < len; i++ )
{
if( str[i] == ',' ) j++ ;
if( j == num ) return (i+1) ;
}
return 0;
}
/****************************************************************************
【功能说明】
****************************************************************************/
static double Get_Double_Number( char *s )
{
char buf[128];
int i;
double rev;
i = Get_Comma_Position(1,s);
strncpy( buf, s, i ) ;
buf[i] = 0 ;
rev = atof(buf) ;
printf("s=%s ,buf= %s,val= %f\n",s,buf,rev);
return rev;
}
/****************************************************************************
【功能说明】将世界时间转换为北京时间
****************************************************************************/
static void UTC_To_BTC(date_time *GPS)
{
//如果秒号先出,再出时间数据,则将时间数据+1秒
GPS->second++; //加一秒
if(GPS->second>59)
{
GPS->second=0;
GPS->minute++;
if(GPS->minute>59)
{
GPS->minute=0;
GPS->hour++;
}
}
GPS->hour+=8; //小时加八
if(GPS->hour>23)
{
GPS->hour-=24;
GPS->day+=1;
if(GPS->month==2 ||
GPS->month==4 ||
GPS->month==6 ||
GPS->month==9 ||
GPS->month==11 )
{
if(GPS->day>30)
{
GPS->day=1;
GPS->month++;
}
}
else
{
if(GPS->day>31)
{
GPS->day=1;
GPS->month++;
}
}
if(GPS->year % 4 == 0 )
{
if(GPS->day > 29 && GPS->month ==2)
{
GPS->day=1;
GPS->month++;
}
}
else
{
if(GPS->day>28 &&GPS->month ==2)
{
GPS->day=1;
GPS->month++;
}
}
if(GPS->month>12)
{
GPS->month-=12;
GPS->year++;
}
}
}
#define MAX_TRASH_NUM (12)
/****************************************************************************
【功能说明】解释gps发出的数据
$GPGGA,014105.350,2232.9727,N,11355.2626,E,1,06,1.2,57.7,M,,,,0000*31
$GPGSA,A,3,28,20,08,02,23,13,,,,,,,2.0,1.2,1.6*35
$GPRMC,014105.350,A,2232.9727,N,11355.2626,E,0.00,,060405,,,A*79
$GPGSV,3,2,09,20,30,048,30,02,16,252,36,13,08,131,34,08,08,186,26*7F
****************************************************************************/
U8 GPS_Parse( char *line, GPS_INFO *GPS )
{
static int tmp, time ;
char c ;
char *buf = line ;
c = buf[5] ;
if( c == 'C' )//"GPRMC"
{
GPS->D.hour =(buf[ 7]-'0')*10+(buf[ 8]-'0');
GPS->D.minute =(buf[ 9]-'0')*10+(buf[10]-'0');
GPS->D.second =(buf[11]-'0')*10+(buf[12]-'0');
tmp = Get_Comma_Position(9,buf);
GPS->D.day =(buf[tmp+0]-'0')*10+(buf[tmp+1]-'0');
GPS->D.month =(buf[tmp+2]-'0')*10+(buf[tmp+3]-'0');
GPS->D.year =(buf[tmp+4]-'0')*10+(buf[tmp+5]-'0')+2000;
//------------------------------
GPS->status =buf[Get_Comma_Position(2,buf)];
GPS->latitude =Get_Double_Number(&buf[Get_Comma_Position(3,buf)]);
GPS->NS =buf[Get_Comma_Position(4,buf)];
GPS->longitude=Get_Double_Number(&buf[Get_Comma_Position(5,buf)]);
GPS->EW =buf[Get_Comma_Position(6,buf)];
#if ( USE_BEIJING_TIMEZONE )
UTC_To_BTC(&GPS->D);
#endif
return 1 ;
}
else if ( c == 'A' ) //"$GPGGA"
{
GPS->high = Get_Double_Number(&buf[Get_Comma_Position(9,buf)]);
return 1 ;
}
return 0 ;
}
/****************************************************************************
【功能说明】
****************************************************************************/
void Show_GPS( GPS_INFO *GPS )
{
printf("DATE : %ld-%02d-%02d \n",GPS->D.year,GPS->D.month,GPS->D.day);
printf("TIME : %02d:%02d:%02d \n",GPS->D.hour,GPS->D.minute,GPS->D.second);
// printf("Latitude : %.4f %c\n",GPS->latitude,GPS->NS);
// printf("Longitude : %.4f%c\n",GPS->longitude,GPS->EW);
// printf("Highness : %.4f \n",GPS->high);
printf("STATUS : %c\n",GPS->status);
}
/****************************************************************************
【功能说明】串口初始化
****************************************************************************/
void Uart_Initial( U8 uart, U16 baud )
{
U32 mclk = GetMasterClock();
if( uart == 0 )
{
//UART0
rUFCON0 = 0x0; //FIFO disable
rUMCON0 = 0x0;
rULCON0 = 0x3; //Normal,No parity,1 stop,8 bit
rUCON0 = 0x245; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
rUBRDIV0 = ((int)(mclk/16./baud + 0.5) -1);
}
else if( uart == 1 )
{
//UART1
rUFCON1 = 0x0; //FIFO disable
rUMCON1 = 0x0;
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1 = ((int)(mclk/16./baud + 0.5) -1);
}
}
/****************************************************************************
【功能说明】接收GPS模块的信息输入
****************************************************************************/
U8 Receive_GPS( void )
{
int i = 0 ;
char c ;
U32 time_out = 500000 ;
while( time_out-- )
{
SerialSwitch( 1 ) ; //选择UART通道为串口1
c = SerialRxChar(); //read from com port
SerialSwitch( 0 ) ; //选择UART通道为串口0
GPS_BUF[i++] = c ;
if( c == '\n' )
{
GPS_BUF[0]='$';
GPS_BUF[i]=0;
puts( GPS_BUF ) ;
return 1 ;
}
}
return 0 ;
}
/****************************************************************************
【功能说明】GPS模块测试程序
****************************************************************************/
void Test_GPS_Model( void )
{
printf( "GPS全球定位系统\n" ) ;
Uart_Initial( 1, 4800 ) ; //串口1初始化,波特率为4800
while( getkey() != ESC_KEY )
{
if( Receive_GPS() )
{
if ( GPS_Parse( GPS_BUF, &Gps_Infor ) ) ;
{
Delay( 10 ) ; //Delay some time
Show_GPS( &Gps_Infor ) ;
Delay( 20 ) ; //Delay some time
}
}
}
}
static char title[] = "GPS测试";
static char tip[] = "测试GPS全球定位系统,按ESC键返回";
//Test_GPS_ModelItem在prog_entry.c里被引用
TEST_PROGRAM_ITEM Test_GPS_ModelItem = {
(TEST_PROGRAM)Test_GPS_Model, //入口地址
title, //显示名称
tip, //帮助或提示信息,可为NULL
0}; //使用printf,puts,putch等函数时在LCD上也显示输出字符(串)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -