用Arduino从卫星上获取时间

#include <avr/wdt.h>
#include <LiquidCrystal.h>
#include <Adafruit_GPS.h>
#include <Arduino.h>

Adafruit_GPS GPS(&Serial3); //Create GPS object
LiquidCrystal lcd(12,11, 10, 9, 8, 7, 6, 5, 4, 3, 2);

boolean flag = false;

void setup() {
    wdt_enable(WDTO_2S);
    pinMode(13,OUTPUT);
    lcd.begin(16,2);
    GPS.begin(9600);       //Turn GPS on at baud rate of 9600
      lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
      lcd.clear();                  // start with a blank screen
      lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
      lcd.print("Waiting for the");    // change this text to whatever you like. keep it clean.
      lcd.setCursor(0,1);           // set cursor to column 0, row 1
      lcd.print("     satellites!");
}

char NMEA2[30];//30 is enough for time

void loop() {
    while(!GPS.newNMEAreceived()) { //Keep reading characters in this loop until a good NMEA sentence is received
          GPS.read(); //read a character from the GPS
      }
      GPS.parse(GPS.lastNMEA());  //Once you get a good NMEA, parse it
      String NMEA1 = GPS.lastNMEA();      //Once parsed, save NMEA sentence into NMEA1
      NMEA1.trim();
      if(!flag)    //if interruput not acitive, then write new value.
          NMEA1.toCharArray(NMEA2,30);
      light();
      delay(250);
}
void show(){
    lcd.clear();                  
      lcd.setCursor(0,0);  
      lcd.print("  ");
      lcd.print(NMEA2[24]);//print year
      lcd.print(NMEA2[25]);
      lcd.print(NMEA2[26]);
      lcd.print(NMEA2[27]);
      lcd.print(".");
      lcd.print(NMEA2[21]);//print month
      lcd.print(NMEA2[22]);
      lcd.print(".");
      lcd.print(NMEA2[18]);//print day
      lcd.print(NMEA2[19]);

      lcd.setCursor(0,1);  
      lcd.print("   ");
      lcd.print(NMEA2[7]);//print hour
      lcd.print(NMEA2[8]);
      lcd.print(":");
      lcd.print(NMEA2[9]);//print minute
      lcd.print(NMEA2[10]);
      lcd.print(":");
      lcd.print(NMEA2[11]);//print second
      lcd.print(NMEA2[12]);
      lcd.print("  UTC");
}

void light(){
    wdt_reset(); 
    flag = true;
    digitalWrite(13, HIGH);

    if(NMEA2[0] == '$' && NMEA2[3]=='Z' && NMEA2[4]=='D' && NMEA2[5] == 'A' ){//if ZDA sentence
        if(NMEA2[7] == ','){ //this means gps haven't get the time
            lcd.clear();
            lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
              lcd.print("Waiting for the");    // change this text to whatever you like. keep it clean.
              lcd.setCursor(0,1);           // set cursor to column 0, row 1
              lcd.print("      satellite!");  //little change to show gps is working but not get time yet 
              flag = false;
            return;
        }
              show();
      }
      digitalWrite(13, LOW);
      flag = false;
}

国产中科微的 ATGM336H-5N 主控芯片,号称是能够支持 GPS、北斗和格洛纳斯,但是实际来看,GPS 的信号更强,北斗的卫星只连上过一颗,而老毛子的格洛纳斯?不存在的。

目前使用 LCD1602 作为显示,使用 I2C 连接的 LCD2004 已经在路上了,个人感觉 1602 这个屏幕很鸡肋啊,基本上显示不了什么东西,而相比之下 2004 就有很大的富裕了。不过对于显示时间的工作,足够了。

我是用的 GPS 模块使用串口发送 NMEA-0183 语句。用惯了 Java 的 Split 之后再使用 C 进行字符处理就令我很头疼了。但是好在是描述时间的 ZDA 语句的格式是固定的,因此将 String 转化为 char 数组之后直接选取第几位进行显示就好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值