AceTimeClock  1.3.0
Clock classes for Arduino that can synchronize from an NTP server or an RTC chip
DS3231.h
1 /*
2  * MIT License
3  * Copyright (c) 2021 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_HW_DS3231_MODULE_H
7 #define ACE_TIME_HW_DS3231_MODULE_H
8 
9 #include <stdint.h>
10 #include <AceCommon.h> // bcdToDec(), decToBcd()
11 #include "HardwareDateTime.h"
12 #include "HardwareTemperature.h"
13 
14 namespace ace_time {
15 namespace hw {
16 
17 class HardwareDateTime;
18 class HardwareTemperature;
19 
31 template <typename T_WIREI>
32 class DS3231 {
33  private:
34  static const uint8_t kAddress = 0x68;
35 
36  public:
38  explicit DS3231(const T_WIREI& wireInterface) :
39  mWireInterface(wireInterface)
40  {}
41 
43  void readDateTime(HardwareDateTime* dateTime) const {
44  using ace_common::bcdToDec;
45 
46  mWireInterface.beginTransmission(kAddress);
47  mWireInterface.write(0); // set DS3231 register pointer to 00h
48  mWireInterface.endTransmission();
49 
50  // request seven bytes from DS3231 starting from register 00h
51  mWireInterface.requestFrom(kAddress, (uint8_t) 7);
52  dateTime->second = bcdToDec(mWireInterface.read() & 0x7F);
53  dateTime->minute = bcdToDec(mWireInterface.read());
54  dateTime->hour = bcdToDec(mWireInterface.read() & 0x3F);
55  dateTime->dayOfWeek = bcdToDec(mWireInterface.read());
56  dateTime->day = bcdToDec(mWireInterface.read());
57  dateTime->month = bcdToDec(mWireInterface.read());
58  dateTime->year = bcdToDec(mWireInterface.read());
59  }
60 
62  void setDateTime(const HardwareDateTime& dateTime) const {
63  using ace_common::decToBcd;
64 
65  mWireInterface.beginTransmission(kAddress);
66  mWireInterface.write(0); // set next input to start at 'seconds' register
67  mWireInterface.write(decToBcd(dateTime.second));
68  mWireInterface.write(decToBcd(dateTime.minute));
69  mWireInterface.write(decToBcd(dateTime.hour));
70  mWireInterface.write(decToBcd(dateTime.dayOfWeek));
71  mWireInterface.write(decToBcd(dateTime.day));
72  mWireInterface.write(decToBcd(dateTime.month));
73  mWireInterface.write(decToBcd(dateTime.year));
74  mWireInterface.endTransmission();
75  }
76 
78  void readTemperature(HardwareTemperature* temperature) const {
79  mWireInterface.beginTransmission(kAddress);
80  mWireInterface.write(0x11); // set DS3231 register pointer to 11h
81  mWireInterface.endTransmission();
82 
83  mWireInterface.requestFrom(kAddress, (uint8_t) 2);
84  temperature->msb = mWireInterface.read();
85  temperature->lsb = mWireInterface.read();
86  }
87 
88  private:
89  const T_WIREI mWireInterface;
90 };
91 
92 } // hw
93 } // ace_time
94 
95 #endif
New version of the DS3231 class templatized so that any of the AceWire classes can be used to access ...
Definition: DS3231.h:32
DS3231(const T_WIREI &wireInterface)
Constructor.
Definition: DS3231.h:38
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: DS3231.h:43
void setDateTime(const HardwareDateTime &dateTime) const
Set the DS3231 with the HardwareDateTime values.
Definition: DS3231.h:62
void readTemperature(HardwareTemperature *temperature) const
Read the temperature into the HardwareTemperature object.
Definition: DS3231.h:78
The date (year, month, day) and time (hour, minute, second) fields supported by the DS3231 RTC chip.
uint8_t year
[00, 99], year - 2000
uint8_t dayOfWeek
[1, 7], interpretation undefined, increments every day
The temperature in Celcius as a signed (8.8) fixed-point integer.
uint8_t msb
Upper byte of signed (8.8) fixed point temperature.
uint8_t lsb
Lower byte of signed (8.8) fixed point temperature.