按照bg4uvr 的程序做的gps解码显示。取消了时间现实。增加了高度显示
应用在这里。
http://bbs.5imx.com/bbs/viewthread.php?tid=208151 就不单独贴图了
现在还有点问题请教一下。
高度显示正常。但是速度的转换有问题。开始我在用串口发送数据。调试km显示正常。但是用我的gps就不行了。仔细看了一下gps的数据发现问题
调试是用的数据:只看着一句$gprmc,024520.00,a,3153.7225,n,12111.9951,e,010.0,000.0,280107,04.7,w,a*19
速度的格式为010.0 速度为10海里转换后显示38km正常
但是我的gps输出为:$gprmc,072035.244,a,4425.2204,n,08454.3392,e,0.00,,260108,,,a*79
格式为:10.00 也是10海里。显示为204km。
程序如下:由于空间不足。删除了bg4ur的版权信息。请见谅
/*************************************
gps解码1602显示程序
作者:bg4uvr
最后更新:
2007.01.27
1.2版,改时间由rmc语句解码,取消日期显示
2007.01.25
1.1版,修正定位后页面不能转换的bug。
1.0版,基本功能。
***************************************/
#include "reg2051.h"
#include "1602.h"
sbit gps_spd=p1^1;
sbit spd_type=p1^0;
sbit key1=p3^7;
/*
sbit gps_spd=p1^4;
sbit key1=p1^6;
sbit spd_type=p1^5;
*/
unsigned int knots;
char code time_area= 8; //时区
//gps数据存储数组
unsigned char jd[10]; //经度
unsigned char jd_a; //经度方向
unsigned char wd[9]; //纬度
unsigned char wd_a; //纬度方向
//unsigned char time[6]; //时间
unsigned char speed[6]={0}; //速度
unsigned char speed2[6]={0}; //速度
unsigned char high[6]; //高度
unsigned char angle[5]; //方位角
unsigned char use_sat[2]; //使用的卫星数
unsigned char total_sat[2]; //天空中总卫星数
unsigned char lock; //定位状态
unsigned char spd_dot;
unsigned char spd_wei;
unsigned char i2;
//串口中断需要的变量
unsigned char seg_count; //逗号计数器
unsigned char dot_count; //小数点计数器
unsigned char byte_count; //位数计数器
unsigned char cmd_number; //命令类型
unsigned char mode; //0:结束模式,1:命令模式,2:数据模式
unsigned char buf_full; //1:整句接收完成,相应数据有效。0:缓存数据无效。
unsigned char cmd[5]; //命令类型存储数组
//显示需要的变量
unsigned int dsp_count; //刷新次数计数器
unsigned char time_count;
bit page;
bit spd_type;
void sys_init(void);
bit chk_key(void);
main()
{
unsigned char i;
//char bhour;
sys_init();
while(1){
if(buf_full==0) //无gps信号时
{
dsp_count++;
if(dsp_count>=65000){
lcd_cls(); //清屏
lcd_write_string(0,0,"no gps connect..");
while(buf_full==0);
lcd_cls();
dsp_count=0;
}
}
else{ //有gps信号时
if(chk_key()){ //检测到按键切换显示
page=!page;
lcd_cls();
}
if(!page){ //页面1
if(buf_full|0x01){ //gga语句
if(lock==Ɔ'){ //如果未定位
lcd_write_string(0,0,"*---.--.---- ");
lcd_write_string(0,1,"* --.--.---- ");
}else{ //如果已定位
lcd_write_char(0,0,jd_a); //显示经度
for(i=0;i<3;i++){
lcd_write_char(i+1,0,jd
);
}
lcd_write_char(4,0,'.');
for(i=3;i<10;i++){
lcd_write_char(i+2,0,jd);
}
lcd_write_char(0,1,wd_a); //显示纬度
lcd_write_char(1,1,' ');
for(i=0;i<2;i++){
lcd_write_char(i+2,1,wd);
}
lcd_write_char(4,1,'.');
for(i=2;i<9;i++){
lcd_write_char(i+3,1,wd);
} }
lcd_write_char(14,0,use_sat[0]); //显示接收卫星数
lcd_write_char(15,0,use_sat[1]);
buf_full&=~0x01;
dsp_count=0;
}
if(buf_full|0x02){ //gsv语句
lcd_write_char(14,1,total_sat[0]);
lcd_write_char(15,1,total_sat[1]);
buf_full&=~0x02;
dsp_count=0;
}
if(buf_full|0x04){
buf_full&=~0x04;
dsp_count=0;
}
}
else{ //页面2
if(buf_full|0x01){ //gga语句
buf_full&=~0x01;
dsp_count=0;
}
if(buf_full|0x02){
buf_full&=~0x02;
dsp_count=0;
}
// if(buf_full|0x04){ //rmc语句
// bhour=((time[0]-0x30)*10+time[1]-0x30)+time_area;
//if(bhour>=24){
// bhour-=24;
// }else if(bhour<0){
//bhour+=24;
//}
// }//////////////////////
if(spd_type){
lcd_write_string(5,0,"km/h a");
}else{
lcd_write_string(5,0,"knot a");
}
if(lock==Ɔ'){ //如果未定位
lcd_write_string(0,0,"---.-");
lcd_write_string(11,0,"---.-");
}else{ //已经定位
if(!(spd_type)){ //km/h显示
for(i=0;i<5;i++){
lcd_write_char(i,0,speed2);
}
}else{ //knot显示
///我的算法—z
for(i=0;i<=5;i++){
speed=speed2;
}
for(spd_wei=0;spd_wei<=5;spd_wei++){
if(speed[spd_wei]<0x30) break;
}//找到小数点的位置
for(i2=0;i2<=(4-spd_wei);i2++)//数据对齐
//数据位移动
for(i=0;i<=3;i++)
{
speed[4-i]=speed[3-i];
}
/*
for(i2=0;i2<=(spd_wei+1-spd_dot);i2++)//小数对齐
//数据位移动
for(i=0;i<=3;i++)
{
speed[4-i]=speed[3-i];
}
*/
knots= (speed[2]-0x30)*100 + (speed[3]-0x30)*10 + (speed[4]-0x30) ;
knots=knots*65;
knots=knots/35;
lcd_write_char(1,0,(knots)/1000+0x30);
lcd_write_char(2,0,(knots%1000)/100+0x30);
//lcd_write_char(3,0,'.');
lcd_write_char(3,0,(knots%100)/10+0x30);
lcd_write_char(4,0,knots%10+0x30);
}
for(i=0;i<5;i++){
lcd_write_char(11+i,0,angle);
}
lcd_write_string(2,1,"hit:");
for(i=0;i<6;i++){
lcd_write_char(i+6,1,high);
}
buf_full&=~0x04;
dsp_count=0;
}
}
}
}
}
bit chk_key(void)
{
if(!key1){
delayms(10);
if(!key1){
while(!key1);
delayms(10);
return(1);
}
}
return(0);
}
//系统初始化
void sys_init() {
unsigned char i;
scon = 0x50; /* scon: mode 1, 8-bit uart, enable rcvr */
tmod = 0x21; /* tmod: timer 1, mode 2, 8-bit reload */
if(gps_spd){
th1 = 0xfd; /* th1: reload value for 9600 baud @ 11.059mhz */
}else{
th1 = 0xfa; /* th1: reload value for 4800 baud @ 11.059mhz */
}
if(spd_type){
spd_type=1; //速度单位km/h
}else{
spd_type=0; //速度单位knot
}
tr1 = 1; /* tr1: timer 1 run */
lcd_init(); //初始化lcd
lcd_cls();
ie=0x90; //开总中断、串口中断
}
//串口接收中断
void uart(void) interrupt 4
{
unsigned char tmp;
if(ri){
tmp=sbuf;
switch(tmp){
case '$':
cmd_number=0; //命令类型清空
mode=1; //接收命令模式
byte_count=0; //接收位数清空
break;
case ',':
seg_count++; //逗号计数加1
byte_count=0;
break;
case '*':
switch(cmd_number){
case 1:
buf_full|=0x01;
break;
case 2:
buf_full|=0x02;
break;
case 3:
buf_full|=0x04;
break;
}
mode=0;
break;
default:
if(mode==1){
//命令种类判断
cmd[byte_count]=tmp; //接收字符放入类型缓存
if(byte_count>=4){ //如果类型数据接收完毕,判断类型
if(cmd[0]=='g'){
if(cmd[1]=='p'){
if(cmd[2]=='g'){
if(cmd[3]=='g'){
if(cmd[4]=='a'){
cmd_number=1;
mode=2;
seg_count=0;
byte_count=0;
}
}
else if(cmd[3]=='s'){
if(cmd[4]=='v'){
cmd_number=2;
mode=2;
seg_count=0;
byte_count=0;
}
}
}
else if(cmd[2]=='r'){
if(cmd[3]=='m'){
if(cmd[4]=='c'){
cmd_number=3;
mode=2;
seg_count=0;
byte_count=0;
}
}
}
}
}
}
}
else if(mode==2){
//接收数据处理
switch (cmd_number){
case 1: //类型1数据接收。gpgga
switch(seg_count){
case 2: //纬度处理
if(byte_count<9){
wd[byte_count]=tmp;
}
break;
case 3: //纬度方向处理
if(byte_count<1){
wd_a=tmp;
}
break;
case 4: //经度处理
if(byte_count<10){
jd[byte_count]=tmp;
}
break;
case 5: //经度方向处理
if(byte_count<1){
jd_a=tmp;
}
break;
case 6: //定位判断
if(byte_count<1){
lock=tmp;
}
break;
case 7: //定位使用的卫星数
if(byte_count<2){
use_sat[byte_count]=tmp;
}
break;
case 9: //高度处理
if(byte_count<6){
high[byte_count]=tmp;
}
break;
}
break;
case 2: //类型2数据接收。gpgsv
switch(seg_count){
case 3: //天空中的卫星总数
if(byte_count<2){
total_sat[byte_count]=tmp;
}
break;
}
break;
case 3: //类型3数据接收。gprmc
switch(seg_count){
case 1:
// if(byte_count<6){ //时间处理
// time[byte_count]=tmp;
// }
// break;
speed2[0]=speed2[1]=speed2[2]=speed2[3]=speed2[4]=speed2[5]=0;
case 7: //速度处理
if(byte_count<5){
speed2[byte_count]=tmp;
// spd_wei=byte_count;
}
break;
case 8: //方位角处理
if(byte_count<5){
angle[byte_count]=tmp;
}
break;
}
break;
}
}
byte_count++; //接收数位加1
break;
}
}
ri=0;
}
实在自己改不了了。请帮忙给弄弄吧。谢谢