-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGravityRtc.cpp
188 lines (159 loc) · 5.4 KB
/
GravityRtc.cpp
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
187
188
/*********************************************************************
* GravityRtc.cpp
*
* Copyright (C) 2017 [DFRobot](http://www.dfrobot.com),
* GitHub Link :https://github.com/DFRobot/Gravity-I2C-SD2405-RTC-Module/
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Description:Get real-time clock data
*
* Product Links:https://www.dfrobot.com/wiki/index.php/Gravity:_I2C_SD2405_RTC_Module_SKU:_DFR0469
*
* Sensor driver pin:I2C
*
* author : Jason([email protected])
* version : V1.0
* date : 2017-04-18
**********************************************************************/
#include "GravityRtc.h"
#include "Arduino.h"
#include "Wire.h"
GravityRtc::GravityRtc() :year(2017), month(4), day(17), week(4), hour(14), minute(5),second(0)
{
}
GravityRtc::~GravityRtc()
{
}
//********************************************************************************************
// Function Name: setup()
// Function Declaration: Initializing sensor
//********************************************************************************************
void GravityRtc::setup()
{
Wire.begin();
//initRtc();
}
//********************************************************************************************
// Function Name: update()
// Function Declaration: Update the sensor value
//********************************************************************************************
void GravityRtc::update()
{
if (millis() - timeUpdate > 1000)
{
timeUpdate = millis();
readRtc();
processRtc();
}
}
//********************************************************************************************
// Function Name: initRtc()
// Function Declaration: Initializing RTC Clock
//********************************************************************************************
void GravityRtc::initRtc(unsigned int year, unsigned char month,unsigned char day,unsigned char week,
unsigned char hour,unsigned char minute,unsigned char second)
{
WriteTimeOn();
Wire.beginTransmission(RTC_Address);
Wire.write(char(0));//Set the address for writing
Wire.write(this->decTobcd(second));
Wire.write(this->decTobcd(minute));
Wire.write(this->decTobcd(hour + 80)); // +80: sets 24 hours format
Wire.write(this->decTobcd(week)); // days values come from 0 to 6: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
Wire.write(this->decTobcd(day));
Wire.write(this->decTobcd(month));
Wire.write(this->decTobcd(year-2000));
Wire.endTransmission();
Wire.beginTransmission(RTC_Address);
Wire.write(0x12); //Set the address for writing
Wire.write(char(0));
Wire.endTransmission();
WriteTimeOff();
}
//********************************************************************************************
// Function Name: readRtc()
// Function Declaration: Read RTC Time
//********************************************************************************************
void GravityRtc::readRtc()
{
unsigned char n = 0;
Wire.requestFrom(RTC_Address, 7);
while (Wire.available())
{
date[n++] = Wire.read();
}
delayMicroseconds(1);
Wire.endTransmission();
}
//********************************************************************************************
// Function Name: processRtc()
// Function Declaration: Analysis RTC Data from readRtc
//********************************************************************************************
void GravityRtc::processRtc()
{
unsigned char i;
for (i = 0; i<7; i++)
{
if (i != 2)
date[i] = (((date[i] & 0xf0) >> 4) * 10) + (date[i] & 0x0f);
else
{
date[2] = (date[2] & 0x7f);
date[2] = (((date[2] & 0xf0) >> 4) * 10) + (date[2] & 0x0f);
}
}
year = date[6] + 2000;
month = date[5];
day = date[4];
week = date[3];
hour = date[2];
minute = date[1];
second = date[0];
}
//********************************************************************************************
// Function Name: decTobcd()
// Function Declaration: Decimal turn to BCD
//********************************************************************************************
char GravityRtc::decTobcd(char num)
{
return ((num / 10 * 16) + (num % 10));
}
void GravityRtc::WriteTimeOn(void)
{
Wire.beginTransmission(RTC_Address);
Wire.write(0x10);//Set the address for writing as 10H
Wire.write(0x80);//Set WRTC1=1
Wire.endTransmission();
Wire.beginTransmission(RTC_Address);
Wire.write(0x0F);//Set the address for writing as OFH
Wire.write(0x84);//Set WRTC2=1,WRTC3=1
Wire.endTransmission();
}
void GravityRtc::WriteTimeOff(void)
{
Wire.beginTransmission(RTC_Address);
Wire.write(0x0F); //Set the address for writing as OFH
Wire.write(0);//Set WRTC2=0,WRTC3=0
Wire.write(0);//Set WRTC1=0
Wire.endTransmission();
}
/*************************************Test & Print Value***********************************/
//Serial.print("Year = ");//year
//Serial.print(rtc.year);
//Serial.print(" Month = ");//month
//Serial.print(rtc.month);
//Serial.print(" Day = ");//day
//Serial.print(rtc.day);
//Serial.print(" Week = ");//week
//Serial.print(rtc.week);
//Serial.print(" Hour = ");//hour
//Serial.print(rtc.hour);
//Serial.print(" Minute = ");//minute
//Serial.print(rtc.minute);
//Serial.print(" Second = ");//second
//Serial.print(rtc.second);
//
//Serial.println();