-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyRTC.cpp
More file actions
162 lines (146 loc) · 3.41 KB
/
MyRTC.cpp
File metadata and controls
162 lines (146 loc) · 3.41 KB
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
/*
MyRTC
*/
#include <Wire.h>
#include "MyRTC.h"
#include "Debug.h"
/*
Initialisierung
*/
MyRTC::MyRTC(int address, byte statusLedPin) : TimeStamp(0, 0, 0, 0, 0, 0, false) {
_address = address;
_statusLedPin = statusLedPin;
pinMode(_statusLedPin, OUTPUT);
digitalWrite(_statusLedPin, LOW);
}
/*
LED ein- oder ausschalten
*/
void MyRTC::statusLed(boolean on) {
if (on) {
digitalWrite(_statusLedPin, HIGH);
} else {
digitalWrite(_statusLedPin, LOW);
}
}
/*
Uhrzeit auslesen und in den Variablen ablegen
*/
void MyRTC::readTime() {
byte returnStatus, count, result, retries = 0;
do {
// Reset the register pointer
Wire.beginTransmission(_address);
Wire.write((uint8_t) 0x00);
result = Wire.endTransmission(false);
count = Wire.requestFrom(_address, 7);
if (count == 7) {
// Success - A few of these need masks because certain bits are control bits
_seconds = bcdToDec(Wire.read() & 0x7f);
_minutes = bcdToDec(Wire.read());
_hours = bcdToDec(Wire.read() & 0x3f);
_dayOfWeek = bcdToDec(Wire.read());
_date = bcdToDec(Wire.read());
_month = bcdToDec(Wire.read());
_year = bcdToDec(Wire.read());
} else {
// Fail - keine 7 Byte zurueck gekommen? Buffer verwerfen...
for (int i = 0; i < count; i++) {
Wire.read();
}
retries++;
}
result = Wire.endTransmission(true); // true, jetzt den Bus freigeben.
} while ((count != 7) && (retries < 8));
// Es konnte nichts gelesen werden
if (retries == 8) {
_seconds = 11;
_minutes = 11;
_hours = 11;
_dayOfWeek = 1;
_date = 1;
_month = 1;
_year = 17;
_isDST = false;
DEBUG_PRINTLN(F("RTC error. Set defaults."));
}
}
/*
Uhrzeit aus den Variablen in die DS3231 schreiben.
*/
void MyRTC::writeTime() {
Wire.beginTransmission(_address);
Wire.write((uint8_t) 0x00);
Wire.write(decToBcd(_seconds));
Wire.write(decToBcd(_minutes));
Wire.write(decToBcd(_hours));
Wire.write(decToBcd(_dayOfWeek));
Wire.write(decToBcd(_date));
Wire.write(decToBcd(_month));
Wire.write(decToBcd(_year));
Wire.endTransmission();
}
/**
* SQW fuer DS1307.
*/
void MyRTC::enableSQWOnDS1307() {
Wire.beginTransmission(_address);
Wire.write(0x07); // Datenregister
Wire.write(0b00010000); // enable 1HZ square wave output
Wire.endTransmission();
}
/*
SQW mit 1Hz einschalten
*/
void MyRTC::enableSQWOnDS3231() {
Wire.beginTransmission(_address);
Wire.write(0x0E);
Wire.write(0b0000000);
Wire.endTransmission();
}
/*
Konvertierung Dezimal zu "Binary Coded Decimal"
*/
byte MyRTC::decToBcd(byte val) {
return ((val / 10 * 16) + (val % 10));
}
/*
Konvertierung "Binary Coded Decimal" zu Dezimal
*/
byte MyRTC::bcdToDec(byte val) {
return ((val / 16 * 10) + (val % 16));
}
/*
Aus einem String zwei Stellen als Zahl bekommen.
*/
uint8_t MyRTC::conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
/*
Setter/Getter
*/
void MyRTC::setHours(byte hours) {
_hours = hours;
}
void MyRTC::setMinutes(byte minutes) {
_minutes = minutes;
}
void MyRTC::setSeconds(byte seconds) {
_seconds = seconds;
}
byte MyRTC::getSeconds() {
return _seconds;
}
/*
Temperatur lesen
*/
int8_t MyRTC::getTemperature() {
Wire.beginTransmission(_address);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(_address, 2);
return Wire.read();
}