#include "STC12.h" //STC12C5A60S2,1T单片机,11.0592Mhz
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//sbit E=P2^7; //1602使能端
//sbit RW=P2^6; //1602读写选择
//sbit RS=P2^5; //
bit ReceivingF = 0; //开始接收标志
bit EndF = 0; //结束标志
bit RMCF = 0; //$GPRMC
bit GGAF = 0; //$GPGGA
bit GGA_END = 0; //$GPVTG
bit RMC_END = 0; //$GPGSA
bit NewByteF;
uchar GPS_time[9]; //UTC时间
uchar GPS_wd[12]; //纬度
uchar GPS_jd[13]; //经度
uchar GPS_warn; //定位警告
uchar GPS_quality; //定位质量
uchar GPS_status; //定位状态
uchar GPS_alt[8]; //海拔
uchar GPS_sv[3]; //使用卫星
uchar GPS_speed[10]; //速度
uchar GPS_date[9]; //UTC日期
uchar Segment; //逗号计数
uchar Bytes_counter;
uchar Command;
uchar num = 0; //读取次数
void GPRMC(uchar);
void GPGGA(uchar);
//void GPVTG(uchar);
//void GPGSA(uchar);
void GPS(uchar);
/*void delay_10ms(uint del) //延时10ms*del
{
uint i,j;
for(i=0; i<del; i++)
for(j=0; j<1827; j++)
;
}*/
/*void delay()
{
int i,j;
for(i=0; i<=10; i++)
for(j=0; j<=2; j++)
;
}*/
/*
void enable(uchar del)
{
P0 = del;
RS = 0;
RW = 0;
E = 0;
delay();
E = 1;
delay();
}
void write(uchar del)
{
P0 = del;
RS = 1;
RW = 0;
E = 0;
delay();
E = 1;
delay();
}
void L1602_init(void)
{
enable(0x01);
enable(0x38);
enable(0x0c);
enable(0x06);
enable(0xd0);
}
void L1602_string(uchar hang,uchar lie,uchar *p)
{
uchar a;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
enable(a);
while(1)
{
if(*p == ' ') break;
write(*p);
p++;
}
}
*/
void Com1_init(void)
{ PCON &= 0x7f; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xfe; //串口1选择定时器1为波特率发生器
TMOD &= 0x0f; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xDC; //设定定时初值
TH1 = 0xDC; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}
/*void Com2_Init(void) //9600bps@11.0592MHz
{
AUXR &= 0xf7; //波特率不倍速
S2CON = 0x50; //8位数据,可变波特率
BRT = 0xDC; //设定独立波特率发生器重装值
AUXR |= 0x04; //独立波特率发生器时钟为Fosc,即1T
AUXR |= 0x10; //启动独立波特率发生器
}*/
void SendChar(unsigned char d)
{
SBUF=d;
while(!TI); //等待发送完成,TI置位
TI=0;
}
/*发送字符串子程序,可以直接发字符串(未必是数组)*/
void SendMsg(unsigned char msg[])
{ int i=0;
while(msg[i] != ' ')
{
SBUF = msg[i]; //SUBF接受/发送缓冲器(又叫串行通信特殊功能寄存器)
while(!TI); // 等特数据传送 (TI发送中断标志)
TI = 0; // 清除数据传送标志
i++;
// 下一个字符
}
i=0;
}
char RecChar ()
{
while(!RI); //RI接受中断标志
RI=0; //清除RI接受中断标志
//SUBF接受/发送缓冲器
return SBUF;
}
/*用串口2发送字符串子程序,可以直接发字符串(未必是数组)*/
/*void SendMsg(unsigned char msg[])
{ int i=0;
while(msg[i] != ' ')
{
S2BUF = msg[i]; //SUBF接受/发送缓冲器(又叫串行通信特殊功能寄存器)
while(!(S2CON&0x02)); // 等特数据传送 (TI发送中断标志)
S2CON &= ~0x02; // 清除数据传送标志
i++; // 下一个字符
}
i=0;
}*/
/*void GPS_interrupt() interrupt 4
{
if(RI == 1)
{
EA = 0;
GPS(SBUF);
}
RI = 0;
EA = 1;
}*/
void GPS(uchar tmp)
{
if(tmp == '$')//起始标志
{
Command = 0;
ReceivingF = 1;
Bytes_counter = 0;
Segment = 0; //清空语句段计数器
return;
}
if(ReceivingF)
{
if(tmp == ',')
{
++Segment;
Bytes_counter = 0; //清空段字节计数器
return;
}
if(tmp == '*')//收到结束标志
{
ReceivingF = 0;
if(GGAF==1){GGA_END=1;GGAF=0;}
if(RMCF==1){RMC_END=1;RMCF=0;}
EndF = 1;
return;
}
if(Segment == 0)
{
if(Bytes_counter == 3) //段0,语句类型判断
switch(tmp)
{
case 'G':Command = 1;//语句类型 $GPGGA
GGAF = 1;
break;
case 'M':Command = 2;//语句类型 $GPRMC
RMCF = 1;
break;
// case 'T':Command = 3;//语句类型 $GPVTG
//VTGF = 1;
break;
// case 'S':break;
default:Command = 0; //非有效数据类型,终止当前数据接收
ReceivingF = 0;
break;
}
/*if(Bytes_counter == 4)
if(Command==0 && tmp=='A')
{
Command = 4;//语句类型 $GPGSA
GSAF = 1;
}*/
}
else
{
switch(Command)
{
case 1:GPGGA(tmp);
break;
case 2:GPRMC(tmp);
break;
//case 3:GPVTG(tmp);
break;
//case 4:GPGSA(tmp);
break;
default:break;
}
}
++Bytes_counter;
}
NewByteF = 0;
return;
}
void GPGGA(uchar tmp)
{
switch(Segment)
{
case 1: if(Bytes_counter == 2 || Bytes_counter == 5) //$GPGGA段1,UTC时间,hhmmss(时分秒)格式,取前6位 转换为HH:MM:SS格式
{
GPS_time[Bytes_counter] = ':';
++Bytes_counter;
}
if(Bytes_counter<8)
GPS_time[Bytes_counter] = tmp;
GPS_time[8] = ' ';
//num++;
break;
case 2: if(Bytes_counter == 3) //$GPGGA 段2处理 纬度ddmm.mmmm(度分)格式
{
GPS_wd[Bytes_counter] = '.'; //接收第二个字节后插入'.'
++Bytes_counter;
GPS_wd[11] = ' ';
}
if(Bytes_counter == 0)
++Bytes_counter;
GPS_wd[Bytes_counter] = tmp;
//num++;
break;
case 3: GPS_wd[0] = tmp; //$GPGGA第3段处理 纬度半球N(北半球)或S(南半球)
//num++;
break;
case 4: if(Bytes_counter == 4) //$GPGGA 段4处理 经度dddmm.mmmm(度分)格式
{
GPS_jd[Bytes_counter] = '.'; //接收第3个字节后插入'.'
++Bytes_counter;
GPS_jd[12] = ' ';
}
if(Bytes_counter == 0)
++Bytes_counter;
GPS_jd[Bytes_counter] = tmp;
//num++;
break;
case 5: GPS_jd[0] = tmp; //$GPGGA第5段处理 经度半球E(东经)或W(西经)
//num++;
break;
case 6: GPS_quality = tmp;
//num++;
break;
case 7: if(Bytes_counter<2) //$GPGGA第7段处理 正在使用解算位置的卫星数量(00~12)(前面的0也将被传输)
GPS_sv[Bytes_counter] = tmp;
GPS_sv[2] = ' ';
//num++;
break;
case 9: if(Bytes_counter<7) //$GPGGA第9段处理 海拔高度(-9999.9~99999.9)
{
GPS_alt[Bytes_counter] = tmp;
GPS_alt[Bytes_counter+1] = ' ';
}
//num++;
break;
default:break;
}
}
void GPRMC(uchar tmp)
{
switch(Segment)
{ case 2: GPS_status = tmp;
//num++; //$GPRMC第二段,A有效V无效
break;
case 7:if(Bytes_counter < 7)//地面速率(0000.0~1851.8公里/小时,前面的0也将被传输)
{
GPS_speed[Bytes_counter] = tmp;
}
//num++;
break;
case 9: if(Bytes_counter<2) //$GPRMC第9段处理 UTC日期,ddmmyy(日月年)格式转换为yy-mm-dd
{
GPS_date[6+Bytes_counter] = tmp;
}
if(Bytes_counter>1 && Bytes_counter<4)//月
{
GPS_date[1+Bytes_counter] = tmp;
GPS_date[5] = '-';
}
if(Bytes_counter>3 && Bytes_counter<6)//年
{
GPS_date[Bytes_counter-4] = tmp;
GPS_date[2] = '-';
GPS_date[8] = ' ';
}
//num++;
break;
default:break;
}
}
/*void GPGSA(uchar tmp)
{
switch(Segment)
{
case 2: GPS_status = tmp; //$GPGSA第2段处理 定位类型,1=没有定位,2=2D定位,3=3D定位
break;
default:break;
}
}
void GPVTG(uchar tmp)
{
switch(Segment)
{
case 7:if(Bytes_counter < 7)//地面速率(0000.0~1851.8公里/小时,前面的0也将被传输)
{
GPS_speed[Bytes_counter] = tmp;
GPS_speed[Bytes_counter+1] = '$';//这里4个字符是Km/h
GPS_speed[Bytes_counter+2] = '"';
GPS_speed[Bytes_counter+3] = '/';
GPS_speed[Bytes_counter+4] = '#';
GPS_speed[Bytes_counter+5] = ' ';
}
break;
default:break;
}
}*/
void show()
{SendMsg("Date: ");
SendMsg(GPS_date);
SendChar(' ');
SendMsg("Time: ");
SendMsg(GPS_time);
SendChar(' ');
SendMsg("Latitude: ");
SendMsg(GPS_wd);
SendChar(' ');
SendMsg("Longitude: ");
SendMsg(GPS_jd);
SendChar(' ');
SendMsg("Altitude: ");
SendMsg(GPS_alt);
SendChar('m');
SendChar(' ');
SendMsg("Speed: ");
SendMsg(GPS_speed);
SendMsg("kts ");
SendChar(' ');
SendMsg("Status: ");
if(GPS_status=='A')
{SendMsg("Location active ");}
else{SendMsg("Location void ");}
SendMsg("
");
}
void main()
{
//L1602_init();
//L1602_string(1,1,"GPS initializing");
//L1602_string(2,1,"Please wait");
//delay_10ms(100);
/*while(num)
{
L1602_string(2,12,".");
delay_10ms(100);
L1602_string(2,13,".");
delay_10ms(100);
L1602_string(2,14,".");
delay_10ms(100);
L1602_string(2,12," ");
L1602_string(2,13," ");
L1602_string(2,14," ");
delay_10ms(100);
num--;
}
L1602_string(1,1," ");
L1602_string(2,1," ");
*/
Com1_init();
while(1)
{ while(!((GGA_END==1)&&(RMC_END==1)))
{GPS(RecChar());
}
GGA_END=0;
RMC_END=0;
show();
//SendMsg(GPS_date);
//GPS(RecChar());
/*uchar GPS_time[9]; //UTC时间
uchar GPS_wd[12]; //纬度
uchar GPS_jd[13]; //经度
uchar GPS_warn; //定位警告
uchar GPS_quality; //定位质量
uchar GPS_status; //定位状态
uchar GPS_alt[8]; //海拔
uchar GPS_sv[3]; //使用卫星
uchar GPS_speed[10]; //速度
uchar GPS_date[9]; //UTC日期
L1602_string(1,1,GPS_wd);
L1602_string(2,1,GPS_jd);
L1602_string(2,14," ");
delay_10ms(400);
L1602_string(1,1," ");
L1602_string(2,1," ");
L1602_string(1,1,"Date:");
L1602_string(2,1,"Time:");
L1602_string(1,6,GPS_date);
L1602_string(2,6,GPS_time);
delay_10ms(400);
L1602_string(1,1," ");
L1602_string(2,1," ");
L1602_string(1,1,"speed:");
L1602_string(2,1,"alt:");
L1602_string(1,7,GPS_speed);
L1602_string(2,5,GPS_alt);
L1602_string(2,10,"m");
delay_10ms(400);
L1602_string(1,1," ");
L1602_string(2,1," ");*/
}
}
一周热门 更多>