#include "includes.h"
unsigned char UTCTime[6]; //UTC时间
unsigned char Latitude[9]; //纬度
unsigned char Longitude[10]; //经度
unsigned char LatAngle; //纬度方向
unsigned char LongAngle; //经度方向
unsigned char GPS_warn; //定位警告
unsigned char GPS_quality; //定位质量
unsigned char UseSat[2]; //使用卫星
unsigned char PosStatus; //定位状态
unsigned char Speed[5]; //速度 单位:节
unsigned char UTCDate[6]; //UTC日期
unsigned char GPSAltitude[8]; //海拔高度
unsigned char TotalSat[2]; //总共卫星数
unsigned char Angle[5]; //方位角
//unsigned char DisplayCounter; //刷新计数器
unsigned char SegCounter; //逗号计数器
unsigned char ByteCounter; //字符计数器
unsigned char CmdType; //命令类型
unsigned char ReceiveMode; //接受命令模式 0:结束模式 1:命令模式 2:数据模式
unsigned char BufFull; //整句接收 1:G-GPGGA整句接受完成,2:S-GPGSV整句接受完成 3:M-GPRMC整句接受完成
unsigned char Cmd; //GPS协议命令类型,G-GPGGA;S-GPGSV;M-GPRMC
unsigned char CurrentDay; //当前时区日
unsigned char CurrentMonth; //当前时区月
unsigned char CurrentYear; //当前时区年
unsigned char CurrentTimeZone = 8; //设置当前时区
unsigned char CurrentTime; //设置当前时间
///////////////计算并返回2月的天数
unsigned char FebDays(unsigned char year) //闰年返回29,平年返回28
{
year+=2000;
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
return 29;
else
return 28;
}
else return 29;
}
else return 28;
}
void AdjustDate() //日期校正
{
unsigned char day;//读取的GPS日寄存器
unsigned char month;//读取的GPS月寄存器
unsigned char year;//读取的GPS年寄存器
unsigned char hour; //设置当前小时
CurrentTime = (UTCTime[0] - 48)*10 + (UTCTime[1] - 48);
CurrentTime = (CurrentTime + CurrentTimeZone)%24;
//CurrentTime = 8;
day = ((UTCDate[0] - 48)*10 + (UTCDate[1] - 48));
month = ((UTCDate[2] - 48)*10 + (UTCDate[3] - 48));
year = ((UTCDate[4] - 48)*10 + (UTCDate[5] - 48));
hour = ((UTCTime[0] - 48)*10 + (UTCTime[1] - 48));
if((hour + CurrentTimeZone) >24)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day < 31)
{
CurrentDay = day + 1;
CurrentMonth = month;
CurrentYear = year;
}
else
{
CurrentMonth = month;
CurrentYear = year;
if(month < 12)
{
CurrentMonth = month + 1;
}
else
{
CurrentDay = 1;
CurrentMonth = 1;
CurrentYear = year + 1;
}
}
break;
case 4:
case 6:
case 9:
case 11:
if(day < 30)
{
CurrentDay = day + 1;
CurrentMonth = month;
CurrentYear = year;
}
else
{
CurrentDay = 1;
CurrentMonth = month +1;
CurrentYear = year;
}
break;
case 2:
if(day < FebDays(year))
{
CurrentDay = day + 1;
CurrentMonth = month;
CurrentYear = year;
}
else
{
CurrentDay = 1;
CurrentMonth = month + 1;
CurrentYear = year;
}
break;
default:
break;
}
}
else
{
CurrentYear = year;
CurrentMonth = month;
CurrentDay = day;
}
}
#pragma vector=USART0RX_VECTOR
__interrupt void usart0_rx (void)
{
unsigned char tmp;
tmp = RXBUF0; //将SBUF换成缓冲寄存器
switch(tmp)
{
case '$':
CmdType = 0x00; //命令类型清空
ReceiveMode = 0x01; //接收命令模式
ByteCounter = 0x00; //接收位数清空
SegCounter = 0x00;
break;
case ',':
SegCounter++;
ByteCounter = 0x00;
break;
default:
if(ReceiveMode == 0x01)
{
//命令种类判断
if(ByteCounter == 3)
{
Cmd = tmp; //存储协议类型,G-GPGGA;S-GPGSV;M-GPRMC
}
else if(ByteCounter > 3) //判断协议头第四位
{
if(Cmd == 'G') //GPGGA
{
CmdType = 0x01;
ReceiveMode = 0x02;
//SegCounter = 0x00;
ByteCounter = 0x00;
}
else if(Cmd == 'S' & tmp == 'V') //GPGSV
{
if(Cmd)
CmdType = 0x02;
ReceiveMode = 0x02;
//SegCounter = 0x00;
ByteCounter = 0x00;
}
else if(Cmd == 'M') //GPRMC
{
CmdType = 0x03;
ReceiveMode = 0x02;
//SegCounter = 0x00;
ByteCounter = 0x00;
}
}
}
else if(ReceiveMode == 0x02)
{
//接收数据处理
switch(CmdType)
{
case 1: //类型1,数据接收——GPGGA
switch(SegCounter)
{
case 2: //纬度处理
if(ByteCounter < 9)
{
Latitude[ByteCounter] = tmp;
}
break;
case 3: //纬度方向处理
if(ByteCounter < 1)
{
LatAngle = tmp;
}
break;
case 4://经度处理
if(ByteCounter < 10)
{
Longitude[ByteCounter] = tmp;
}
break;
case 5: //经度方向处理
if(ByteCounter <1)
{
LongAngle = tmp;
}
break;
case 6://定位判断
if(ByteCounter <1)
{
PosStatus = tmp;
}
break;
case 7://定位使用卫星数
if(ByteCounter <2)
{
UseSat[ByteCounter] = tmp;
}
break;
case 9://高度处理
if(ByteCounter <6)
{
GPSAltitude[ByteCounter] = tmp;
}
break;
case 10:
ReceiveMode = 0x00;
BufFull = 0x01;
break;
}
break;
case 2: //类型2,数据接收——GPGSV
switch(SegCounter)
{
case 3:
if(ByteCounter < 2)
{
TotalSat[ByteCounter
- 1
- 2
前往页