forked from adafruit/RTClib
-
Notifications
You must be signed in to change notification settings - Fork 10
/
RTC_DS3234.cpp
103 lines (86 loc) · 2.23 KB
/
RTC_DS3234.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
// 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 <SPI.h>
#include "RTClib.h"
#include "RTC_DS3234.h"
////////////////////////////////////////////////////////////////////////////////
// RTC_DS3234 implementation
// Registers we use
const int CONTROL_R = 0x0e;
const int CONTROL_W = 0x8e;
const int CONTROL_STATUS_R = 0x0f;
const int CONTROL_STATUS_W = 0x8f;
const int SECONDS_R = 0x00;
const int SECONDS_W = 0x80;
// Bits we use
const int EOSC = 7;
const int OSF = 7;
uint8_t RTC_DS3234::begin(void)
{
pinMode(cs_pin,OUTPUT);
cs(HIGH);
SPI.setBitOrder(MSBFIRST);
//Ugh! In order to get this to interop with other SPI devices,
//This has to be done in cs()
SPI.setDataMode(SPI_MODE1);
//Enable oscillator, disable square wave, alarms
cs(LOW);
SPI.transfer(CONTROL_W);
SPI.transfer(0x0);
cs(HIGH);
delay(1);
//Clear oscilator stop flag, 32kHz pin
cs(LOW);
SPI.transfer(CONTROL_STATUS_W);
SPI.transfer(0x0);
cs(HIGH);
delay(1);
return 1;
}
void RTC_DS3234::cs(int _value)
{
SPI.setDataMode(SPI_MODE1);
digitalWrite(cs_pin,_value);
}
uint8_t RTC_DS3234::isrunning(void)
{
cs(LOW);
SPI.transfer(CONTROL_R);
uint8_t ss = SPI.transfer(-1);
cs(HIGH);
return !(ss & _BV(OSF));
}
void RTC_DS3234::adjust(const DateTime& dt)
{
cs(LOW);
SPI.transfer(SECONDS_W);
SPI.transfer(bin2bcd(dt.second()));
SPI.transfer(bin2bcd(dt.minute()));
SPI.transfer(bin2bcd(dt.hour()));
SPI.transfer(bin2bcd(dt.dayOfWeek()));
SPI.transfer(bin2bcd(dt.day()));
SPI.transfer(bin2bcd(dt.month()));
SPI.transfer(bin2bcd(dt.year() - 2000));
cs(HIGH);
}
DateTime RTC_DS3234::now()
{
cs(LOW);
SPI.transfer(SECONDS_R);
uint8_t ss = bcd2bin(SPI.transfer(-1) & 0x7F);
uint8_t mm = bcd2bin(SPI.transfer(-1));
uint8_t hh = bcd2bin(SPI.transfer(-1));
SPI.transfer(-1);
uint8_t d = bcd2bin(SPI.transfer(-1));
uint8_t m = bcd2bin(SPI.transfer(-1));
uint16_t y = bcd2bin(SPI.transfer(-1)) + 2000;
cs(HIGH);
return DateTime (y, m, d, hh, mm, ss);
}
// vim:ai:cin:sw=4 sts=4 ft=cpp