主要的一个问题是,有时候能接到数据,并且显示在lcd1602上,但有时候就不行,在lcd上面显示的是2.22222或者是一堆四条横杠的东西。特别是刚刚下完程序,打开单片机没什么问题,关掉再马上打开就变得不行了。如果过段时间再开单片机,就又可以正常显示了。还有,我的显示程序应该是没有问题的,因为我如果给用来存数据的数组赋一组可用数据的话,显示是一点问题也没有的
#include<reg52.h>
#include<string.h>
#include<stdlib.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;//1^6//3^4
sbit lcdrs=P3^5;//1^4//3^5
sbit lcdrw=P3^6;//1^5//3^6
uchar code table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
//设置gps储存数据的结构体
typedef struct
{
double latitude;
double longitude;
}nmea_msg;
nmea_msg *gpsx;
char flag,flag2,flag3;
/*
flag2,flag3来接受gprmc的标志
flag是中断标志位
*/
char date;
uchar dx,dataresult;
double temp1;
uchar idata buffer[100]={0}; //从串口接收的数据
//uchar idata buffer[]="$GPRMC,064439.00,A,3117.44506,N,10779.50426,E,3.098,99.02,291013,,,A*5";
uchar i;
void delay(int z)
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
lcdrw=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void clear_buffer(void)
{
int k ;
for( k=0;k<100;k++)
buffer[k]=0;
i=0;
}
void dataprocess (double x)
{
int xdata shu,qian,bai,shi,ge,i;
double f;
shu=(int)x;
qian=shu/1000;
if(qian==0)
write_data(' ');
else
write_data(table[qian]);
bai=(shu%1000)/100;
if(bai==0&&qian==0)
write_data(' ');
else if(bai==0&&qian!=0)
write_data('0');
else
write_data(table[bai]);
shi=((shu%1000)%100)/10;
if(shi==0&&qian==0&&bai==0)
write_data(' ');
else if(shi==0&&bai!=0&&qian!=0)
write_data('0');
else
write_data(table[shi]);
ge=((shu%1000)%100)%10;
if(ge==0)
write_data('0');
else
write_data(table[ge]);
write_data('.');
//--------------------------------
f=x-shu;
i=0;
while(i<6)
{
f=f*10;
shu=(int)f;
if(shu==0&&f-shu==0)
break;
write_data(table[shu]);
f=f-shu;
i++;
}
}
void init()
{
clear_buffer();
i=0;
flag=0;
flag2=0;
flag3=0;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1; //串口中断
// gpsx->latitude= 0.0 ;
// gpsx->longitude=0.0 ;
//lcd---------------------
lcden=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
uchar NMEA_Comma_Pos(uchar*buf,uchar cx)
{
uchar*p=buf;
while(cx)
{
if(*p=='*'||*p<' '||*p>'z')
return 0xff;
if(*p==',')
cx--;
p++;
}
return p-buf;
}
//m^n次方
uint NMEA_Pow(uchar m,uchar n)
{
uint result=1;
while(n--)
result*=m;
return result;
}
double NMEA_Str2num(uchar*buf)
{
uchar*p=buf;
double ires=0,fres=0;
uchar ilen=0,flen=0,i;
uchar mask=0;
double res;
while(1)//得到整数和小数的长度
{
if(*p==','||(*p=='*'))break;
if(*p=='.')
{
mask=1;
p++;
}
else if(*p>'9'||(*p<'0'))
{
break;
}
if(mask==1)flen++;
else ilen++;
p++;
}
for(i=0;i<ilen;i++) //得到整数部分
ires+=NMEA_Pow(10,ilen-1-i)*(buf-'0');
if(flen>4)
flen=4;
dx=flen;//小数点位数
for(i=0;i<flen;i++)
{
fres+=NMEA_Pow(10,flen-1-i)*(buf[ilen+1+i]-'0');
}
res=ires+fres/NMEA_Pow(10,flen);
return res;
}
void NMEA_GPRMC_Analysis1(nmea_msg *gpsx,uchar *buf)
{
uchar *p1;
uchar posx;
// p1=(uchar*)strstr((uchar *)buf,"MC");
p1=buf;
posx=NMEA_Comma_Pos(p1,3);
if(posx!=0XFF)
{
gpsx->latitude=NMEA_Str2num(p1+posx)/NMEA_Pow(10,2);
}
}
void NMEA_GPRMC_Analysis2(nmea_msg *gpsx,uchar *buf)
{
uchar *p1;
uchar posx;
// p1=(uchar*)strstr((uchar *)buf,"M");
p1=buf;
posx=NMEA_Comma_Pos(p1,5); //得到经度
if(posx!=0XFF)
{
gpsx->longitude=NMEA_Str2num(p1+posx)/NMEA_Pow(10,2);
}
}
void main()
{
gpsx=(nmea_msg*)malloc(sizeof(nmea_msg));
init();
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
if((date=='M'||flag2==1)) //寻找$GPRMC中的R
{
flag2=1;
if(date=='M')
{
i=0;
}
if(i>100)
{
i=0;
continue;
}
if(date=='*')
flag2=0,flag3=1;
}
if(flag3==1)
{
i=0;
flag3=0;
NMEA_GPRMC_Analysis1(gpsx,buffer);
write_data('l');
write_data('a');
write_data('t');
write_data(':');
temp1=gpsx->latitude;
dataprocess(temp1);
write_data('N');
NMEA_GPRMC_Analysis2(gpsx,buffer);
write_com(0x80+0x40);
write_data('l');
write_data('o');
write_data('g');
write_data(':');
temp1=gpsx->longitude;
dataprocess(temp1);
write_data('E');
delay(500);
write_com(0x01);
//-----------------------------------------------
clear_buffer();
}
ES=1;
}
}
}
void ser() interrupt 4
{
RI=0;
date=SBUF;
flag=1;
buffer= date; //存取串口接收的数据
i++;
if( i==100)
i= 0;
}