-
Notifications
You must be signed in to change notification settings - Fork 2
/
RTC_DS1307.cpp
executable file
·77 lines (64 loc) · 1.72 KB
/
RTC_DS1307.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
// Code by JeeLabs http://news.jeelabs.org/code/
// Released to the public domain! Enjoy!
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#include <avr/pgmspace.h>
#include <Wire.h>
#include "RTClib.h"
#include "RTC_DS1307.h"
#define DS1307_ADDRESS 0x68
#if ARDUINO < 100
#define SEND(x) send(x)
#define RECEIVE(x) receive(x)
#else
#define SEND(x) write(static_cast<uint8_t>(x))
#define RECEIVE(x) read(x)
#endif
////////////////////////////////////////////////////////////////////////////////
// RTC_DS1307 implementation
uint8_t RTC_DS1307::begin(void)
{
return 1;
}
uint8_t RTC_DS1307::isrunning(void)
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.SEND(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 1);
uint8_t ss = Wire.RECEIVE();
return !(ss>>7);
}
void RTC_DS1307::adjust(const DateTime& dt)
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.SEND(0);
Wire.SEND(bin2bcd(dt.second()));
Wire.SEND(bin2bcd(dt.minute()));
Wire.SEND(bin2bcd(dt.hour()));
Wire.SEND(bin2bcd(0));
Wire.SEND(bin2bcd(dt.day()));
Wire.SEND(bin2bcd(dt.month()));
Wire.SEND(bin2bcd(dt.year() - 2000));
Wire.SEND(0);
Wire.endTransmission();
}
DateTime RTC_DS1307::now()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.SEND(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
uint8_t ss = bcd2bin(Wire.RECEIVE() & 0x7F);
uint8_t mm = bcd2bin(Wire.RECEIVE());
uint8_t hh = bcd2bin(Wire.RECEIVE());
Wire.RECEIVE();
uint8_t d = bcd2bin(Wire.RECEIVE());
uint8_t m = bcd2bin(Wire.RECEIVE());
uint16_t y = bcd2bin(Wire.RECEIVE()) + 2000;
return DateTime (y, m, d, hh, mm, ss);
}
// vim:ci:sw=4 sts=4 ft=cpp