example using DS3234 RTC connected to BBC micro:bit using SPI
DS3234/SparkFunDS3234RTC.cpp@0:d5fb8cef7c31, 2018-05-11 (annotated)
- Committer:
- jsa1969
- Date:
- Fri May 11 09:37:45 2018 +0000
- Revision:
- 0:d5fb8cef7c31
initial, just show time
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jsa1969 | 0:d5fb8cef7c31 | 1 | /****************************************************************************** |
jsa1969 | 0:d5fb8cef7c31 | 2 | SparkFunDS3234RTC.cpp |
jsa1969 | 0:d5fb8cef7c31 | 3 | Jim Lindblom @ SparkFun Electronics |
jsa1969 | 0:d5fb8cef7c31 | 4 | original creation date: October 2, 2016 |
jsa1969 | 0:d5fb8cef7c31 | 5 | https://github.com/sparkfun/SparkFun_DS3234_RTC_Arduino_Library |
jsa1969 | 0:d5fb8cef7c31 | 6 | |
jsa1969 | 0:d5fb8cef7c31 | 7 | Implementation of DS3234 real time clock functions |
jsa1969 | 0:d5fb8cef7c31 | 8 | |
jsa1969 | 0:d5fb8cef7c31 | 9 | Resources: |
jsa1969 | 0:d5fb8cef7c31 | 10 | SPI.h - Arduino I2C Library |
jsa1969 | 0:d5fb8cef7c31 | 11 | |
jsa1969 | 0:d5fb8cef7c31 | 12 | Development environment specifics: |
jsa1969 | 0:d5fb8cef7c31 | 13 | Arduino 1.6.8 |
jsa1969 | 0:d5fb8cef7c31 | 14 | SparkFun RedBoard |
jsa1969 | 0:d5fb8cef7c31 | 15 | SparkFun Real Time Clock Module (v14) |
jsa1969 | 0:d5fb8cef7c31 | 16 | |
jsa1969 | 0:d5fb8cef7c31 | 17 | Updated 16 October 2016 by Vassilis Serasidis <avrsite@yahoo.gr> |
jsa1969 | 0:d5fb8cef7c31 | 18 | - Added readFromSRAM' and 'writeToSRAM' functions |
jsa1969 | 0:d5fb8cef7c31 | 19 | |
jsa1969 | 0:d5fb8cef7c31 | 20 | ******************************************************************************/ |
jsa1969 | 0:d5fb8cef7c31 | 21 | |
jsa1969 | 0:d5fb8cef7c31 | 22 | #include "SparkFunDS3234RTC.h" |
jsa1969 | 0:d5fb8cef7c31 | 23 | |
jsa1969 | 0:d5fb8cef7c31 | 24 | // Parse the __DATE__ predefined macro to generate date defaults: |
jsa1969 | 0:d5fb8cef7c31 | 25 | // __Date__ Format: MMM DD YYYY (First D may be a space if <10) |
jsa1969 | 0:d5fb8cef7c31 | 26 | // <MONTH> |
jsa1969 | 0:d5fb8cef7c31 | 27 | #define BUILD_MONTH_JAN ((__DATE__[0] == 'J') && (__DATE__[1] == 'a')) ? 1 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 28 | #define BUILD_MONTH_FEB (__DATE__[0] == 'F') ? 2 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 29 | #define BUILD_MONTH_MAR ((__DATE__[0] == 'M') && (__DATE__[1] == 'a') && (__DATE__[2] == 'r')) ? 3 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 30 | #define BUILD_MONTH_APR ((__DATE__[0] == 'A') && (__DATE__[1] == 'p')) ? 4 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 31 | #define BUILD_MONTH_MAY ((__DATE__[0] == 'M') && (__DATE__[1] == 'a') && (__DATE__[2] == 'y')) ? 5 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 32 | #define BUILD_MONTH_JUN ((__DATE__[0] == 'J') && (__DATE__[1] == 'u') && (__DATE__[2] == 'n')) ? 6 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 33 | #define BUILD_MONTH_JUL ((__DATE__[0] == 'J') && (__DATE__[1] == 'u') && (__DATE__[2] == 'l')) ? 7 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 34 | #define BUILD_MONTH_AUG ((__DATE__[0] == 'A') && (__DATE__[1] == 'u')) ? 8 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 35 | #define BUILD_MONTH_SEP (__DATE__[0] == 'S') ? 9 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 36 | #define BUILD_MONTH_OCT (__DATE__[0] == 'O') ? 10 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 37 | #define BUILD_MONTH_NOV (__DATE__[0] == 'N') ? 11 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 38 | #define BUILD_MONTH_DEC (__DATE__[0] == 'D') ? 12 : 0 |
jsa1969 | 0:d5fb8cef7c31 | 39 | #define BUILD_MONTH BUILD_MONTH_JAN | BUILD_MONTH_FEB | BUILD_MONTH_MAR | \ |
jsa1969 | 0:d5fb8cef7c31 | 40 | BUILD_MONTH_APR | BUILD_MONTH_MAY | BUILD_MONTH_JUN | \ |
jsa1969 | 0:d5fb8cef7c31 | 41 | BUILD_MONTH_JUL | BUILD_MONTH_AUG | BUILD_MONTH_SEP | \ |
jsa1969 | 0:d5fb8cef7c31 | 42 | BUILD_MONTH_OCT | BUILD_MONTH_NOV | BUILD_MONTH_DEC |
jsa1969 | 0:d5fb8cef7c31 | 43 | // <DATE> |
jsa1969 | 0:d5fb8cef7c31 | 44 | #define BUILD_DATE_0 ((__DATE__[4] == ' ') ? 0 : (__DATE__[4] - 0x30)) |
jsa1969 | 0:d5fb8cef7c31 | 45 | #define BUILD_DATE_1 (__DATE__[5] - 0x30) |
jsa1969 | 0:d5fb8cef7c31 | 46 | #define BUILD_DATE ((BUILD_DATE_0 * 10) + BUILD_DATE_1) |
jsa1969 | 0:d5fb8cef7c31 | 47 | // <YEAR> |
jsa1969 | 0:d5fb8cef7c31 | 48 | #define BUILD_YEAR (((__DATE__[7] - 0x30) * 1000) + ((__DATE__[8] - 0x30) * 100) + \ |
jsa1969 | 0:d5fb8cef7c31 | 49 | ((__DATE__[9] - 0x30) * 10) + ((__DATE__[10] - 0x30) * 1)) |
jsa1969 | 0:d5fb8cef7c31 | 50 | |
jsa1969 | 0:d5fb8cef7c31 | 51 | // Parse the __TIME__ predefined macro to generate time defaults: |
jsa1969 | 0:d5fb8cef7c31 | 52 | // __TIME__ Format: HH:MM:SS (First number of each is padded by 0 if <10) |
jsa1969 | 0:d5fb8cef7c31 | 53 | // <HOUR> |
jsa1969 | 0:d5fb8cef7c31 | 54 | #define BUILD_HOUR_0 ((__TIME__[0] == ' ') ? 0 : (__TIME__[0] - 0x30)) |
jsa1969 | 0:d5fb8cef7c31 | 55 | #define BUILD_HOUR_1 (__TIME__[1] - 0x30) |
jsa1969 | 0:d5fb8cef7c31 | 56 | #define BUILD_HOUR ((BUILD_HOUR_0 * 10) + BUILD_HOUR_1) |
jsa1969 | 0:d5fb8cef7c31 | 57 | // <MINUTE> |
jsa1969 | 0:d5fb8cef7c31 | 58 | #define BUILD_MINUTE_0 ((__TIME__[3] == ' ') ? 0 : (__TIME__[3] - 0x30)) |
jsa1969 | 0:d5fb8cef7c31 | 59 | #define BUILD_MINUTE_1 (__TIME__[4] - 0x30) |
jsa1969 | 0:d5fb8cef7c31 | 60 | #define BUILD_MINUTE ((BUILD_MINUTE_0 * 10) + BUILD_MINUTE_1) |
jsa1969 | 0:d5fb8cef7c31 | 61 | // <SECOND> |
jsa1969 | 0:d5fb8cef7c31 | 62 | #define BUILD_SECOND_0 ((__TIME__[6] == ' ') ? 0 : (__TIME__[6] - 0x30)) |
jsa1969 | 0:d5fb8cef7c31 | 63 | #define BUILD_SECOND_1 (__TIME__[7] - 0x30) |
jsa1969 | 0:d5fb8cef7c31 | 64 | #define BUILD_SECOND ((BUILD_SECOND_0 * 10) + BUILD_SECOND_1) |
jsa1969 | 0:d5fb8cef7c31 | 65 | |
jsa1969 | 0:d5fb8cef7c31 | 66 | // DS3234 SPI Settings: |
jsa1969 | 0:d5fb8cef7c31 | 67 | #define DS3234_MAX_SCLK 4000000 // 4MHz max SPI clock rate |
jsa1969 | 0:d5fb8cef7c31 | 68 | //SPISettings DS3234SPISettings(DS3234_MAX_SCLK, MSBFIRST, SPI_MODE3); |
jsa1969 | 0:d5fb8cef7c31 | 69 | |
jsa1969 | 0:d5fb8cef7c31 | 70 | SPI spi(MICROBIT_PIN_P15, MICROBIT_PIN_P14, MICROBIT_PIN_P13); // mosi, miso, sclk |
jsa1969 | 0:d5fb8cef7c31 | 71 | |
jsa1969 | 0:d5fb8cef7c31 | 72 | // Constructor -- Initialize class variables to 0 |
jsa1969 | 0:d5fb8cef7c31 | 73 | DS3234::DS3234() |
jsa1969 | 0:d5fb8cef7c31 | 74 | { |
jsa1969 | 0:d5fb8cef7c31 | 75 | for (int i=0; i<TIME_ARRAY_LENGTH; i++) |
jsa1969 | 0:d5fb8cef7c31 | 76 | { |
jsa1969 | 0:d5fb8cef7c31 | 77 | _time[i] = 0; |
jsa1969 | 0:d5fb8cef7c31 | 78 | } |
jsa1969 | 0:d5fb8cef7c31 | 79 | _pm = false; |
jsa1969 | 0:d5fb8cef7c31 | 80 | } |
jsa1969 | 0:d5fb8cef7c31 | 81 | |
jsa1969 | 0:d5fb8cef7c31 | 82 | // Begin -- Initialize SPI interface |
jsa1969 | 0:d5fb8cef7c31 | 83 | void DS3234::begin(MicroBitPin* csPin, MicroBit* uBit) |
jsa1969 | 0:d5fb8cef7c31 | 84 | { |
jsa1969 | 0:d5fb8cef7c31 | 85 | _csPin = csPin; |
jsa1969 | 0:d5fb8cef7c31 | 86 | _uBit = uBit; |
jsa1969 | 0:d5fb8cef7c31 | 87 | |
jsa1969 | 0:d5fb8cef7c31 | 88 | _csPin->setDigitalValue(1); |
jsa1969 | 0:d5fb8cef7c31 | 89 | |
jsa1969 | 0:d5fb8cef7c31 | 90 | spi.format(8,3); |
jsa1969 | 0:d5fb8cef7c31 | 91 | spi.frequency(DS3234_MAX_SCLK); |
jsa1969 | 0:d5fb8cef7c31 | 92 | } |
jsa1969 | 0:d5fb8cef7c31 | 93 | |
jsa1969 | 0:d5fb8cef7c31 | 94 | // setTime -- Set time and date/day registers of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 95 | void DS3234::setTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t date, uint8_t month, uint8_t year) |
jsa1969 | 0:d5fb8cef7c31 | 96 | { |
jsa1969 | 0:d5fb8cef7c31 | 97 | _time[TIME_SECONDS] = DECtoBCD(sec); |
jsa1969 | 0:d5fb8cef7c31 | 98 | _time[TIME_MINUTES] = DECtoBCD(min); |
jsa1969 | 0:d5fb8cef7c31 | 99 | _time[TIME_HOURS] = DECtoBCD(hour); |
jsa1969 | 0:d5fb8cef7c31 | 100 | _time[TIME_DAY] = DECtoBCD(day); |
jsa1969 | 0:d5fb8cef7c31 | 101 | _time[TIME_DATE] = DECtoBCD(date); |
jsa1969 | 0:d5fb8cef7c31 | 102 | _time[TIME_MONTH] = DECtoBCD(month); |
jsa1969 | 0:d5fb8cef7c31 | 103 | _time[TIME_YEAR] = DECtoBCD(year); |
jsa1969 | 0:d5fb8cef7c31 | 104 | |
jsa1969 | 0:d5fb8cef7c31 | 105 | setTime(_time, TIME_ARRAY_LENGTH); |
jsa1969 | 0:d5fb8cef7c31 | 106 | } |
jsa1969 | 0:d5fb8cef7c31 | 107 | |
jsa1969 | 0:d5fb8cef7c31 | 108 | void DS3234::setTime(uint8_t sec, uint8_t min, uint8_t hour12, bool pm, uint8_t day, uint8_t date, uint8_t month, uint8_t year) |
jsa1969 | 0:d5fb8cef7c31 | 109 | { |
jsa1969 | 0:d5fb8cef7c31 | 110 | _time[TIME_SECONDS] = DECtoBCD(sec); |
jsa1969 | 0:d5fb8cef7c31 | 111 | _time[TIME_MINUTES] = DECtoBCD(min); |
jsa1969 | 0:d5fb8cef7c31 | 112 | _time[TIME_HOURS] = DECtoBCD(hour12); |
jsa1969 | 0:d5fb8cef7c31 | 113 | _time[TIME_HOURS] |= TWELVE_HOUR_MODE; |
jsa1969 | 0:d5fb8cef7c31 | 114 | if (pm) |
jsa1969 | 0:d5fb8cef7c31 | 115 | _time[TIME_HOURS] |= TWELVE_HOUR_PM; |
jsa1969 | 0:d5fb8cef7c31 | 116 | _time[TIME_DAY] = DECtoBCD(day); |
jsa1969 | 0:d5fb8cef7c31 | 117 | _time[TIME_DATE] = DECtoBCD(date); |
jsa1969 | 0:d5fb8cef7c31 | 118 | _time[TIME_MONTH] = DECtoBCD(month); |
jsa1969 | 0:d5fb8cef7c31 | 119 | _time[TIME_YEAR] = DECtoBCD(year); |
jsa1969 | 0:d5fb8cef7c31 | 120 | |
jsa1969 | 0:d5fb8cef7c31 | 121 | setTime(_time, TIME_ARRAY_LENGTH); |
jsa1969 | 0:d5fb8cef7c31 | 122 | } |
jsa1969 | 0:d5fb8cef7c31 | 123 | |
jsa1969 | 0:d5fb8cef7c31 | 124 | // setTime -- Set time and date/day registers of DS3234 (using data array) |
jsa1969 | 0:d5fb8cef7c31 | 125 | void DS3234::setTime(uint8_t * time, uint8_t len) |
jsa1969 | 0:d5fb8cef7c31 | 126 | { |
jsa1969 | 0:d5fb8cef7c31 | 127 | if (len != TIME_ARRAY_LENGTH) |
jsa1969 | 0:d5fb8cef7c31 | 128 | return; |
jsa1969 | 0:d5fb8cef7c31 | 129 | |
jsa1969 | 0:d5fb8cef7c31 | 130 | spiWriteBytes(DS3234_REGISTER_BASE, time, TIME_ARRAY_LENGTH); |
jsa1969 | 0:d5fb8cef7c31 | 131 | } |
jsa1969 | 0:d5fb8cef7c31 | 132 | |
jsa1969 | 0:d5fb8cef7c31 | 133 | // autoTime -- Fill DS3234 time registers with compiler time/date |
jsa1969 | 0:d5fb8cef7c31 | 134 | bool DS3234::autoTime() |
jsa1969 | 0:d5fb8cef7c31 | 135 | { |
jsa1969 | 0:d5fb8cef7c31 | 136 | _time[TIME_SECONDS] = DECtoBCD(BUILD_SECOND); |
jsa1969 | 0:d5fb8cef7c31 | 137 | _time[TIME_MINUTES] = DECtoBCD(BUILD_MINUTE); |
jsa1969 | 0:d5fb8cef7c31 | 138 | _time[TIME_HOURS] = BUILD_HOUR; |
jsa1969 | 0:d5fb8cef7c31 | 139 | // Convert hour to 12-hour if the DS3234 is in 12-hour mode: |
jsa1969 | 0:d5fb8cef7c31 | 140 | if (is12Hour()) |
jsa1969 | 0:d5fb8cef7c31 | 141 | { |
jsa1969 | 0:d5fb8cef7c31 | 142 | uint8_t pmBit = 0; |
jsa1969 | 0:d5fb8cef7c31 | 143 | if (_time[TIME_HOURS] <= 11) |
jsa1969 | 0:d5fb8cef7c31 | 144 | { |
jsa1969 | 0:d5fb8cef7c31 | 145 | if (_time[TIME_HOURS] == 0) |
jsa1969 | 0:d5fb8cef7c31 | 146 | _time[TIME_HOURS] = 12; |
jsa1969 | 0:d5fb8cef7c31 | 147 | } |
jsa1969 | 0:d5fb8cef7c31 | 148 | else |
jsa1969 | 0:d5fb8cef7c31 | 149 | { |
jsa1969 | 0:d5fb8cef7c31 | 150 | pmBit = TWELVE_HOUR_PM; |
jsa1969 | 0:d5fb8cef7c31 | 151 | if (_time[TIME_HOURS] >= 13) |
jsa1969 | 0:d5fb8cef7c31 | 152 | _time[TIME_HOURS] -= 12; |
jsa1969 | 0:d5fb8cef7c31 | 153 | } |
jsa1969 | 0:d5fb8cef7c31 | 154 | DECtoBCD(_time[TIME_HOURS]); |
jsa1969 | 0:d5fb8cef7c31 | 155 | _time[TIME_HOURS] |= pmBit; |
jsa1969 | 0:d5fb8cef7c31 | 156 | _time[TIME_HOURS] |= TWELVE_HOUR_MODE; |
jsa1969 | 0:d5fb8cef7c31 | 157 | } |
jsa1969 | 0:d5fb8cef7c31 | 158 | else |
jsa1969 | 0:d5fb8cef7c31 | 159 | { |
jsa1969 | 0:d5fb8cef7c31 | 160 | _time[TIME_HOURS] = DECtoBCD(_time[TIME_HOURS]); |
jsa1969 | 0:d5fb8cef7c31 | 161 | } |
jsa1969 | 0:d5fb8cef7c31 | 162 | |
jsa1969 | 0:d5fb8cef7c31 | 163 | _time[TIME_MONTH] = DECtoBCD(BUILD_MONTH); |
jsa1969 | 0:d5fb8cef7c31 | 164 | _time[TIME_DATE] = DECtoBCD(BUILD_DATE); |
jsa1969 | 0:d5fb8cef7c31 | 165 | _time[TIME_YEAR] = DECtoBCD(BUILD_YEAR - 2000); //! Not Y2K (or Y2.1K)-proof :\ |
jsa1969 | 0:d5fb8cef7c31 | 166 | |
jsa1969 | 0:d5fb8cef7c31 | 167 | // Calculate weekday (from here: http://stackoverflow.com/a/21235587) |
jsa1969 | 0:d5fb8cef7c31 | 168 | // Result: 0 = Sunday, 6 = Saturday |
jsa1969 | 0:d5fb8cef7c31 | 169 | int d = BUILD_DATE; |
jsa1969 | 0:d5fb8cef7c31 | 170 | int m = BUILD_MONTH; |
jsa1969 | 0:d5fb8cef7c31 | 171 | int y = BUILD_YEAR; |
jsa1969 | 0:d5fb8cef7c31 | 172 | int weekday = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7; |
jsa1969 | 0:d5fb8cef7c31 | 173 | weekday += 1; // Library defines Sunday=1, Saturday=7 |
jsa1969 | 0:d5fb8cef7c31 | 174 | _time[TIME_DAY] = DECtoBCD(weekday); |
jsa1969 | 0:d5fb8cef7c31 | 175 | |
jsa1969 | 0:d5fb8cef7c31 | 176 | |
jsa1969 | 0:d5fb8cef7c31 | 177 | setTime(_time, TIME_ARRAY_LENGTH); |
jsa1969 | 0:d5fb8cef7c31 | 178 | } |
jsa1969 | 0:d5fb8cef7c31 | 179 | |
jsa1969 | 0:d5fb8cef7c31 | 180 | // update -- Read all time/date registers and update the _time array |
jsa1969 | 0:d5fb8cef7c31 | 181 | void DS3234::update(void) |
jsa1969 | 0:d5fb8cef7c31 | 182 | { |
jsa1969 | 0:d5fb8cef7c31 | 183 | uint8_t rtcReads[TIME_ARRAY_LENGTH]; |
jsa1969 | 0:d5fb8cef7c31 | 184 | |
jsa1969 | 0:d5fb8cef7c31 | 185 | spiReadBytes(DS3234_REGISTER_BASE, rtcReads, TIME_ARRAY_LENGTH); |
jsa1969 | 0:d5fb8cef7c31 | 186 | |
jsa1969 | 0:d5fb8cef7c31 | 187 | for (int i=0; i<TIME_ARRAY_LENGTH; i++) |
jsa1969 | 0:d5fb8cef7c31 | 188 | { |
jsa1969 | 0:d5fb8cef7c31 | 189 | _time[i] = rtcReads[i]; |
jsa1969 | 0:d5fb8cef7c31 | 190 | } |
jsa1969 | 0:d5fb8cef7c31 | 191 | |
jsa1969 | 0:d5fb8cef7c31 | 192 | if (_time[TIME_HOURS] & TWELVE_HOUR_MODE) |
jsa1969 | 0:d5fb8cef7c31 | 193 | { |
jsa1969 | 0:d5fb8cef7c31 | 194 | if (_time[TIME_HOURS] & TWELVE_HOUR_PM) |
jsa1969 | 0:d5fb8cef7c31 | 195 | _pm = true; |
jsa1969 | 0:d5fb8cef7c31 | 196 | else |
jsa1969 | 0:d5fb8cef7c31 | 197 | _pm = false; |
jsa1969 | 0:d5fb8cef7c31 | 198 | |
jsa1969 | 0:d5fb8cef7c31 | 199 | _time[TIME_HOURS] &= 0x1F; // Mask out 24-hour bit, am/pm from hours |
jsa1969 | 0:d5fb8cef7c31 | 200 | } |
jsa1969 | 0:d5fb8cef7c31 | 201 | else |
jsa1969 | 0:d5fb8cef7c31 | 202 | { |
jsa1969 | 0:d5fb8cef7c31 | 203 | _time[TIME_HOURS] &= 0x3F; // Mask out 24-hour bit from hours |
jsa1969 | 0:d5fb8cef7c31 | 204 | } |
jsa1969 | 0:d5fb8cef7c31 | 205 | } |
jsa1969 | 0:d5fb8cef7c31 | 206 | |
jsa1969 | 0:d5fb8cef7c31 | 207 | // getSecond -- read/return seconds register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 208 | uint8_t DS3234::getSecond(void) |
jsa1969 | 0:d5fb8cef7c31 | 209 | { |
jsa1969 | 0:d5fb8cef7c31 | 210 | _time[TIME_SECONDS] = spiReadByte(DS3234_REGISTER_SECONDS); |
jsa1969 | 0:d5fb8cef7c31 | 211 | |
jsa1969 | 0:d5fb8cef7c31 | 212 | return BCDtoDEC(_time[TIME_SECONDS]); |
jsa1969 | 0:d5fb8cef7c31 | 213 | } |
jsa1969 | 0:d5fb8cef7c31 | 214 | |
jsa1969 | 0:d5fb8cef7c31 | 215 | // getMinute -- read/return minutes register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 216 | uint8_t DS3234::getMinute(void) |
jsa1969 | 0:d5fb8cef7c31 | 217 | { |
jsa1969 | 0:d5fb8cef7c31 | 218 | _time[TIME_MINUTES] = spiReadByte(DS3234_REGISTER_MINUTES); |
jsa1969 | 0:d5fb8cef7c31 | 219 | |
jsa1969 | 0:d5fb8cef7c31 | 220 | return BCDtoDEC(_time[TIME_MINUTES]); |
jsa1969 | 0:d5fb8cef7c31 | 221 | } |
jsa1969 | 0:d5fb8cef7c31 | 222 | |
jsa1969 | 0:d5fb8cef7c31 | 223 | // getHour -- read/return hour register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 224 | uint8_t DS3234::getHour(void) |
jsa1969 | 0:d5fb8cef7c31 | 225 | { |
jsa1969 | 0:d5fb8cef7c31 | 226 | uint8_t hourRegister = spiReadByte(DS3234_REGISTER_HOURS); |
jsa1969 | 0:d5fb8cef7c31 | 227 | |
jsa1969 | 0:d5fb8cef7c31 | 228 | if (hourRegister & TWELVE_HOUR_MODE) |
jsa1969 | 0:d5fb8cef7c31 | 229 | hourRegister &= 0x1F; // Mask out am/pm, 24-hour bit |
jsa1969 | 0:d5fb8cef7c31 | 230 | _time[TIME_HOURS] = hourRegister; |
jsa1969 | 0:d5fb8cef7c31 | 231 | |
jsa1969 | 0:d5fb8cef7c31 | 232 | return BCDtoDEC(_time[TIME_HOURS]); |
jsa1969 | 0:d5fb8cef7c31 | 233 | } |
jsa1969 | 0:d5fb8cef7c31 | 234 | |
jsa1969 | 0:d5fb8cef7c31 | 235 | // getDay -- read/return day register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 236 | uint8_t DS3234::getDay(void) |
jsa1969 | 0:d5fb8cef7c31 | 237 | { |
jsa1969 | 0:d5fb8cef7c31 | 238 | _time[TIME_DAY] = spiReadByte(DS3234_REGISTER_DAY); |
jsa1969 | 0:d5fb8cef7c31 | 239 | |
jsa1969 | 0:d5fb8cef7c31 | 240 | return BCDtoDEC(_time[TIME_DAY]); |
jsa1969 | 0:d5fb8cef7c31 | 241 | } |
jsa1969 | 0:d5fb8cef7c31 | 242 | |
jsa1969 | 0:d5fb8cef7c31 | 243 | // getDate -- read/return date register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 244 | uint8_t DS3234::getDate(void) |
jsa1969 | 0:d5fb8cef7c31 | 245 | { |
jsa1969 | 0:d5fb8cef7c31 | 246 | _time[TIME_DATE] = spiReadByte(DS3234_REGISTER_DATE); |
jsa1969 | 0:d5fb8cef7c31 | 247 | |
jsa1969 | 0:d5fb8cef7c31 | 248 | return BCDtoDEC(_time[TIME_DATE]); |
jsa1969 | 0:d5fb8cef7c31 | 249 | } |
jsa1969 | 0:d5fb8cef7c31 | 250 | |
jsa1969 | 0:d5fb8cef7c31 | 251 | // getMonth -- read/return month register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 252 | uint8_t DS3234::getMonth(void) |
jsa1969 | 0:d5fb8cef7c31 | 253 | { |
jsa1969 | 0:d5fb8cef7c31 | 254 | _time[TIME_MONTH] = spiReadByte(DS3234_REGISTER_MONTH); |
jsa1969 | 0:d5fb8cef7c31 | 255 | _time[TIME_MONTH] &= 0x7F; // Mask out century bit |
jsa1969 | 0:d5fb8cef7c31 | 256 | |
jsa1969 | 0:d5fb8cef7c31 | 257 | return BCDtoDEC(_time[TIME_MONTH]); |
jsa1969 | 0:d5fb8cef7c31 | 258 | } |
jsa1969 | 0:d5fb8cef7c31 | 259 | |
jsa1969 | 0:d5fb8cef7c31 | 260 | // getYear -- read/return year register of DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 261 | uint8_t DS3234::getYear(void) |
jsa1969 | 0:d5fb8cef7c31 | 262 | { |
jsa1969 | 0:d5fb8cef7c31 | 263 | _time[TIME_YEAR] = spiReadByte(DS3234_REGISTER_YEAR); |
jsa1969 | 0:d5fb8cef7c31 | 264 | |
jsa1969 | 0:d5fb8cef7c31 | 265 | return BCDtoDEC(_time[TIME_YEAR]); |
jsa1969 | 0:d5fb8cef7c31 | 266 | } |
jsa1969 | 0:d5fb8cef7c31 | 267 | |
jsa1969 | 0:d5fb8cef7c31 | 268 | // setSecond -- set the second register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 269 | void DS3234::setSecond(uint8_t s) |
jsa1969 | 0:d5fb8cef7c31 | 270 | { |
jsa1969 | 0:d5fb8cef7c31 | 271 | if (s <= 59) |
jsa1969 | 0:d5fb8cef7c31 | 272 | { |
jsa1969 | 0:d5fb8cef7c31 | 273 | uint8_t _s = DECtoBCD(s); |
jsa1969 | 0:d5fb8cef7c31 | 274 | spiWriteByte(DS3234_REGISTER_SECONDS, _s); |
jsa1969 | 0:d5fb8cef7c31 | 275 | } |
jsa1969 | 0:d5fb8cef7c31 | 276 | } |
jsa1969 | 0:d5fb8cef7c31 | 277 | |
jsa1969 | 0:d5fb8cef7c31 | 278 | // setMinute -- set the minute register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 279 | void DS3234::setMinute(uint8_t m) |
jsa1969 | 0:d5fb8cef7c31 | 280 | { |
jsa1969 | 0:d5fb8cef7c31 | 281 | if (m <= 59) |
jsa1969 | 0:d5fb8cef7c31 | 282 | { |
jsa1969 | 0:d5fb8cef7c31 | 283 | uint8_t _m = DECtoBCD(m); |
jsa1969 | 0:d5fb8cef7c31 | 284 | spiWriteByte(DS3234_REGISTER_MINUTES, _m); |
jsa1969 | 0:d5fb8cef7c31 | 285 | } |
jsa1969 | 0:d5fb8cef7c31 | 286 | } |
jsa1969 | 0:d5fb8cef7c31 | 287 | |
jsa1969 | 0:d5fb8cef7c31 | 288 | // setHour -- set the hour register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 289 | void DS3234::setHour(uint8_t h) |
jsa1969 | 0:d5fb8cef7c31 | 290 | { |
jsa1969 | 0:d5fb8cef7c31 | 291 | //! Check if 24-hour mode, am/pm |
jsa1969 | 0:d5fb8cef7c31 | 292 | if (h <= 23) |
jsa1969 | 0:d5fb8cef7c31 | 293 | { |
jsa1969 | 0:d5fb8cef7c31 | 294 | uint8_t _h = DECtoBCD(h); |
jsa1969 | 0:d5fb8cef7c31 | 295 | spiWriteByte(DS3234_REGISTER_HOURS, _h); |
jsa1969 | 0:d5fb8cef7c31 | 296 | } |
jsa1969 | 0:d5fb8cef7c31 | 297 | } |
jsa1969 | 0:d5fb8cef7c31 | 298 | |
jsa1969 | 0:d5fb8cef7c31 | 299 | // setDay -- set the day register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 300 | void DS3234::setDay(uint8_t d) |
jsa1969 | 0:d5fb8cef7c31 | 301 | { |
jsa1969 | 0:d5fb8cef7c31 | 302 | if ((d >= 1) && (d <= 7)) |
jsa1969 | 0:d5fb8cef7c31 | 303 | { |
jsa1969 | 0:d5fb8cef7c31 | 304 | uint8_t _d = DECtoBCD(d); //! Unecessary? |
jsa1969 | 0:d5fb8cef7c31 | 305 | return spiWriteByte(DS3234_REGISTER_DAY, _d); |
jsa1969 | 0:d5fb8cef7c31 | 306 | } |
jsa1969 | 0:d5fb8cef7c31 | 307 | } |
jsa1969 | 0:d5fb8cef7c31 | 308 | |
jsa1969 | 0:d5fb8cef7c31 | 309 | // setDate -- set the date register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 310 | void DS3234::setDate(uint8_t d) |
jsa1969 | 0:d5fb8cef7c31 | 311 | { |
jsa1969 | 0:d5fb8cef7c31 | 312 | if (d <= 31) |
jsa1969 | 0:d5fb8cef7c31 | 313 | { |
jsa1969 | 0:d5fb8cef7c31 | 314 | uint8_t _d = DECtoBCD(d); |
jsa1969 | 0:d5fb8cef7c31 | 315 | spiWriteByte(DS3234_REGISTER_DATE, _d); |
jsa1969 | 0:d5fb8cef7c31 | 316 | } |
jsa1969 | 0:d5fb8cef7c31 | 317 | } |
jsa1969 | 0:d5fb8cef7c31 | 318 | |
jsa1969 | 0:d5fb8cef7c31 | 319 | // setMonth -- set the month register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 320 | void DS3234::setMonth(uint8_t mo) |
jsa1969 | 0:d5fb8cef7c31 | 321 | { |
jsa1969 | 0:d5fb8cef7c31 | 322 | if ((mo >= 1) && (mo <= 12)) |
jsa1969 | 0:d5fb8cef7c31 | 323 | { |
jsa1969 | 0:d5fb8cef7c31 | 324 | uint8_t _mo = DECtoBCD(mo); |
jsa1969 | 0:d5fb8cef7c31 | 325 | spiWriteByte(DS3234_REGISTER_MONTH, _mo); |
jsa1969 | 0:d5fb8cef7c31 | 326 | } |
jsa1969 | 0:d5fb8cef7c31 | 327 | } |
jsa1969 | 0:d5fb8cef7c31 | 328 | |
jsa1969 | 0:d5fb8cef7c31 | 329 | // setYear -- set the year register of the DS3234 |
jsa1969 | 0:d5fb8cef7c31 | 330 | void DS3234::setYear(uint8_t y) |
jsa1969 | 0:d5fb8cef7c31 | 331 | { |
jsa1969 | 0:d5fb8cef7c31 | 332 | if (y <= 99) |
jsa1969 | 0:d5fb8cef7c31 | 333 | { |
jsa1969 | 0:d5fb8cef7c31 | 334 | uint8_t _y = DECtoBCD(y); |
jsa1969 | 0:d5fb8cef7c31 | 335 | spiWriteByte(DS3234_REGISTER_YEAR, _y); |
jsa1969 | 0:d5fb8cef7c31 | 336 | } |
jsa1969 | 0:d5fb8cef7c31 | 337 | } |
jsa1969 | 0:d5fb8cef7c31 | 338 | |
jsa1969 | 0:d5fb8cef7c31 | 339 | // set12Hour -- set (or not) to 12-hour mode) | enable12 defaults to true |
jsa1969 | 0:d5fb8cef7c31 | 340 | void DS3234::set12Hour(bool enable12) |
jsa1969 | 0:d5fb8cef7c31 | 341 | { |
jsa1969 | 0:d5fb8cef7c31 | 342 | if (enable12) |
jsa1969 | 0:d5fb8cef7c31 | 343 | set24Hour(false); |
jsa1969 | 0:d5fb8cef7c31 | 344 | else |
jsa1969 | 0:d5fb8cef7c31 | 345 | set24Hour(true); |
jsa1969 | 0:d5fb8cef7c31 | 346 | } |
jsa1969 | 0:d5fb8cef7c31 | 347 | |
jsa1969 | 0:d5fb8cef7c31 | 348 | // set24Hour -- set (or not) to 24-hour mode) | enable24 defaults to true |
jsa1969 | 0:d5fb8cef7c31 | 349 | void DS3234::set24Hour(bool enable24) |
jsa1969 | 0:d5fb8cef7c31 | 350 | { |
jsa1969 | 0:d5fb8cef7c31 | 351 | uint8_t hourRegister = spiReadByte(DS3234_REGISTER_HOURS); |
jsa1969 | 0:d5fb8cef7c31 | 352 | |
jsa1969 | 0:d5fb8cef7c31 | 353 | bool hour12 = hourRegister & TWELVE_HOUR_MODE; |
jsa1969 | 0:d5fb8cef7c31 | 354 | if ((hour12 && !enable24) || (!hour12 && enable24)) |
jsa1969 | 0:d5fb8cef7c31 | 355 | return; |
jsa1969 | 0:d5fb8cef7c31 | 356 | |
jsa1969 | 0:d5fb8cef7c31 | 357 | uint8_t oldHour; |
jsa1969 | 0:d5fb8cef7c31 | 358 | uint8_t newHour; |
jsa1969 | 0:d5fb8cef7c31 | 359 | |
jsa1969 | 0:d5fb8cef7c31 | 360 | if (enable24) |
jsa1969 | 0:d5fb8cef7c31 | 361 | { |
jsa1969 | 0:d5fb8cef7c31 | 362 | oldHour = hourRegister & 0x1F; // Mask out am/pm and 12-hour mode |
jsa1969 | 0:d5fb8cef7c31 | 363 | oldHour = BCDtoDEC(oldHour); // Convert to decimal |
jsa1969 | 0:d5fb8cef7c31 | 364 | newHour = oldHour; |
jsa1969 | 0:d5fb8cef7c31 | 365 | |
jsa1969 | 0:d5fb8cef7c31 | 366 | bool hourPM = hourRegister & TWELVE_HOUR_PM; |
jsa1969 | 0:d5fb8cef7c31 | 367 | if ((hourPM) && (oldHour >= 1)) newHour += 12; |
jsa1969 | 0:d5fb8cef7c31 | 368 | else if (!(hourPM) && (oldHour == 12)) newHour = 0; |
jsa1969 | 0:d5fb8cef7c31 | 369 | newHour = DECtoBCD(newHour); |
jsa1969 | 0:d5fb8cef7c31 | 370 | } |
jsa1969 | 0:d5fb8cef7c31 | 371 | else |
jsa1969 | 0:d5fb8cef7c31 | 372 | { |
jsa1969 | 0:d5fb8cef7c31 | 373 | oldHour = hourRegister & 0x3F; // Mask out am/pm and 12-hour mode |
jsa1969 | 0:d5fb8cef7c31 | 374 | oldHour = BCDtoDEC(oldHour); // Convert to decimal |
jsa1969 | 0:d5fb8cef7c31 | 375 | newHour = oldHour; |
jsa1969 | 0:d5fb8cef7c31 | 376 | |
jsa1969 | 0:d5fb8cef7c31 | 377 | if (oldHour == 0) |
jsa1969 | 0:d5fb8cef7c31 | 378 | newHour = 12; |
jsa1969 | 0:d5fb8cef7c31 | 379 | else if (oldHour >= 13) |
jsa1969 | 0:d5fb8cef7c31 | 380 | newHour -= 12; |
jsa1969 | 0:d5fb8cef7c31 | 381 | |
jsa1969 | 0:d5fb8cef7c31 | 382 | newHour = DECtoBCD(newHour); |
jsa1969 | 0:d5fb8cef7c31 | 383 | newHour |= TWELVE_HOUR_MODE; // Set bit 6 to set 12-hour mode |
jsa1969 | 0:d5fb8cef7c31 | 384 | if (oldHour >= 12) |
jsa1969 | 0:d5fb8cef7c31 | 385 | newHour |= TWELVE_HOUR_PM; // Set PM bit if necessary |
jsa1969 | 0:d5fb8cef7c31 | 386 | } |
jsa1969 | 0:d5fb8cef7c31 | 387 | |
jsa1969 | 0:d5fb8cef7c31 | 388 | return spiWriteByte(DS3234_REGISTER_HOURS, newHour); |
jsa1969 | 0:d5fb8cef7c31 | 389 | } |
jsa1969 | 0:d5fb8cef7c31 | 390 | |
jsa1969 | 0:d5fb8cef7c31 | 391 | // is12Hour -- check if the DS3234 is in 12-hour mode |
jsa1969 | 0:d5fb8cef7c31 | 392 | bool DS3234::is12Hour(void) |
jsa1969 | 0:d5fb8cef7c31 | 393 | { |
jsa1969 | 0:d5fb8cef7c31 | 394 | uint8_t hourRegister = spiReadByte(DS3234_REGISTER_HOURS); |
jsa1969 | 0:d5fb8cef7c31 | 395 | |
jsa1969 | 0:d5fb8cef7c31 | 396 | return hourRegister & TWELVE_HOUR_MODE; |
jsa1969 | 0:d5fb8cef7c31 | 397 | } |
jsa1969 | 0:d5fb8cef7c31 | 398 | |
jsa1969 | 0:d5fb8cef7c31 | 399 | // pm -- Check if 12-hour state is AM or PM |
jsa1969 | 0:d5fb8cef7c31 | 400 | bool DS3234::pm(void) |
jsa1969 | 0:d5fb8cef7c31 | 401 | { |
jsa1969 | 0:d5fb8cef7c31 | 402 | uint8_t hourRegister = spiReadByte(DS3234_REGISTER_HOURS); |
jsa1969 | 0:d5fb8cef7c31 | 403 | |
jsa1969 | 0:d5fb8cef7c31 | 404 | return hourRegister & TWELVE_HOUR_PM; |
jsa1969 | 0:d5fb8cef7c31 | 405 | } |
jsa1969 | 0:d5fb8cef7c31 | 406 | |
jsa1969 | 0:d5fb8cef7c31 | 407 | // enable -- enable the DS3234's oscillator. |
jsa1969 | 0:d5fb8cef7c31 | 408 | void DS3234::enable(void) |
jsa1969 | 0:d5fb8cef7c31 | 409 | { |
jsa1969 | 0:d5fb8cef7c31 | 410 | uint8_t controlRegister = spiReadByte(DS3234_REGISTER_CONTROL); |
jsa1969 | 0:d5fb8cef7c31 | 411 | |
jsa1969 | 0:d5fb8cef7c31 | 412 | controlRegister &= ~(1<<7); |
jsa1969 | 0:d5fb8cef7c31 | 413 | |
jsa1969 | 0:d5fb8cef7c31 | 414 | spiWriteByte(DS3234_REGISTER_CONTROL, controlRegister); |
jsa1969 | 0:d5fb8cef7c31 | 415 | } |
jsa1969 | 0:d5fb8cef7c31 | 416 | |
jsa1969 | 0:d5fb8cef7c31 | 417 | // disable -- disable the DS3234's oscillator |
jsa1969 | 0:d5fb8cef7c31 | 418 | // (Only effects chip when powered by battery.) |
jsa1969 | 0:d5fb8cef7c31 | 419 | void DS3234::disable(void) |
jsa1969 | 0:d5fb8cef7c31 | 420 | { |
jsa1969 | 0:d5fb8cef7c31 | 421 | uint8_t controlRegister = spiReadByte(DS3234_REGISTER_CONTROL); |
jsa1969 | 0:d5fb8cef7c31 | 422 | |
jsa1969 | 0:d5fb8cef7c31 | 423 | controlRegister |= (1<<7); |
jsa1969 | 0:d5fb8cef7c31 | 424 | |
jsa1969 | 0:d5fb8cef7c31 | 425 | spiWriteByte(DS3234_REGISTER_CONTROL, controlRegister); |
jsa1969 | 0:d5fb8cef7c31 | 426 | } |
jsa1969 | 0:d5fb8cef7c31 | 427 | |
jsa1969 | 0:d5fb8cef7c31 | 428 | // setAlarm1 -- Alarm 1 can be set to trigger on seconds, minutes, hours, and/or date/day. |
jsa1969 | 0:d5fb8cef7c31 | 429 | // Any of those can be masked out -- ignored for an alarm match. By setting |
jsa1969 | 0:d5fb8cef7c31 | 430 | // If a value is set to 255, the library will mask out that data. |
jsa1969 | 0:d5fb8cef7c31 | 431 | // By default the "date" value is the day-of-month (1-31) |
jsa1969 | 0:d5fb8cef7c31 | 432 | // The "day" boolean changes the "date" value to a day (between 1-7). |
jsa1969 | 0:d5fb8cef7c31 | 433 | void DS3234::setAlarm1(uint8_t second, uint8_t minute, uint8_t hour, uint8_t date, bool day) |
jsa1969 | 0:d5fb8cef7c31 | 434 | { |
jsa1969 | 0:d5fb8cef7c31 | 435 | uint8_t alarmRegister[4]; |
jsa1969 | 0:d5fb8cef7c31 | 436 | uint8_t timeValue[4] = {second, minute, hour, date}; |
jsa1969 | 0:d5fb8cef7c31 | 437 | uint8_t timeMin[4] = {0, 0, 0, 1}; |
jsa1969 | 0:d5fb8cef7c31 | 438 | uint8_t timeMax[4] = {59, 59, 23, 31}; |
jsa1969 | 0:d5fb8cef7c31 | 439 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 440 | timeMax[3] = 7; |
jsa1969 | 0:d5fb8cef7c31 | 441 | |
jsa1969 | 0:d5fb8cef7c31 | 442 | spiReadBytes(DS3234_REGISTER_A1SEC, alarmRegister, 4); // Read current alarm values |
jsa1969 | 0:d5fb8cef7c31 | 443 | |
jsa1969 | 0:d5fb8cef7c31 | 444 | // Run through all four alarm values and set their register values: |
jsa1969 | 0:d5fb8cef7c31 | 445 | for (int i=0; i<4; i++) |
jsa1969 | 0:d5fb8cef7c31 | 446 | { |
jsa1969 | 0:d5fb8cef7c31 | 447 | if (timeValue[i] == 255) // If 255, disable the check on that value |
jsa1969 | 0:d5fb8cef7c31 | 448 | alarmRegister[i] |= ALARM_MODE_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 449 | else if ((timeValue[i] >= timeMin[i]) && (timeValue[i] <= timeMax[i])) |
jsa1969 | 0:d5fb8cef7c31 | 450 | alarmRegister[i] = DECtoBCD(timeValue[i]); |
jsa1969 | 0:d5fb8cef7c31 | 451 | } |
jsa1969 | 0:d5fb8cef7c31 | 452 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 453 | alarmRegister[3] |= ALARM_DAY_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 454 | |
jsa1969 | 0:d5fb8cef7c31 | 455 | spiWriteBytes(DS3234_REGISTER_A1SEC, alarmRegister, 4); // Write the values |
jsa1969 | 0:d5fb8cef7c31 | 456 | } |
jsa1969 | 0:d5fb8cef7c31 | 457 | |
jsa1969 | 0:d5fb8cef7c31 | 458 | // setAlarm1 (12-hour mode) |
jsa1969 | 0:d5fb8cef7c31 | 459 | void DS3234::setAlarm1(uint8_t second, uint8_t minute, uint8_t hour12, bool pm, uint8_t date, bool day) |
jsa1969 | 0:d5fb8cef7c31 | 460 | { |
jsa1969 | 0:d5fb8cef7c31 | 461 | uint8_t alarmRegister[4]; |
jsa1969 | 0:d5fb8cef7c31 | 462 | uint8_t timeValue[4] = {second, minute, hour12, date}; |
jsa1969 | 0:d5fb8cef7c31 | 463 | uint8_t timeMin[4] = {0, 0, 0, 1}; |
jsa1969 | 0:d5fb8cef7c31 | 464 | uint8_t timeMax[4] = {59, 59, 23, 31}; |
jsa1969 | 0:d5fb8cef7c31 | 465 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 466 | timeMax[3] = 7; |
jsa1969 | 0:d5fb8cef7c31 | 467 | |
jsa1969 | 0:d5fb8cef7c31 | 468 | spiReadBytes(DS3234_REGISTER_A1SEC, alarmRegister, 4); // Read current alarm values |
jsa1969 | 0:d5fb8cef7c31 | 469 | |
jsa1969 | 0:d5fb8cef7c31 | 470 | // Run through all four alarm values and set their register values: |
jsa1969 | 0:d5fb8cef7c31 | 471 | for (int i=0; i<4; i++) |
jsa1969 | 0:d5fb8cef7c31 | 472 | { |
jsa1969 | 0:d5fb8cef7c31 | 473 | if (timeValue[i] == 255) // If 255, disable the check on that value |
jsa1969 | 0:d5fb8cef7c31 | 474 | alarmRegister[i] |= ALARM_MODE_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 475 | else if ((timeValue[i] >= timeMin[i]) && (timeValue[i] <= timeMax[i])) |
jsa1969 | 0:d5fb8cef7c31 | 476 | alarmRegister[i] = DECtoBCD(timeValue[i]); |
jsa1969 | 0:d5fb8cef7c31 | 477 | } |
jsa1969 | 0:d5fb8cef7c31 | 478 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 479 | alarmRegister[3] |= ALARM_DAY_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 480 | alarmRegister[2] |= TWELVE_HOUR_MODE; |
jsa1969 | 0:d5fb8cef7c31 | 481 | if (pm) alarmRegister[2] |= TWELVE_HOUR_PM; |
jsa1969 | 0:d5fb8cef7c31 | 482 | |
jsa1969 | 0:d5fb8cef7c31 | 483 | spiWriteBytes(DS3234_REGISTER_A1SEC, alarmRegister, 4); // Write the values |
jsa1969 | 0:d5fb8cef7c31 | 484 | } |
jsa1969 | 0:d5fb8cef7c31 | 485 | |
jsa1969 | 0:d5fb8cef7c31 | 486 | // setAlarm2 -- Alarm 2 can be set to trigger on minutes, hours, and/or date/day. |
jsa1969 | 0:d5fb8cef7c31 | 487 | // Any of those can be masked out -- ignored for an alarm match. By setting |
jsa1969 | 0:d5fb8cef7c31 | 488 | // If a value is set to 255, the library will mask out that data. |
jsa1969 | 0:d5fb8cef7c31 | 489 | // By default the "date" value is the day-of-month (1-31) |
jsa1969 | 0:d5fb8cef7c31 | 490 | // The "day" boolean changes the "date" value to a day (between 1-7). |
jsa1969 | 0:d5fb8cef7c31 | 491 | void DS3234::setAlarm2(uint8_t minute, uint8_t hour, uint8_t date, bool day) |
jsa1969 | 0:d5fb8cef7c31 | 492 | { |
jsa1969 | 0:d5fb8cef7c31 | 493 | uint8_t alarmRegister[3]; |
jsa1969 | 0:d5fb8cef7c31 | 494 | uint8_t timeValue[3] = {minute, hour, date}; |
jsa1969 | 0:d5fb8cef7c31 | 495 | uint8_t timeMin[3] = {0, 0, 1}; |
jsa1969 | 0:d5fb8cef7c31 | 496 | uint8_t timeMax[3] = {59, 23, 31}; |
jsa1969 | 0:d5fb8cef7c31 | 497 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 498 | timeMax[2] = 7; |
jsa1969 | 0:d5fb8cef7c31 | 499 | |
jsa1969 | 0:d5fb8cef7c31 | 500 | spiReadBytes(DS3234_REGISTER_A2MIN, alarmRegister, 3); // Read all alarm 2 registers |
jsa1969 | 0:d5fb8cef7c31 | 501 | |
jsa1969 | 0:d5fb8cef7c31 | 502 | for (int i=0; i<3; i++) |
jsa1969 | 0:d5fb8cef7c31 | 503 | { |
jsa1969 | 0:d5fb8cef7c31 | 504 | if (timeValue[i] == 255) // If a value is 255, disable that alarm check |
jsa1969 | 0:d5fb8cef7c31 | 505 | alarmRegister[i] |= ALARM_MODE_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 506 | else if ((timeValue[i] >= timeMin[i]) && (timeValue[i] <= timeMax[i])) |
jsa1969 | 0:d5fb8cef7c31 | 507 | alarmRegister[i] = DECtoBCD(timeValue[i]); |
jsa1969 | 0:d5fb8cef7c31 | 508 | } |
jsa1969 | 0:d5fb8cef7c31 | 509 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 510 | alarmRegister[2] |= ALARM_DAY_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 511 | |
jsa1969 | 0:d5fb8cef7c31 | 512 | spiWriteBytes(DS3234_REGISTER_A2MIN, alarmRegister, 3); |
jsa1969 | 0:d5fb8cef7c31 | 513 | } |
jsa1969 | 0:d5fb8cef7c31 | 514 | |
jsa1969 | 0:d5fb8cef7c31 | 515 | // setAlarm2 (12-hour mode) |
jsa1969 | 0:d5fb8cef7c31 | 516 | void DS3234::setAlarm2(uint8_t minute, uint8_t hour12, bool pm, uint8_t date, bool day) |
jsa1969 | 0:d5fb8cef7c31 | 517 | { |
jsa1969 | 0:d5fb8cef7c31 | 518 | uint8_t alarmRegister[3]; |
jsa1969 | 0:d5fb8cef7c31 | 519 | uint8_t timeValue[3] = {minute, hour12, date}; |
jsa1969 | 0:d5fb8cef7c31 | 520 | uint8_t timeMin[3] = {0, 0, 1}; |
jsa1969 | 0:d5fb8cef7c31 | 521 | uint8_t timeMax[3] = {59, 23, 31}; |
jsa1969 | 0:d5fb8cef7c31 | 522 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 523 | timeMax[2] = 7; |
jsa1969 | 0:d5fb8cef7c31 | 524 | |
jsa1969 | 0:d5fb8cef7c31 | 525 | spiReadBytes(DS3234_REGISTER_A2MIN, alarmRegister, 3); // Read all alarm 2 registers |
jsa1969 | 0:d5fb8cef7c31 | 526 | |
jsa1969 | 0:d5fb8cef7c31 | 527 | for (int i=0; i<3; i++) |
jsa1969 | 0:d5fb8cef7c31 | 528 | { |
jsa1969 | 0:d5fb8cef7c31 | 529 | if (timeValue[i] == 255) // If a value is 255, disable that alarm check |
jsa1969 | 0:d5fb8cef7c31 | 530 | alarmRegister[i] |= ALARM_MODE_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 531 | else if ((timeValue[i] >= timeMin[i]) && (timeValue[i] <= timeMax[i])) |
jsa1969 | 0:d5fb8cef7c31 | 532 | alarmRegister[i] = DECtoBCD(timeValue[i]); |
jsa1969 | 0:d5fb8cef7c31 | 533 | } |
jsa1969 | 0:d5fb8cef7c31 | 534 | if (day) |
jsa1969 | 0:d5fb8cef7c31 | 535 | alarmRegister[1] |= ALARM_DAY_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 536 | alarmRegister[1] |= TWELVE_HOUR_MODE; |
jsa1969 | 0:d5fb8cef7c31 | 537 | if (pm) alarmRegister[1] |= TWELVE_HOUR_PM; |
jsa1969 | 0:d5fb8cef7c31 | 538 | |
jsa1969 | 0:d5fb8cef7c31 | 539 | spiWriteBytes(DS3234_REGISTER_A2MIN, alarmRegister, 3); |
jsa1969 | 0:d5fb8cef7c31 | 540 | } |
jsa1969 | 0:d5fb8cef7c31 | 541 | |
jsa1969 | 0:d5fb8cef7c31 | 542 | // alarm1 -- Check the alarm 1 flag in the status register |
jsa1969 | 0:d5fb8cef7c31 | 543 | bool DS3234::alarm1(bool clear) |
jsa1969 | 0:d5fb8cef7c31 | 544 | { |
jsa1969 | 0:d5fb8cef7c31 | 545 | uint8_t statusRegister = spiReadByte(DS3234_REGISTER_STATUS); |
jsa1969 | 0:d5fb8cef7c31 | 546 | |
jsa1969 | 0:d5fb8cef7c31 | 547 | if (statusRegister & ALARM_1_FLAG_BIT) |
jsa1969 | 0:d5fb8cef7c31 | 548 | { |
jsa1969 | 0:d5fb8cef7c31 | 549 | if (clear) |
jsa1969 | 0:d5fb8cef7c31 | 550 | { |
jsa1969 | 0:d5fb8cef7c31 | 551 | statusRegister &= ~(ALARM_1_FLAG_BIT); |
jsa1969 | 0:d5fb8cef7c31 | 552 | spiWriteByte(DS3234_REGISTER_STATUS, statusRegister); |
jsa1969 | 0:d5fb8cef7c31 | 553 | } |
jsa1969 | 0:d5fb8cef7c31 | 554 | return true; |
jsa1969 | 0:d5fb8cef7c31 | 555 | } |
jsa1969 | 0:d5fb8cef7c31 | 556 | |
jsa1969 | 0:d5fb8cef7c31 | 557 | return false; |
jsa1969 | 0:d5fb8cef7c31 | 558 | } |
jsa1969 | 0:d5fb8cef7c31 | 559 | |
jsa1969 | 0:d5fb8cef7c31 | 560 | // alarm2 -- Check the alarm 2 flag in the status register |
jsa1969 | 0:d5fb8cef7c31 | 561 | bool DS3234::alarm2(bool clear) |
jsa1969 | 0:d5fb8cef7c31 | 562 | { |
jsa1969 | 0:d5fb8cef7c31 | 563 | uint8_t statusRegister = spiReadByte(DS3234_REGISTER_STATUS); |
jsa1969 | 0:d5fb8cef7c31 | 564 | |
jsa1969 | 0:d5fb8cef7c31 | 565 | if (statusRegister & ALARM_2_FLAG_BIT) |
jsa1969 | 0:d5fb8cef7c31 | 566 | { |
jsa1969 | 0:d5fb8cef7c31 | 567 | if (clear) |
jsa1969 | 0:d5fb8cef7c31 | 568 | { |
jsa1969 | 0:d5fb8cef7c31 | 569 | statusRegister &= ~(ALARM_2_FLAG_BIT); |
jsa1969 | 0:d5fb8cef7c31 | 570 | spiWriteByte(DS3234_REGISTER_STATUS, statusRegister); |
jsa1969 | 0:d5fb8cef7c31 | 571 | } |
jsa1969 | 0:d5fb8cef7c31 | 572 | return true; |
jsa1969 | 0:d5fb8cef7c31 | 573 | } |
jsa1969 | 0:d5fb8cef7c31 | 574 | |
jsa1969 | 0:d5fb8cef7c31 | 575 | return false; |
jsa1969 | 0:d5fb8cef7c31 | 576 | } |
jsa1969 | 0:d5fb8cef7c31 | 577 | |
jsa1969 | 0:d5fb8cef7c31 | 578 | // enableAlarmInterrupt -- Enable the SQW interrupt output on one, or both, alarms |
jsa1969 | 0:d5fb8cef7c31 | 579 | void DS3234::enableAlarmInterrupt(bool alarm1, bool alarm2) |
jsa1969 | 0:d5fb8cef7c31 | 580 | { |
jsa1969 | 0:d5fb8cef7c31 | 581 | uint8_t controlRegister = spiReadByte(DS3234_REGISTER_CONTROL); |
jsa1969 | 0:d5fb8cef7c31 | 582 | controlRegister |= ALARM_INTCN_BIT; |
jsa1969 | 0:d5fb8cef7c31 | 583 | if (alarm1) |
jsa1969 | 0:d5fb8cef7c31 | 584 | controlRegister |= (1<<0); |
jsa1969 | 0:d5fb8cef7c31 | 585 | if (alarm2) |
jsa1969 | 0:d5fb8cef7c31 | 586 | controlRegister |= (1<<1); |
jsa1969 | 0:d5fb8cef7c31 | 587 | spiWriteByte(DS3234_REGISTER_CONTROL, controlRegister); |
jsa1969 | 0:d5fb8cef7c31 | 588 | } |
jsa1969 | 0:d5fb8cef7c31 | 589 | |
jsa1969 | 0:d5fb8cef7c31 | 590 | // writeSQW -- Set the SQW pin high, low, or to one of the square wave frequencies |
jsa1969 | 0:d5fb8cef7c31 | 591 | void DS3234::writeSQW(sqw_rate value) |
jsa1969 | 0:d5fb8cef7c31 | 592 | { |
jsa1969 | 0:d5fb8cef7c31 | 593 | uint8_t controlRegister = spiReadByte(DS3234_REGISTER_CONTROL); |
jsa1969 | 0:d5fb8cef7c31 | 594 | |
jsa1969 | 0:d5fb8cef7c31 | 595 | controlRegister &= SQW_CONTROL_MASK; // Mask out RS1, RS2 bits (bits 3 and 4) |
jsa1969 | 0:d5fb8cef7c31 | 596 | controlRegister |= (value << 3); // Add rate bits, shift left 3 |
jsa1969 | 0:d5fb8cef7c31 | 597 | controlRegister &= ~(SQW_ENABLE_BIT); // Clear INTCN bit to enable SQW output |
jsa1969 | 0:d5fb8cef7c31 | 598 | spiWriteByte(DS3234_REGISTER_CONTROL, controlRegister); |
jsa1969 | 0:d5fb8cef7c31 | 599 | } |
jsa1969 | 0:d5fb8cef7c31 | 600 | |
jsa1969 | 0:d5fb8cef7c31 | 601 | // temperature -- Read the DS3234's die-temperature. |
jsa1969 | 0:d5fb8cef7c31 | 602 | // Value is produced in multiples of 0.25 deg C |
jsa1969 | 0:d5fb8cef7c31 | 603 | float DS3234::temperature(void) |
jsa1969 | 0:d5fb8cef7c31 | 604 | { |
jsa1969 | 0:d5fb8cef7c31 | 605 | float retVal = 0; |
jsa1969 | 0:d5fb8cef7c31 | 606 | int8_t integer; |
jsa1969 | 0:d5fb8cef7c31 | 607 | uint8_t tempRegister[2]; |
jsa1969 | 0:d5fb8cef7c31 | 608 | spiReadBytes(DS3234_REGISTER_TEMPM, tempRegister, 2); |
jsa1969 | 0:d5fb8cef7c31 | 609 | |
jsa1969 | 0:d5fb8cef7c31 | 610 | integer = tempRegister[0]; |
jsa1969 | 0:d5fb8cef7c31 | 611 | tempRegister[1] = tempRegister[1] >> 6; |
jsa1969 | 0:d5fb8cef7c31 | 612 | retVal = integer + ((float) tempRegister[1] * 0.25); |
jsa1969 | 0:d5fb8cef7c31 | 613 | |
jsa1969 | 0:d5fb8cef7c31 | 614 | return retVal; |
jsa1969 | 0:d5fb8cef7c31 | 615 | } |
jsa1969 | 0:d5fb8cef7c31 | 616 | |
jsa1969 | 0:d5fb8cef7c31 | 617 | // BCDtoDEC -- convert binary-coded decimal (BCD) to decimal |
jsa1969 | 0:d5fb8cef7c31 | 618 | uint8_t DS3234::BCDtoDEC(uint8_t val) |
jsa1969 | 0:d5fb8cef7c31 | 619 | { |
jsa1969 | 0:d5fb8cef7c31 | 620 | return ( ( val / 0x10) * 10 ) + ( val % 0x10 ); |
jsa1969 | 0:d5fb8cef7c31 | 621 | } |
jsa1969 | 0:d5fb8cef7c31 | 622 | |
jsa1969 | 0:d5fb8cef7c31 | 623 | // BCDtoDEC -- convert decimal to binary-coded decimal (BCD) |
jsa1969 | 0:d5fb8cef7c31 | 624 | uint8_t DS3234::DECtoBCD(uint8_t val) |
jsa1969 | 0:d5fb8cef7c31 | 625 | { |
jsa1969 | 0:d5fb8cef7c31 | 626 | return ( ( val / 10 ) * 0x10 ) + ( val % 10 ); |
jsa1969 | 0:d5fb8cef7c31 | 627 | } |
jsa1969 | 0:d5fb8cef7c31 | 628 | |
jsa1969 | 0:d5fb8cef7c31 | 629 | // spiWriteBytes -- write a set number of bytes to an SPI device, incrementing from a register |
jsa1969 | 0:d5fb8cef7c31 | 630 | void DS3234::spiWriteBytes(DS3234_registers reg, uint8_t * values, uint8_t len) |
jsa1969 | 0:d5fb8cef7c31 | 631 | { |
jsa1969 | 0:d5fb8cef7c31 | 632 | uint8_t writeReg = reg | 0x80; |
jsa1969 | 0:d5fb8cef7c31 | 633 | |
jsa1969 | 0:d5fb8cef7c31 | 634 | _csPin->setDigitalValue(0); |
jsa1969 | 0:d5fb8cef7c31 | 635 | spi.write(writeReg); |
jsa1969 | 0:d5fb8cef7c31 | 636 | for (int i=0; i<len; i++) |
jsa1969 | 0:d5fb8cef7c31 | 637 | { |
jsa1969 | 0:d5fb8cef7c31 | 638 | spi.write(values[i]); |
jsa1969 | 0:d5fb8cef7c31 | 639 | } |
jsa1969 | 0:d5fb8cef7c31 | 640 | _csPin->setDigitalValue(1); |
jsa1969 | 0:d5fb8cef7c31 | 641 | } |
jsa1969 | 0:d5fb8cef7c31 | 642 | |
jsa1969 | 0:d5fb8cef7c31 | 643 | // spiWriteBytes -- write a byte value to an spi device's register |
jsa1969 | 0:d5fb8cef7c31 | 644 | void DS3234::spiWriteByte(DS3234_registers reg, uint8_t value) |
jsa1969 | 0:d5fb8cef7c31 | 645 | { |
jsa1969 | 0:d5fb8cef7c31 | 646 | uint8_t writeReg = reg | 0x80; |
jsa1969 | 0:d5fb8cef7c31 | 647 | |
jsa1969 | 0:d5fb8cef7c31 | 648 | _csPin->setDigitalValue(0); |
jsa1969 | 0:d5fb8cef7c31 | 649 | spi.write(writeReg); |
jsa1969 | 0:d5fb8cef7c31 | 650 | spi.write(value); |
jsa1969 | 0:d5fb8cef7c31 | 651 | _csPin->setDigitalValue(1); |
jsa1969 | 0:d5fb8cef7c31 | 652 | } |
jsa1969 | 0:d5fb8cef7c31 | 653 | |
jsa1969 | 0:d5fb8cef7c31 | 654 | // spiWriteBytes -- read a byte from an spi device's register |
jsa1969 | 0:d5fb8cef7c31 | 655 | uint8_t DS3234::spiReadByte(DS3234_registers reg) |
jsa1969 | 0:d5fb8cef7c31 | 656 | { |
jsa1969 | 0:d5fb8cef7c31 | 657 | uint8_t retVal = 0; |
jsa1969 | 0:d5fb8cef7c31 | 658 | |
jsa1969 | 0:d5fb8cef7c31 | 659 | _csPin->setDigitalValue(0); |
jsa1969 | 0:d5fb8cef7c31 | 660 | |
jsa1969 | 0:d5fb8cef7c31 | 661 | spi.write(reg); |
jsa1969 | 0:d5fb8cef7c31 | 662 | retVal = spi.write(0x00); |
jsa1969 | 0:d5fb8cef7c31 | 663 | |
jsa1969 | 0:d5fb8cef7c31 | 664 | _csPin->setDigitalValue(1); |
jsa1969 | 0:d5fb8cef7c31 | 665 | |
jsa1969 | 0:d5fb8cef7c31 | 666 | return retVal; |
jsa1969 | 0:d5fb8cef7c31 | 667 | } |
jsa1969 | 0:d5fb8cef7c31 | 668 | |
jsa1969 | 0:d5fb8cef7c31 | 669 | // spiWriteBytes -- read a set number of bytes from an spi device, incrementing from a register |
jsa1969 | 0:d5fb8cef7c31 | 670 | void DS3234::spiReadBytes(DS3234_registers reg, uint8_t * dest, uint8_t len) |
jsa1969 | 0:d5fb8cef7c31 | 671 | { |
jsa1969 | 0:d5fb8cef7c31 | 672 | _csPin->setDigitalValue(0); |
jsa1969 | 0:d5fb8cef7c31 | 673 | spi.write(reg); |
jsa1969 | 0:d5fb8cef7c31 | 674 | for (int i=0; i<len; i++) |
jsa1969 | 0:d5fb8cef7c31 | 675 | { |
jsa1969 | 0:d5fb8cef7c31 | 676 | dest[i] = spi.write(0x00); |
jsa1969 | 0:d5fb8cef7c31 | 677 | } |
jsa1969 | 0:d5fb8cef7c31 | 678 | _csPin->setDigitalValue(1); |
jsa1969 | 0:d5fb8cef7c31 | 679 | } |
jsa1969 | 0:d5fb8cef7c31 | 680 | |
jsa1969 | 0:d5fb8cef7c31 | 681 | void DS3234::writeToSRAM(uint8_t address, uint8_t data){ |
jsa1969 | 0:d5fb8cef7c31 | 682 | spiWriteByte(DS3234_REGISTER_SRAMA, address); |
jsa1969 | 0:d5fb8cef7c31 | 683 | spiWriteByte(DS3234_REGISTER_SRAMD, data); |
jsa1969 | 0:d5fb8cef7c31 | 684 | } |
jsa1969 | 0:d5fb8cef7c31 | 685 | |
jsa1969 | 0:d5fb8cef7c31 | 686 | uint8_t DS3234::readFromSRAM(uint8_t address){ |
jsa1969 | 0:d5fb8cef7c31 | 687 | spiWriteByte(DS3234_REGISTER_SRAMA, address); |
jsa1969 | 0:d5fb8cef7c31 | 688 | return spiReadByte(DS3234_REGISTER_SRAMD); |
jsa1969 | 0:d5fb8cef7c31 | 689 | } |
jsa1969 | 0:d5fb8cef7c31 | 690 | |
jsa1969 | 0:d5fb8cef7c31 | 691 | |
jsa1969 | 0:d5fb8cef7c31 | 692 | DS3234 rtc; // Use rtc in sketches |