DS3231.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. DS3231.cpp - Arduino/chipKit library support for the DS3231 I2C Real-Time Clock
  3. Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
  4. This library has been made to easily interface and use the DS3231 RTC with
  5. an Arduino or chipKit.
  6. You can find the latest version of the library at
  7. http://www.RinkyDinkElectronics.com/
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the CC BY-NC-SA 3.0 license.
  10. Please see the included documents for further information.
  11. Commercial use of this library requires you to buy a license that
  12. will allow commercial use. This includes using the library,
  13. modified or not, as a tool to sell products.
  14. The license applies to all part of the library including the
  15. examples and tools supplied with the library.
  16. */
  17. #ifndef DS3231_h
  18. #define DS3231_h
  19. #if defined(__AVR__)
  20. #include "Arduino.h"
  21. #include "hardware/avr/HW_AVR_defines.h"
  22. #elif defined(__PIC32MX__)
  23. #include "WProgram.h"
  24. #include "hardware/pic32/HW_PIC32_defines.h"
  25. #elif defined(__arm__)
  26. #include "Arduino.h"
  27. #include "hardware/arm/HW_ARM_defines.h"
  28. #endif
  29. #define DS3231_ADDR_R 0xD1
  30. #define DS3231_ADDR_W 0xD0
  31. #define DS3231_ADDR 0x68
  32. #define FORMAT_SHORT 1
  33. #define FORMAT_LONG 2
  34. #define FORMAT_LITTLEENDIAN 1
  35. #define FORMAT_BIGENDIAN 2
  36. #define FORMAT_MIDDLEENDIAN 3
  37. #define MONDAY 1
  38. #define TUESDAY 2
  39. #define WEDNESDAY 3
  40. #define THURSDAY 4
  41. #define FRIDAY 5
  42. #define SATURDAY 6
  43. #define SUNDAY 7
  44. #define SQW_RATE_1 0
  45. #define SQW_RATE_1K 1
  46. #define SQW_RATE_4K 2
  47. #define SQW_RATE_8K 3
  48. #define OUTPUT_SQW 0
  49. #define OUTPUT_INT 1
  50. class Time
  51. {
  52. public:
  53. uint8_t hour;
  54. uint8_t min;
  55. uint8_t sec;
  56. uint8_t date;
  57. uint8_t mon;
  58. uint16_t year;
  59. uint8_t dow;
  60. Time();
  61. };
  62. class DS3231
  63. {
  64. public:
  65. DS3231(uint8_t data_pin, uint8_t sclk_pin);
  66. void begin();
  67. Time getTime();
  68. void setTime(uint8_t hour, uint8_t min, uint8_t sec);
  69. void setAlarm1Time(uint8_t hour, uint8_t min);
  70. char *getAlarm1Str(uint8_t format=FORMAT_LONG);
  71. void setControl();
  72. void resetAlarm();
  73. void setDate(uint8_t date, uint8_t mon, uint16_t year);
  74. void setDOW();
  75. void setDOW(uint8_t dow);
  76. char *getTimeStr(uint8_t format=FORMAT_LONG);
  77. char *getDateStr(uint8_t slformat=FORMAT_LONG, uint8_t eformat=FORMAT_LITTLEENDIAN, char divider='.');
  78. char *getDOWStr(uint8_t format=FORMAT_LONG);
  79. char *getMonthStr(uint8_t format=FORMAT_LONG);
  80. long getUnixTime(Time t);
  81. void enable32KHz(bool enable);
  82. void setOutput(byte enable);
  83. void setSQWRate(int rate);
  84. float getTemp();
  85. private:
  86. uint8_t _scl_pin;
  87. uint8_t _sda_pin;
  88. uint8_t _burstArray[7];
  89. boolean _use_hw;
  90. void _sendStart(byte addr);
  91. void _sendStop();
  92. void _sendAck();
  93. void _sendNack();
  94. void _waitForAck();
  95. uint8_t _readByte();
  96. void _writeByte(uint8_t value);
  97. void _burstRead();
  98. uint8_t _readRegister(uint8_t reg);
  99. void _writeRegister(uint8_t reg, uint8_t value);
  100. uint8_t _decode(uint8_t value);
  101. uint8_t _decodeH(uint8_t value);
  102. uint8_t _decodeY(uint8_t value);
  103. uint8_t _encode(uint8_t vaule);
  104. #if defined(__arm__)
  105. Twi *twi;
  106. #endif
  107. };
  108. #endif