Arduino mega2560 和商城里的GPS 求助怎么显示经度和纬度
本人已纠结于 从GPS获取经纬度 ,代码如下#include <TinyGPS.h>#include <LiquidCrystal.h>
TinyGPS gps;
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
int DEG;
int MIN1;
int MIN2;
void LAT(){ //Latitude state
DEG=lat/1000000;
MIN1=(lat/10000)%100;
MIN2=lat%10000;
Serial.println("LAT:");
Serial.print(MIN1);
Serial.print(".");
Serial.print(MIN2);
Serial.print("' ");
Serial.println("");
}
void LON(){ //Longitude state
DEG=lon/1000000;
MIN1=(lon/10000)%100;
MIN2=lon%10000;
Serial.println("LON:");
Serial.print(DEG);
Serial.print(MIN1);
Serial.print(".");
Serial.print(MIN2);
Serial.print("' ");
Serial.println("");
}
void setup()
{
Serial.begin(38400); //Set the GPS baud rate.
delay(10000);
}
void loop()
{
while (Serial.available())
{
int c = Serial.read(); // Read the GPS data
if (gps.encode(c)) // Check the GPS data
{
gps.get_position(&lat, &lon, &fix_age);
// process new gps info here
}
}
gps.get_position(&lat, &lon, &fix_age); // retrieves +/- lat/long in 100000ths of a degree
gps.get_datetime(&date, &time, &fix_age); // time in hhmmsscc, date in ddmmyy
//LAT();
//LON();
Serial.println(lat);
delay(1000);
Serial.println(lon);
}
输出结果如下,可是经度和纬度都是9999999999 哪位大神能说一下哪错了吗
$GPGSV,3,1,09,12,23,127,,14,33,274,25,15,31,073,26,18,87,266,25*72
$GPGSV,3,2,09,20,24,134,,21,31,210,21,22,46,307,34,24,66,063,27*75
$GPGSV,3,3,09,25,07,162,18*4C
$GPGLL,4000.06376,N,11620.45307,E,111024.60,A,A*69
999999999
999999999
999999999
999999999
999999999
999999999
999999999
file:///C:\Users\DELL\Documents\Tencent Files\2991299442\Image\C2C\3)K~HBT$U~$U%QJY2@_HX_Q.png
我记得我写过这段程序你可以自己参考下 #include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#define WireSend(args) Wire.write(args)
#define WireRead(args) Wire.read(args)
#define printByte(args) Serial.write(args)
#define printlnByte(args)Serial.write(args),Serial.println()
#else
#include "WProgram.h"
#define WireSend(args) Wire.send(args)
#define WireRead(args) Wire.receive(args)
#define printByte(args) Serial.print(args,BYTE)
#define printlnByte(args)Serial.println(args,BYTE)
#endif
#include <Wire.h>
#define BUFFER_LENGTH 10//Define the buffer length
int GPSAddress = 0x42;//GPS I2C Address
double Datatransfer(char *data_buf,char num)//Data type converter:convert char type to float
{ //*data_buf:char data array ;num:float length
double temp=0.0;
unsigned char i,j;
if(data_buf=='-')//负数的情况
{
i=1;
//数组中的字符型数据转换成整数并累加
while(data_buf!='.')
temp=temp*10+(data_buf-0x30);
for(j=0;j<num;j++)
temp=temp*10+(data_buf[++i]-0x30);
//将转换后的整数转换成浮点数
for(j=0;j<num;j++)
temp=temp/10;
//转换成负数
temp=0-temp;
}
else//正数情况
{
i=0;
while(data_buf!='.')
temp=temp*10+(data_buf-0x30);
for(j=0;j<num;j++)
temp=temp*10+(data_buf[++i]-0x30);
for(j=0;j<num;j++)
temp=temp/10 ;
}
return temp;
}
void rec_init()//initial GPS
{
Wire.beginTransmission(GPSAddress);
WireSend(0xff);//发送数据所在的地址
Wire.endTransmission();
Wire.beginTransmission(GPSAddress);
Wire.requestFrom(GPSAddress,10);//要求从GPS器件读取10个字节
}
char ID()//接收语句的ID
{
char i = 0;
char value={
'$','G','P','G','G','A',',' };//要接收的GPS语句的ID内容
char buff={
'0','0','0','0','0','0','0' };
while(1)
{
rec_init();//接收数据初始化
while(Wire.available())
{
buff = WireRead();//接收串口的数据
if(buff==value)//对比是否是正确的ID
{
i++;
if(i==7)
{
Wire.endTransmission();//结束接收
return 1;//接收完毕返回1
}
}
else
i=0;
}
Wire.endTransmission();//结束接收
}
}
void UTC()//获取时间信息
{
char i = 0,flag=0;
char value={
'$','G','P','G','G','A',',' };
char buff={
'0','0','0','0','0','0','0' };
char time={
'0','0','0','0','0','0','0','0','0' };//存放时间数据
double t=0.0;
while(1)
{
rec_init();
while(Wire.available())
{
if(!flag)
{
buff = WireRead();
if(buff==value)
{
i++;
if(i==7)
{
i=0;
flag=1;
}
}
else
i=0;
}
else
{
time = WireRead();
i++;
if(i==9)
{
t=Datatransfer(time,2);//转换成浮点型数据
t=t+80000.00;//将时间转换成北京时间
Serial.println(t);//输出时间数据
Wire.endTransmission();
return;
}
}
}
Wire.endTransmission();
}
}
void rec_data(char *buff,char num1,char num2)//接收数据子函数
{ //*buff:存放接收数据的数组;num1:逗号数目;num2:数组长度。
char i=0,count=0;
if(ID())
{
while(1)
{
rec_init();
while(Wire.available())
{
buff = WireRead();
if(count!=num1)
{
if(buff==',')
count++;
}
else
{
i++;
if(i==num2)
{
Wire.endTransmission();
return;
}
}
}
Wire.endTransmission();
}
}
}
float latitude()//获取纬度信息
{
char lat={
'0','0','0','0','0','0','0','0','0','0' };//存放纬度数据
rec_data(lat,1 ,10);//接收纬度数据
Serial.println(Datatransfer(lat,5),5);//将纬度数据转换成浮点型数据并输出
return Datatransfer(lat,5);
}
void lat_dir()//获取纬度方向信息
{
char dir={'0'};//存放纬度方向数据
rec_data(dir,2,1);//接收纬度方向数据
printlnByte(dir);//将纬度方向信息输出
}
voidlongitude( )//获取经度信息
{
char lon={
'0','0','0','0','0','0','0','0','0','0','0' };//存放经度数据
rec_data(lon,3,11);//接收经度数据
Serial.println(Datatransfer(lon,5),5);//将经度数据转换成浮点型数据并输出
lcd.print(Datatransfer(lon,5),5);
}
void lon_dir()//获取经度方向信息
{
char dir={'0'};
rec_data(dir,4,1);
printlnByte(dir);//将纬度方向信息输出
}
void altitude()//获取海拔信息
{
char i=0,count=0;
char alt={
'0','0','0','0','0','0','0','0' };
if(ID())
{
while(1)
{
rec_init();
while(Wire.available())
{
alt = WireRead();
if(count!=8)
{
if(alt==',')
count++;
}
else
{
if(alt==',')
{
Serial.println(Datatransfer(alt,1),1);
Wire.endTransmission();
return;
}
else
i++;
}
}
Wire.endTransmission();
}
}
}
void setup()
{
Wire.begin();//IIC初始化
Serial.begin(9600);//设置波特率
Serial.println("DFRobot DFRduino GPS Shield v1.0");
Serial.println("$GPGGA statement information: ");
lcd.begin(16,2);
}
void loop()
{
while(1)
{
//Serial.print("UTC:");
//UTC();
//Serial.print("Lat:");
// latitude();
// Serial.print("Dir:");
//lat_dir();
// Serial.print("Lon:");
// longitude();
//Serial.print("Dir:");
// lon_dir();
// Serial.print("Alt:");
// altitude();
// Serial.println(' ');
// Serial.println(' ');
lcd.begin(16, 2); // 开始
lcd.setCursor(0,0);
lcd.print(latitude());
Serial.println("++++++");
Serial.println(latitude());
}
} hnyzcj 发表于 2015-11-3 21:04
#include
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#if defined(ARDUINO) && ARDUINO >= 100
把GPS放到室外,运行了一下,还是没出数据,不知为什么 hnyzcj 发表于 2015-11-3 21:04
#include
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#if defined(ARDUINO) && ARDUINO >= 100
把GPS放到室外,运行了一下,还是没出数据,不知为什么 我的是做了叠加三层,硬件顺序从下往上依次为:UNO+GPS+LCD Keypad Shield 按键扩展板 hnyzcj 发表于 2015-11-4 06:14
我的是做了叠加三层,硬件顺序从下往上依次为:UNO+GPS+LCD Keypad Shield 按键扩展板...
我的是arduino mega2560和GPS模块直接连起来了, 连的是TX0和RX0,想问一下你连的是是哪几个口呢 请收下我的膝盖
页:
[1]