为了避免使用Arduino的自带串口,从而影响与电脑的链接,所以使#include<SoftwareSerial.h>这个库来模拟串口。但是看网上的人说,毕竟是模拟的,会存在一些未知的Bug,至少我目前没有遇到。
本文将使用串口GPS模块进行SoftwareSerial.h库的学习
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(2, 3);
void setup()
{
Serial.begin(9600);
ss.begin(9600);
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
}
void displayInfo()
{
Serial.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() +8< 10) Serial.print(F("0"));
Serial.print((gps.time.hour()+8)%100);
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
}
else
{
Serial.print(F("NO Time"));
}
Serial.println();
}
---------------------
作者:zuoheizhu
来源:CSDN
原文:https://blog.csdn.net/zuoheizhu/article/details/86553884
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件