RTC - Read from DS1307/DS3231 RTC module

Preparation

  • Ameba x 1
  • DS1307/DS3231 RTC x 1

Example

DS1307 is a common RTC IC, and is used in TinyRTC. If you higher precision is required, DS3231 can be used instead.
Please download following libraries first,
DS1307 RTC library:
https://github.com/ambiot/amb1_arduino/raw/master/Arduino_libraries/DS1307RTC-1.0.0.zip
Follow the tutorial to install the .zip library to Ameba:

https://www.arduino.cc/en/Guide/Libraries#toc4

The RTC library supports DS1307, DS1337 and DS3231. It uses I2C interface, and works at 3.3V.
Please open the sample code from “File” -> “Examples” -> “AmebaDS1307RTC” -> “ReadTest”
In this example, we read time value from DS1307/DS3231.
If you are using TinyRTC (DS1307), please do the wiring as the figure below:
1

If you use DS3231, please refer to the wiring diagram:
2

RTL8710,wiring diagram as follows:
2

Then compile and upload the sample to Ameba and press reset button. You should see similar log in the Serial Monitor:
3

Code Reference

In loop(), we declare a variable with type struct tmElements_t to store the time-related data.

tmElements_t tm;

Next, call RTC.read() to store the data into the tmElements_t variable.

RTC.read(tm)

The data structure in tmElements_t:

typedef struct  { 
  uint8_t Second; 
  uint8_t Minute; 
  uint8_t Hour; 
  uint8_t Wday;   // day of week, sunday is day 1
  uint8_t Day;
  uint8_t Month; 
  uint8_t Year;   // offset from 1970; 
}  tmElements_t, TimeElements, *tmElementsPtr_t;
Please confirm that QQ communication software is installed