SoftwareSerial.h模拟串口~GPS受时实例

为了避免使用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博客文章一键转载插件

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值