#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 数组之后直接选取第几位进行显示就好了。