-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbclock.ino
186 lines (157 loc) · 3.87 KB
/
rbclock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <Arduino.h>
#include <TM1637Display.h>
#include <NMEAGPS.h>
#include <GPSport.h>
const int CLK1 = 16;
const int DIO1 = 15;
const int CLK2 = 18;
const int DIO2 = 17;
const int BUTTONPIN = 5;
const int PPS = 6;
const int SETLED = 2;
const int T1 = 11; // Timer 1 fuer 1kHz-Signal
volatile unsigned int i, s, m, h, mon, dom, y, to;
volatile unsigned int leapday = 0 ;
unsigned long hms = 100000 ;
int gps_s, gps_m, gps_h, gps_mon, gps_dom, gps_y;
int month_length[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
boolean setme = true;
String nmeabuffer;
TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);
NMEAGPS gps; // This parses the GPS characters
gps_fix fix; // This holds on to the latest values
void setup() {
noInterrupts();
pinMode(T1, INPUT);
pinMode(BUTTONPIN, INPUT_PULLUP);
pinMode(SETLED, OUTPUT); // setme LED
digitalWrite(1, HIGH);
delay(1000);
digitalWrite(T1, HIGH);
display1.setBrightness(0x0f);
display2.setBrightness(0x0f);
display1.clear();
display2.clear();
// Timer zum Zählen der 1kHz-Pulse konfigurieren
TCCR1A = 0;
TCCR1B = 71;
TCCR1B |= (1 << WGM12); // CTC mode
TCNT1 = 1;
TIMSK1 = 2;
OCR1A = 999;
interrupts();
Serial.begin(38400);
while (!Serial && millis () < 3000UL)
;
gpsPort.begin(9600);
attachInterrupt(digitalPinToInterrupt(BUTTONPIN), set_me, CHANGE );
Serial.println("hello in setup()");
}
ISR(TIMER1_COMPA_vect) {
Serial.println("INTERRUPT#######################");
i = 0; // Hack!
if (s < 59) { // FIXME Schaltsekunde
s++ ;
}
else {
s = 0;
if (m < 59) {
m++ ;
}
else {
m = 0;
if (h < 23) {
h++ ;
}
else {
h = 0;
if (dom < (month_length[mon - 1] + leapday ) ) {
dom++;
}
else {
dom = 1 ;
if (mon < 12) {
mon++ ;
}
else {
mon = 1 ;
y++ ;
}
}
}
}
}
}
void set_me() {
setme = true;
}
void align_pps() {
i = 0;
Serial.println("align pps");
}
void set_clock(){
Serial.println("in set_clock");
TIMSK1 = 0; //disable Timer interrupt
attachInterrupt(digitalPinToInterrupt(PPS), align_pps, RISING);
delay(2000);
TIMSK1 = 2; //disable Timer interrupt
detachInterrupt(digitalPinToInterrupt(PPS));
while (!gps.available( gpsPort )) {
Serial.println("gps not available");
delay(10);
}
fix = gps.read();
while (!fix.valid.altitude) {
Serial.println("no overdetermined fix");
delay(10);
fix = gps.read();
}
if (fix.valid.time) { // fix.valid.altitude = overdetermined fix mit 4 oder mehr sats
gps_s = fix.dateTime.seconds;
gps_m = fix.dateTime.minutes;
gps_h = fix.dateTime.hours;
gps_mon = fix.dateTime.month;
gps_dom = fix.dateTime.date;
gps_y = fix.dateTime.year + 2000; // FIXME: library nimmt nur die letzten 2 Stellen des Jahrs
h = gps_h;
m = gps_m;
s = gps_s;
mon = gps_mon;
dom = gps_dom;
y = gps_y;
}
}
int getCheckSum(String s) {
// NMEA-Checksum Matthias Busse 18.05.2014 Version 1.1
unsigned int j, XOR, c;
for (XOR = 0, j = 0; j < s.length(); j++) {
c = (unsigned char)s.charAt(j);
if (c == '*') break;
if ((c != '$') && (c != '!')) XOR ^= c;
}
return XOR;
}
void loop() {
display1.showNumberDec(100 * h + m, true);
display2.showNumberDec(s, false);
i = TCNT1;
delay(100);
nmeabuffer = "";
nmeabuffer = nmeabuffer + "$ZAZDA," + (10000L*h + 100*m + s) + "." + i + "," + dom + "," + mon + "," + y + ",-02,00*";
digitalWrite(SETLED, setme);
if ( (mon == 2 ) && (( y % 4 == 0 ) && (y % 100 != 0) || (y % 400 == 0)) ) {
leapday = 1;
}
else {
leapday = 0 ;
}
if (setme) {
set_clock();
setme = false ;
digitalWrite(1, LOW);
} else {
Serial.print(nmeabuffer);
Serial.println( getCheckSum(nmeabuffer), HEX);
}
}