-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichUNODS1307.cpp
122 lines (115 loc) · 4.2 KB
/
RichUNODS1307.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
/****************************************************************************/
// Function: Cpp file for DS1307
// Hardware: Grove - RTC
// Arduino IDE: Arduino-1.0
// Author: FrankieChu
// Date: Jan 19,2013
// Version: v1.0
// by www.seeedstudio.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/****************************************************************************/
#include <Wire.h>
#include "RichUNODS1307.h"
uint8_t DS1307::decToBcd(uint8_t val)
{
return ( (val/10*16) + (val%10) );
}
//Convert binary coded decimal to normal decimal numbers
uint8_t DS1307::bcdToDec(uint8_t val)
{
return ( (val/16*10) + (val%16) );
}
void DS1307::begin()
{
Wire.begin();
}
/*Function: The clock timing will start */
void DS1307::startClock(void) // set the ClockHalt bit low to start the rtc
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00); // Register 0x00 holds the oscillator start/stop bit
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
second = Wire.read() & 0x7f; // save actual seconds and AND sec with bit 7 (sart/stop bit) = clock started
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write((uint8_t)second); // write seconds back and start the clock
Wire.endTransmission();
}
/*Function: The clock timing will stop */
void DS1307::stopClock(void) // set the ClockHalt bit high to stop the rtc
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00); // Register 0x00 holds the oscillator start/stop bit
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
second = Wire.read() | 0x80; // save actual seconds and OR sec with bit 7 (sart/stop bit) = clock stopped
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write((uint8_t)second); // write seconds back and stop the clock
Wire.endTransmission();
}
/****************************************************************/
/*Function: Read time and clock from RTC */
void DS1307::getTime()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f);// Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
/*******************************************************************/
/*Frunction: Write the time that includes the clock to the RTC chip */
void DS1307::setTime()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write((uint8_t)0x00);
Wire.write(decToBcd(second));// 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set bit 6
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void DS1307::fillByHMS(uint8_t _hour, uint8_t _minute, uint8_t _second)
{
// assign variables
hour = _hour;
minute = _minute;
second = _second;
}
void DS1307::fillByYMD(uint16_t _year, uint8_t _month, uint8_t _day)
{
year = _year-2000;
month = _month;
dayOfMonth = _day;
}
void DS1307::fillDayOfWeek(uint8_t _dow)
{
dayOfWeek = _dow;
}