個体差もあるので必ずとも言えないようだがDS3231は頻繁にリードするほど(1秒に一回程度でも)時刻誤差が増大してしまう。誤差を増大させないようにするにはリード頻度を下げるしかないため最初に一度だけリードした後はDS3231のSQW(1Hz)信号によるGPIOピン割込みで時計を更新できるようにしたライブラリを作成してみた。
SQW(1Hz)信号を利用するとアラーム割込みにも対応できる。begin()にてSQW信号の接続先GPIOピンを指定するだけでよく、無指定(0xFFFF)の場合は毎回リード(誤差増大)するようになりアラーム割込みのサポートも無しとなる。
全てスタティック・メソッドなので本来はインスタンス生成する必要はないが、テンプレート・クラス特有のクラス指定の煩わしさ(名前が長くなる)があるためインスタンス変数経由で利用したほうが簡単。かも。
【スケッチ】
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 |
#include <Wire.> #include "ds3231.h" #define DS3231_SQW_PIN 0xFFFF DECLARE_DS3231(Wire) rtc; const char *WDAYS[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; int second = -1; void setup(void) { Serial.begin(115200); rtc.begin(DS3231_SQW_PIN); #if 0 /* Write RTC */ rtc.tmbuf.tm_year = 2024 - 1900; rtc.tmbuf.tm_mon = 9 - 1; rtc.tmbuf.tm_mday = 9; rtc.tmbuf.tm_hour = 12; rtc.tmbuf.tm_min = 0; rtc.tmbuf.tm_sec = 0; rtc.setCalendar(rtc.tmbuf); #endif } void loop(void) { rtc.getCalendar(rtc.tmbuf); if (second != rtc.tmbuf.tm_sec) { second = rtc.tmbuf.tm_sec; Serial.print(rtc.tmbuf.tm_year + 1900); Serial.print("-"); Serial.print(rtc.tmbuf.tm_mon + 1); Serial.print("-"); Serial.print(rtc.tmbuf.tm_mday); Serial.print("("); Serial.print(WDAYS[rtc.tmbuf.tm_wday]); Serial.print(") "); Serial.print(rtc.tmbuf.tm_hour); Serial.print(":"); Serial.print(rtc.tmbuf.tm_min); Serial.print(":"); Serial.println(rtc.tmbuf.tm_sec); } } |
【ライブラリ】
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
/* ds3231.h - DS3231/DS3232 RTC Library for ARDUINO Copyright (c) 2024 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include <stdint.h> #include <stdbool.h> #include <time.h> #if !defined(ARDUINO_ARCH_AVR) #include <functional> #endif #if defined(JENNIC_CHIP_FAMILY_JN516x) #undef _BIT_FIELDS_HTOL #define _BIT_FIELDS_HTOL #endif #define DECLARE_DS3231(instance) DS3231<typeof(instance), instance> template<typename T, T& WIRE> class DS3231 { public: static constexpr uint32_t WIRE_CLOCK = 400000; enum { ERR_NONE = 0, ERR_IO = 4, ERR_ARG = 8, }; typedef enum { SQWRATE_1, SQWRATE_1024, SQWRATE_4096, SQWRATE_8192, } SQWRATE; typedef enum { CNVRATE_64, CNVRATE_128, CNVRATE_256, CNVRATE_512, } CNVRATE; typedef struct __attribute((packed)) { #if defined(_BIT_FIELDS_HTOL) uint8_t OSF : 1; /* RW[1]: Oscillator Stop Flag */ uint8_t BB32KHz : 1; /* RW[1]: Battery-Backed 32KHz Output [DS3232] */ CNVRATE CRATE : 2; /* RW[CNVRATE_64]: Conversion Rate [DS3232] */ uint8_t EN32KHz : 1; /* RW[1]: Enable 32KHz Output */ uint8_t BSY : 1; /* RO[x]: Busy */ uint8_t A2F : 1; /* RW[x]: Alarm 2 Flag */ uint8_t A1F : 1; /* RW[x]: Alarm 1 Flag */ #else uint8_t A1F : 1; /* RW[x]: Alarm 1 Flag */ uint8_t A2F : 1; /* RW[x]: Alarm 2 Flag */ uint8_t BSY : 1; /* RO[x]: Busy */ uint8_t EN32KHz : 1; /* RW[1]: Enable 32KHz Output */ CNVRATE CRATE : 2; /* RW[CNVRATE_64]: Conversion Rate [DS3232] */ uint8_t BB32KHz : 1; /* RW[1]: Battery-Backed 32KHz Output [DS3232] */ uint8_t OSF : 1; /* RW[1]: Oscillator Stop Flag */ #endif } status_t; #if defined(ARDUINO_ARCH_AVR) typedef void (*callback_t)(status_t status); #else typedef std::function<void(status_t status)> callback_t; #endif /* Free to use variables */ static inline struct tm tmbuf; static inline status_t status; static inline int8_t agingoffset; static inline int16_t temperature; static bool begin(uint16_t pin = PIN_NC) { /* Initialize Wire with setup() */ _errcode = 0; _callback = nullptr; _pinsqw = PIN_NC; bool rv = read(offsetof(regmap_t, Alarm1), &_regmap.Alarm1, offsetof(regmap_t, Temperature) - offsetof(regmap_t, Alarm1)); if (rv && (pin != PIN_NC)) { rv = outputSQW(SQWRATE_1); if (rv) rv = emuration(pin); } return rv; } static void setCallback(callback_t callback) { /* Only valid in emulation mode */ _callback = callback; } static bool getCalendar(struct tm &tmbuf) { bool rv = true; if (_pinsqw == PIN_NC) { rv = read(offsetof(regmap_t, Calendar), &_regmap.Calendar, sizeof(_regmap.Calendar)); if (rv) toBinary(_regmap.Calendar); } if (rv) { do { tmbuf = { .tm_sec = _regmap.Calendar.Seconds, .tm_min = _regmap.Calendar.Minutes, .tm_hour = _regmap.Calendar.Hour, .tm_mday = _regmap.Calendar.Date, .tm_mon = _regmap.Calendar.Month - 1, .tm_year = _regmap.Calendar.Year + 100, .tm_wday = _regmap.Calendar.Day - 1, .tm_yday = dayOfYear(_regmap.Calendar), .tm_isdst = 0 }; } while (tmbuf.tm_sec != _regmap.Calendar.Seconds); } return rv; } static bool setCalendar(const struct tm &tmbuf) { bool rv = (tmbuf.tm_year >= 100) && (tmbuf.tm_year < 200); if (!rv) _errcode = ERR_ARG; else { calendar_t cal = { .Seconds = bin2bcd(tmbuf.tm_sec), .Minutes = bin2bcd(tmbuf.tm_min), .Hour = bin2bcd(tmbuf.tm_hour), .Day = bin2bcd(dayOfWeek(tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday) + 1), .Date = bin2bcd(tmbuf.tm_mday), .Month = bin2bcd(tmbuf.tm_mon + 1), .Year = bin2bcd(tmbuf.tm_year - 100) }; rv = write(offsetof(regmap_t, Calendar), &cal, sizeof(cal)); if (rv && (_pinsqw != PIN_NC)) toBinary(cal); } return rv; } static bool getAlarm1(struct tm &tmbuf) { bool rv = true || read(offsetof(regmap_t, Alarm1), &_regmap.Alarm1, sizeof(_regmap.Alarm1)); if (rv) { tmbuf = { .tm_sec = _regmap.Alarm1.Seconds & ALMMSK ? -1 : bcd2bin(_regmap.Alarm1.Seconds), .tm_min = _regmap.Alarm1.Minutes & ALMMSK ? -1 : bcd2bin(_regmap.Alarm1.Minutes), .tm_hour = _regmap.Alarm1.Hour & ALMMSK ? -1 : bcd2bin(_regmap.Alarm1.Hour), .tm_mday = _regmap.Alarm1.DayOrDate & (ALMMSK | ALMDAY) ? -1 : bcd2bin(_regmap.Alarm1.DayOrDate), .tm_mon = -1, .tm_year = -1, .tm_wday = _regmap.Alarm1.DayOrDate & ALMMSK ? -1 : bcd2bin(_regmap.Alarm1.DayOrDate & ~ALMDAY) - 1, .tm_yday = -1, .tm_isdst = -1 }; }; return rv; } static bool setAlarm1(const struct tm &tmbuf, bool week, bool enable = true) { alarm1_t alarm = { .Seconds = tmbuf.tm_sec < 0 ? ALMMSK : bin2bcd(tmbuf.tm_sec), .Minutes = tmbuf.tm_min < 0 ? ALMMSK : bin2bcd(tmbuf.tm_min), .Hour = tmbuf.tm_hour < 0 ? ALMMSK : bin2bcd(tmbuf.tm_hour), .DayOrDate = (uint8_t)(week ? (tmbuf.tm_wday < 0 ? ALMMSK : bin2bcd(tmbuf.tm_wday + 1) | ALMDAY) : (tmbuf.tm_mday <= 0 ? ALMMSK : bin2bcd(tmbuf.tm_mday))) }; bool rv = write(offsetof(regmap_t, Alarm1), &alarm, sizeof(alarm)); if (rv) { _regmap.Alarm1 = alarm; rv = alarm1(enable); } return rv; } static bool alarm1(bool enable) { control_t control; bool rv = getControl(control); if (rv) { control.A1IE = enable; control.INTCN = _pinsqw == PIN_NC; rv = setControl(control); } return rv; } static bool getAlarm2(struct tm &tmbuf) { bool rv = true || read(offsetof(regmap_t, Alarm2), &_regmap.Alarm2, sizeof(_regmap.Alarm2)); if (rv) { tmbuf = { .tm_sec = -1, .tm_min = _regmap.Alarm2.Minutes & ALMMSK ? -1 : bcd2bin(_regmap.Alarm2.Minutes), .tm_hour = _regmap.Alarm2.Hour & ALMMSK ? -1 : bcd2bin(_regmap.Alarm2.Hour), .tm_mday = _regmap.Alarm2.DayOrDate & (ALMMSK | ALMDAY) ? -1 : bcd2bin(_regmap.Alarm2.DayOrDate), .tm_mon = -1, .tm_year = -1, .tm_wday = _regmap.Alarm2.DayOrDate & ALMMSK ? -1 : bcd2bin(_regmap.Alarm2.DayOrDate & ~ALMDAY) - 1, .tm_yday = -1, .tm_isdst = -1 }; }; return rv; } static bool setAlarm2(const struct tm &tmbuf, bool week, bool enable = true) { alarm2_t alarm = { .Minutes = tmbuf.tm_min < 0 ? ALMMSK : bin2bcd(tmbuf.tm_min), .Hour = tmbuf.tm_hour < 0 ? ALMMSK : bin2bcd(tmbuf.tm_hour), .DayOrDate = (uint8_t)(week ? (tmbuf.tm_wday < 0 ? ALMMSK : bin2bcd(tmbuf.tm_wday + 1) | ALMDAY) : (tmbuf.tm_mday <= 0 ? ALMMSK : bin2bcd(tmbuf.tm_mday))) }; bool rv = write(offsetof(regmap_t, Alarm2), &alarm, sizeof(alarm)); if (rv) { _regmap.Alarm2 = alarm; rv = alarm2(enable); } return rv; } static bool alarm2(bool enable) { control_t control; bool rv = getControl(control); if (rv) { control.A2IE = enable; control.INTCN = _pinsqw == PIN_NC; rv = setControl(control); } return rv; } static bool getStatus(status_t &value) { value = {}; bool rv = read(offsetof(regmap_t, Status), &_regmap.Status, sizeof(_regmap.Status)); if (rv) value = _regmap.Status; return rv; } static bool setStatus(status_t value) { bool rv = write(offsetof(regmap_t, Status), &value, sizeof(value)); if (rv) _regmap.Status = value; return rv; } static bool getAgingOffset(int8_t &value) { bool rv = true || read(offsetof(regmap_t, AgingOffset), &_regmap.AgingOffset, sizeof(_regmap.AgingOffset)); if (rv) value = _regmap.AgingOffset; return rv; } static bool setAgingOffset(int8_t value, bool conv = true) { bool rv = write(offsetof(regmap_t, AgingOffset), &value, sizeof(value)); if (rv) { _regmap.AgingOffset = value; if (conv) rv = conversion(); } return rv; } static bool getTemperature(int16_t &value) { bool rv = read(offsetof(regmap_t, Temperature), &_regmap.Temperature, sizeof(_regmap.Temperature)); if (rv) value = (((int16_t)_regmap.Temperature.MSB << 8) | _regmap.Temperature.LSB) >> 6; return rv; } static bool oscillator(bool enable = true) { control_t control; bool rv = getControl(control); if (rv) { control.EOSC = !enable; rv = setControl(control); } return rv; } static bool getStopflag(bool &stopped) { status_t status = {}; bool rv = getStatus(status); stopped = status.OSF; if (rv && status.OSF) { status.OSF = 0; setStatus(status); } return rv; } static bool conversion(void) { control_t control; bool rv = getControl(control); if (rv) { control.CONV = 1; rv = setControl(control); } return rv; } static bool outputSQW(SQWRATE rate = SQWRATE_8192, bool BBSQW = false) { control_t control; bool rv = getControl(control); if (rv) { control.RS = rate; control.INTCN = 0; control.BBSQW = BBSQW; rv = setControl(control); } return rv; } static bool output32KHz(bool enable, bool BB32KHz = false /* [DS3232] */) { status_t status; bool rv = getStatus(status); if (rv) { status.EN32KHz = enable; status.BB32KHz = BB32KHz; /* [DS3232] */ rv = setStatus(status); } return rv; } static bool emuration(uint16_t pin = PIN_NC) { cancel(); if ((pin != PIN_NC) && (_regmap.Control.RS == SQWRATE_1) && (_regmap.Control.INTCN == 0)) { if (digitalPinToInterrupt(pin) != NOT_AN_INTERRUPT) { _regmap.Calendar.Seconds = 0; pinMode(pin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(pin), update, FALLING); for (uint32_t t = millis(); millis() - t <= 1000; yield()) { if (_regmap.Calendar.Seconds) { struct tm tmbuf; if (getCalendar(tmbuf)) { _pinsqw = pin; return true; } break; } } detachInterrupt(digitalPinToInterrupt(pin)); } } return false; } /* [DS3232] */ static bool setConversionRate(CNVRATE rate = CNVRATE_64) { status_t status; bool rv = getStatus(status); if (rv) { status.CRATE = rate; rv = setStatus(status); } return rv; } /* [DS3232] */ static bool getSRAM(uint8_t addr, void *buf, size_t len) { bool rv = ((addr + len <= sizeof(_regmap.SRAM)) && (buf || !len)); if (!rv) _errcode = ERR_ARG; else if (len) rv = read(offsetof(regmap_t, SRAM) + addr, buf, len); return rv; } /* [DS3232] */ static bool setSRAM(uint8_t addr, const void *buf, size_t len) { bool rv = ((addr + len <= sizeof(_regmap.SRAM)) && (buf || !len)); if (!rv) _errcode = ERR_ARG; else if (len) rv = write(offsetof(regmap_t, SRAM) + addr, buf, len); return rv; } static int errcode(void) { return _errcode; } private: static constexpr uint8_t DEVICE = 0x68; static constexpr uint16_t PIN_NC = 0xFFFF; static constexpr uint8_t CENTURY = 0x80; static constexpr uint8_t ALMMSK = 0x80; static constexpr uint8_t ALMDAY = 0x40; typedef struct __attribute__((packed)) { uint8_t Seconds; uint8_t Minutes; uint8_t Hour; uint8_t Day; uint8_t Date; uint8_t Month; uint8_t Year; } calendar_t; typedef struct __attribute__((packed)) { uint8_t Seconds; uint8_t Minutes; uint8_t Hour; uint8_t DayOrDate; } alarm1_t; typedef struct __attribute__((packed)) { uint8_t Minutes; uint8_t Hour; uint8_t DayOrDate; } alarm2_t; typedef struct __attribute__((packed)) { int8_t MSB; uint8_t LSB; } temperature_t; typedef struct __attribute((packed)) { #if defined(_BIT_FIELDS_HTOL) uint8_t EOSC : 1; /* RW[0]: Enable Oscillator */ uint8_t BBSQW : 1; /* RW[0]: Battery-Backed Square-Wave Enable */ uint8_t CONV : 1; /* RW[0]: Convert Temperature */ SQWRATE RS : 2; /* RW[SQWFREQ_8192]: Rate Select */ uint8_t INTCN : 1; /* RW[1]: Interrupt Control */ uint8_t A2IE : 1; /* RW[0]: Alarm 2 Interrupt Enable */ uint8_t A1IE : 1; /* RW[0]: Alarm 1 Interrupt Enable */ #else uint8_t A1IE : 1; /* RW[0]: Alarm 1 Interrupt Enable */ uint8_t A2IE : 1; /* RW[0]: Alarm 2 Interrupt Enable */ uint8_t INTCN : 1; /* RW[1]: Interrupt Control */ SQWRATE RS : 2; /* RW[SQWFREQ_8192]: Rate Select */ uint8_t CONV : 1; /* RW[0]: Convert Temperature */ uint8_t BBSQW : 1; /* RW[0]: Battery-Backed Square-Wave Enable */ uint8_t EOSC : 1; /* RW[0]: Enable Oscillator */ #endif } control_t; typedef struct __attribute__((packed)) { calendar_t Calendar; alarm1_t Alarm1; alarm2_t Alarm2; control_t Control; status_t Status; int8_t AgingOffset; temperature_t Temperature; uint8_t Reserved; /* [DS3232] */ uint8_t SRAM[236]; /* [DS3232] */ } regmap_t; /* static inline is C++17 and later */ static inline int _errcode; static inline callback_t _callback; static inline uint16_t _pinsqw; static inline regmap_t _regmap; static bool read(uint8_t addr, void *buf, size_t len) { if (!write(addr, nullptr, 0, false)) return false; WIRE.requestFrom(DEVICE, len); return (_errcode = WIRE.readBytes((uint8_t *)buf, len) == len ? ERR_NONE : ERR_IO) == ERR_NONE; } static bool write(uint8_t addr, const void *buf, size_t len, uint8_t sendStop = true) { WIRE.beginTransmission(DEVICE); WIRE.write(&addr, sizeof(addr)); if (buf && len) WIRE.write((const uint8_t *)buf, len); return (_errcode = WIRE.endTransmission(sendStop)) == ERR_NONE; } static bool getControl(control_t &value) { bool rv = true || read(offsetof(regmap_t, Control), &_regmap.Control, sizeof(_regmap.Control)); if (rv) value = _regmap.Control; return rv; } static bool setControl(control_t value) { bool rv = write(offsetof(regmap_t, Control), &value, sizeof(value)); if (rv) { _regmap.Control = value; if ((value.RS != SQWRATE_1) || value.INTCN) cancel(); } return rv; } static bool isLeapYear(int year) { return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))); } static int dayOfYear(calendar_t &cal) { static constexpr int YDAYS[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; return YDAYS[cal.Month - 1] + cal.Date + ((cal.Month >= 3) && isLeapYear(cal.Year + 2000)) - 1; } static int dayOfMonth(calendar_t &cal) { static constexpr int MDAYS[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; return MDAYS[cal.Month - 1] + ((cal.Month == 2) && isLeapYear(cal.Year + 2000)); } static uint8_t dayOfWeek(uint32_t year, uint8_t month, uint8_t day) { /* Day is calculated with hypothesis that year > 2000 */ /* 0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat */ return month < 3 ? (((23 * month) / 9) + day + 4 + year + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400) ) % 7 : (((23 * month) / 9) + day + 4 + year + (year / 4) - (year / 100) + (year / 400) - 2) % 7; } static uint8_t bin2bcd(uint8_t n) { return n + 6 * (n / 10); } static uint8_t bcd2bin(uint8_t n) { return n - 6 * (n >> 4); } static void toBinary(calendar_t &cal) { _regmap.Calendar = { .Seconds = bcd2bin(cal.Seconds), .Minutes = bcd2bin(cal.Minutes), .Hour = bcd2bin(cal.Hour), .Day = bcd2bin(cal.Day), .Date = bcd2bin(cal.Date), .Month = bcd2bin(cal.Month & ~CENTURY), .Year = bcd2bin(cal.Year) }; } static void cancel(void) { if (_pinsqw != PIN_NC) detachInterrupt(_pinsqw); _pinsqw = PIN_NC; } static void update(void) { status_t status = {}; if (++_regmap.Calendar.Seconds > 59) { _regmap.Calendar.Seconds = 0; if (++_regmap.Calendar.Minutes > 59) { _regmap.Calendar.Minutes = 0; if (++_regmap.Calendar.Hour > 23) { _regmap.Calendar.Hour = 0; if (++_regmap.Calendar.Day > 7) _regmap.Calendar.Day = 1; if (++_regmap.Calendar.Date > dayOfMonth(_regmap.Calendar)) { _regmap.Calendar.Date = 1; if (++_regmap.Calendar.Month > 12) { _regmap.Calendar.Month = 1; if (++_regmap.Calendar.Year > 99) _regmap.Calendar.Year = 0; } } } } status.A2F = _regmap.Control.A2IE && ((_regmap.Alarm2.Minutes & ALMMSK) || (bcd2bin(_regmap.Alarm2.Minutes) == _regmap.Calendar.Minutes)) && ((_regmap.Alarm2.Hour & ALMMSK) || (bcd2bin(_regmap.Alarm2.Hour ) == _regmap.Calendar.Hour )) && ((_regmap.Alarm2.DayOrDate & ALMMSK) || (bcd2bin(_regmap.Alarm2.DayOrDate & ~ALMDAY) == (_regmap.Alarm2.DayOrDate & ALMDAY ? _regmap.Calendar.Day : _regmap.Calendar.Date))); } status.A1F = _regmap.Control.A1IE && ((_regmap.Alarm1.Seconds & ALMMSK) || (bcd2bin(_regmap.Alarm1.Seconds) == _regmap.Calendar.Seconds)) && ((_regmap.Alarm1.Minutes & ALMMSK) || (bcd2bin(_regmap.Alarm1.Minutes) == _regmap.Calendar.Minutes)) && ((_regmap.Alarm1.Hour & ALMMSK) || (bcd2bin(_regmap.Alarm1.Hour ) == _regmap.Calendar.Hour )) && ((_regmap.Alarm1.DayOrDate & ALMMSK) || (bcd2bin(_regmap.Alarm1.DayOrDate & ~ALMDAY) == (_regmap.Alarm1.DayOrDate & ALMDAY ? _regmap.Calendar.Day : _regmap.Calendar.Date))); if ((status.A1F || status.A2F) && _callback) _callback(status); } }; |