亚龙电子工作室 
 
 
 
基于 51 单片机解析 GPS
数据的实验程序 
(附有效果图) 
作者:朱亚龙 
2010 
Y l d z 8 8 8 @ y a h o . c o m . c n  
1  基于 51 单片机解析 GPS 数据的实验程序 
 
//***用 51 单片机解析 GPS 数据并在 12864 液晶显示经纬度、时钟以及高度的源代码程序 
//***邮箱:    yldz888@yahoo.com.cn                                                                                                                                       
//***作者:  zhuyalong                                                                                               
//***时间:  2010/6/16                                                                                                                                                                                                                                                                                                                                           
//【声明】此程序仅用于学习与参考,引用请注明版权和作者信息! 
#include 
#define uchar unsigned char 
#define uint unsigned int 
 
sbit duan=P2^6; 
sbit wei=P2^7; 
sbit lcd_en=P3^4; 
sbit lcd_rs=P3^5; 
sbit lcd_rw=P3^6; 
sbit lcd_psb=P3^7; 
uchar a,b,c,d,i,flag; 
uchar table0[62]; 
void delay(uint z); 
void write_com(uchar com); 
void write_date(uchar date); 
void init(); 
void main() 
{ 
init(); 
write_com(0x90); 
for(a=0;a<17;a++)//局部定义数组并写入液晶(局部定义极大的节省 RAM) 
 
uchar table[]="亚龙电子欢迎您!"; 
write_date(table[a]); 
{ 
 
 
} 
delay(5000); 
write_com(0x01);//清屏 
write_com(0x90);//局部定义数组并写入液晶 
for(a=0;a<16;a++) 
{ 
 
 
uchar table1[]="GPS  信号接收中"; 
 
write_date(table1[a]); 
} 
write_com(0x8b);//局部定义数组并写入液晶 
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2  基于 51 单片机解析 GPS 数据的实验程序 
for(a=0;a<10;a++) 
 
{ 
 
 
} 
uchar table2[]={"请稍后…………"}; 
write_date(table2[a]); 
delay(6000); 
ES=1; 
while(1) 
 
{ 
 
 
 
 
 
 
if(flag==1) 
{ 
 
 
 
 
 
if(table0[4]=='G')//确定是否收到"GPGGA"这一帧数据 
{ 
 
 
 
 
d++; 
if((d>=0)&&(d<=15))//前 15 秒显示经纬度 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
//以下给液晶写经纬度内容 
{ 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
uchar table3[]="纬度:"; 
uchar table4[]="经度:"; 
if(d==1) 
 
write_com(0x01);//显示之前清屏 
write_com(0x80); 
for(a=0;a<7;a++) 
 
write_date(table3[a]); 
 
 
 
write_com(0x88); 
for(a=0;a<7;a++) 
 
write_date(table4[a]); 
write_com(0x92); 
for(a=0;a<11;a++) 
 
write_date(table0[a+18]); 
 
 
write_com(0x9a); 
for(a=0;a<12;a++) 
 
write_date(table0[a+30]); 
ES=1; 
flag=0; 
} 
if((d>=16)&&(d<=30))//后 15 秒显示时间和高度 
{ 
 
 
 
 
 
 
 
 
uchar temp1,temp2,shi_1,shi_2; 
uchar code sj[]="0123456789"; 
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3  基于 51 单片机解析 GPS 数据的实验程序 
 
uchar table5[]="时间:"; 
uchar table6[]="高度:"; 
uchar GD[8]; 
a=0; 
for(b=0;b<62;b++)//这里完成高度数据分离 
 
 
 
 
 
 
 
 
 
 
 
 
 
{ 
 
 
 
 
 
 
 
 
 
 
 
} 
if(table0[b]==',') 
 
 
 
 
 
 
 
 
 
 
{ 
 
 
 
 
 
 
 
 
} 
a++; 
if(a==9) 
 
 
 
 
 
 
{ 
for(c=0;c<8;c++) 
GD[c]=table0[b+c+1];// 将 分 离 的 高
{ 
 
} 
 
 
 
} 
if(d==30) 
d=0; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
度内容写入高度数组 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
//以下给液晶写高度内容 
if(d==16)  
write_com(0x01);//显示之前清屏 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
位'M' 
 
 
 
write_com(0x80); 
for(a=0;a<6;a++) 
 
write_date(table5[a]); 
write_com(0x88); 
for(a=0;a<6;a++) 
 
write_date(table6[a]); 
 
 
 
 
 
 
 
write_com(0x98+2); 
for(a=0;a<9;a++) 
 
 
 
 
 
 
{ 
 
 
 
 
 
if(GD[a]==',') 
{ 
 
 
} 
a=9; 
write_date('M');//无数据提前退出写入同时结尾补写单
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483 
 
4  基于 51 单片机解析 GPS 数据的实验程序 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
else 
 
write_date(GD[a]); 
 
 
 
 
 
} 
switch(table0[7])//分离字符串 
{ 
 
 
 
 
 
 
 
 
 
 
case '0': 
temp1=0; 
break; 
case '1': 
temp1=1; 
break; 
case '2': 
temp1=2; 
break; 
} 
switch(table0[8])//分离字符串 
{ 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
case '0': 
temp2=0; 
break; 
case '1': 
temp2=1; 
break; 
case '2': 
temp2=2; 
break; 
case '3': 
temp2=3; 
break; 
case '4': 
temp2=4; 
break; 
case '5': 
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483 
 
5  基于 51 单片机解析 GPS 数据的实验程序 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
temp2=5; 
break; 
case '6': 
temp2=6; 
break; 
case '7': 
temp2=7; 
break; 
case '8': 
temp2=8; 
break; 
case '9': 
temp2=9; 
break; 
 
} 
c=temp1*10+temp2;//时差转换(即转换为北京时间) 
if(c>=16,c<=23) 
{ 
 
 
} 
shi_1=0; 
shi_2=c-16; 
if(c>=2,c<=15) 
{ 
 
 
 
} 
c+=8; 
shi_1=c/10; 
shi_2=c%10; 
if(c>=0,c<=1) 
{ 
 
 
shi_1=0; 
shi_2=c+8; 
 
 
 
 
} 
//以下给液晶写时间内容 
 
 
 
 
 
 
 
 
 
 
 
 
 
write_com(0x90); 
write_date(sj[shi_1]); 
write_com(0x90+1); 
 
 
 
 
 
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483 
 
6  基于 51 单片机解析 GPS 数据的实验程序 
write_date(sj[shi_2]); 
 
write_com(0x90+2); 
write_date(':'); 
write_com(0x90+3); 
write_date(table0[9]); 
write_com(0x90+4); 
write_date(table0[10]); 
write_com(0x90+5); 
write_date(':'); 
write_com(0x90+6); 
write_date(table0[11]); 
write_com(0x90+7); 
write_date(table0[12]); 
ES=1; 
flag=0; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
} 
else 
ES=1; 
 
flag=0; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
} 
void write_com(uchar com)//写指令函数 
{ 
 
 
 
 
 
 
 
 
lcd_rs=0; 
lcd_rw=0; 
lcd_en=0; 
P0=com; 
delay(5); 
lcd_en=1; 
delay(5); 
lcd_en=0; 
} 
void write_date(uchar date)//写数据函数 
{ 
 
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483 
 
7  基于 51 单片机解析 GPS 数据的实验程序 
 
 
 
 
 
 
 
 
 
lcd_rs=1; 
lcd_rw=0; 
lcd_en=0; 
P0=date; 
delay(5); 
lcd_en=1; 
delay(5); 
lcd_en=0; 
} 
void init()//初始化液晶、接收中断方式 
{ 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
duan=0; 
wei=0; 
i=0; 
lcd_psb=1; 
write_com(0x30); 
delay(5); 
write_com(0x0c); 
delay(5); 
write_com(0x01); 
delay(5); 
TMOD=0x20; 
TH1=0xfa; 
TL1=0xfa; 
SM0=0; 
SM1=1; 
REN=1; 
EA=1; 
TR1=1; 
 
} 
void delay(uint z)//延时函数 
{ 
 
 
 
uint x,y; 
for(x=z;x>0;x--) 
 
for(y=110;y>0;y--); 
} 
void ser()interrupt 4//串口中断、初次分离'$'开头的字符串,并写入接收数组 
亚龙电子工作室   
E-mail:yldz888@yahoo.com.cn 
QQ:369485483