“RTC 3231”的版本间的差异

来自YFRobotwiki
跳转至: 导航搜索
(以“ DS3231是低成本、高精度I²C实时时钟(RTC),具有集成的温补晶振(TCXO)和晶体。该器件包含电池输入端,断开主电源时仍可保持...”为内容创建页面)
 
第30行: 第30行:
  
 
[[RTC DS1307 DS3231库]]
 
[[RTC DS1307 DS3231库]]
 +
 +
==应用示例==
 +
<br>
 +
:'''电路连接'''
 +
<br>
 +
{|border="1" cellspacing="0" align="center" cellpadding="5" width="450px"
 +
|-
 +
|align="left"|'''RTC3231时钟模块'''
 +
|align="left"|'''Arduino UNO'''
 +
|-
 +
|align="left"|VCC
 +
|align="left"|+5V
 +
|-
 +
|align="left"|GND
 +
|align="left"|GND
 +
|-
 +
|align="left"|SDA
 +
|align="left"|SDA/A4
 +
|-
 +
|align="left"|SCL
 +
|align="left"|SCL/A5
 +
|}
 +
 +
 +
<!--[[Image: RTC3231时钟模块.jpg|400px|center|RTC3231时钟模块接线图]]-->
 +
 +
 +
 +
:'''示例代码'''
 +
<pre >
 +
 +
/* arduino sketch -- arduino print date and time with ds3231 RTC breakout
 +
  SETUP:
 +
    arduino (UNO R3)    ds3231
 +
      A4/SDA          →    SDA
 +
      A5/SCL          →    SCL
 +
      GND             →    GND
 +
 +
  Read More about DS3231: http://www.yfrobot.com/wiki/index.php?title=RTC_3231
 +
 +
  BY YFROBOT
 +
*/
 +
 +
#include <Wire.h>  // must be incuded here so that Arduino library object file references work
 +
#include <RtcDS3231.h>
 +
 +
RtcDS3231 Rtc;
 +
 +
void setup ()
 +
{
 +
  Serial.begin(115200);
 +
 +
  Serial.print("compiled: ");
 +
  Serial.print(__DATE__);      // 编译文档的日期
 +
  Serial.println(__TIME__);    // 编译文档的时间
 +
 +
  //--------RTC SETUP ------------
 +
  Rtc.Begin();
 +
 +
  // if you are using ESP-01 then uncomment the line below to reset the pins to
 +
  // the available pins for SDA, SCL
 +
  // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
 +
 +
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); // 存储当前编译的时间及日期并打印
 +
  printDateTime(compiled);
 +
  Serial.println();
 +
 +
  if (!Rtc.IsDateTimeValid())
 +
  {
 +
    // Common Cuases:
 +
    //    1) first time you ran and the device wasn't running yet
 +
    //    2) the battery on the device is low or even missing
 +
 +
    Serial.println("RTC lost confidence in the DateTime!");
 +
 +
    // following line sets the RTC to the date & time this sketch was compiled
 +
    // it will also reset the valid flag internally unless the Rtc device is
 +
    // having an issue
 +
 +
    Rtc.SetDateTime(compiled);
 +
  }
 +
 +
  if (!Rtc.GetIsRunning())
 +
  {
 +
    Serial.println("RTC was not actively running, starting now");
 +
    Rtc.SetIsRunning(true);
 +
  }
 +
 +
  RtcDateTime now = Rtc.GetDateTime();  // 获取当前DS3231时间及日期
 +
  if (now < compiled)
 +
  {
 +
    Serial.println("RTC is older than compile time!  (Updating DateTime)");
 +
    Rtc.SetDateTime(compiled);
 +
  }
 +
  else if (now > compiled)
 +
  {
 +
    Serial.println("RTC is newer than compile time. (this is expected)");
 +
  }
 +
  else if (now == compiled)
 +
  {
 +
    Serial.println("RTC is the same as compile time! (not expected but all is fine)");
 +
  }
 +
 +
  // never assume the Rtc was last configured by you, so
 +
  // just clear them to your needed state
 +
  Rtc.Enable32kHzPin(false);
 +
  Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
 +
}
 +
 +
void loop ()
 +
{
 +
  if (!Rtc.IsDateTimeValid())
 +
  {
 +
    // Common Cuases:
 +
    //    1) the battery on the device is low or even missing and the power line was disconnected
 +
    Serial.println("RTC lost confidence in the DateTime!");
 +
  }
 +
 +
  RtcDateTime now = Rtc.GetDateTime();
 +
  printDateTime(now);
 +
  Serial.println();
 +
 +
  //输出温度 -- 温度每64秒更新一次 / 精度±3°C
 +
  RtcTemperature temp = Rtc.GetTemperature();
 +
  Serial.print("Temp:");
 +
  Serial.print(temp.AsFloat());
 +
  Serial.println("C");
 +
 +
  delay(10000); // ten seconds
 +
}
 +
 +
#define countof(a) (sizeof(a) / sizeof(a[0]))
 +
 +
// 串口打印日期时间
 +
void printDateTime(const RtcDateTime& dt)
 +
{
 +
  char datestring[20];
 +
 +
  snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
 +
            dt.Month(), dt.Day(), dt.Year(),
 +
            dt.Hour(), dt.Minute(), dt.Second() );
 +
  Serial.print(datestring);
 +
}
 +
 +
</pre>
 +
程序下载地址:[http://pan.baidu.com/s/1eQ8SmzC passiveBuzzer_test]
 +
  
  

2016年12月14日 (三) 09:27的版本


DS3231是低成本、高精度I²C实时时钟(RTC),具有集成的温补晶振(TCXO)和晶体。该器件包含电池输入端,断开主电源时仍可保持精确计时。集成微机电系统(MEMS)提高了器件的长期精确度,并减少了生产线的元件数量。DS3231提供商用级和工业级温度范围,采用16引脚300mil的SO封装。

RTC保存秒、分、时、星期、日期、月和年信息。少于31天的月份,将自动调整月末的日期,包括闰年修正。时钟格式可以是24小时或带/AM/PM指示的12小时格式。提供两个可设置的日历闹钟和一个1Hz输出。地址与数据通过I²C双向总线串行传输。

精密的、经过温度补偿的电压基准和比较器电路用来监视VCC状态,检测电源故障,提供复位输出,并在必要时自动切换到备份电源。另外,/RST监测引脚可以作为产生微处理器复位的按键输入,详细信息请参考完整数据资料中的方框图。

除计时精度高之外,DS3231还具有一些其它功能,这些功能扩展了系统主机的附加功能和选择范围。该器件内部集成了一个非常精确的数字温度传感器,可通过I2C*接口对其进行访问(如同时间一样)。这个温度传感器的精度为±3°C。片上控制电路可实现自动电源检测,并管理主电源和备用电源(即低压电池)之间的电源切换。如果主电源掉电,该器件仍可继续提供精确的计时和温度,性能不受影响。当主电源重新加电或电压值返回到容许范围内时,片上复位功能可用来重新启动系统微处理器。

特性

  • 0°C 至 +40°C 范围内精度为±2ppm
  • -40°C 至 +85°C 范围内精度为±3.5ppm
  • 为连续计时提供电池备份输入
  • 工作温度范围
商用级:0°C 至 +70°C (DS3231S#)
工业级:-40°C 至 +85°C (DS3231SN#)
  • 低功耗
  • 实时时钟产生秒、分、时、星期、日期、月和年计时,并提供有效期到2100年的闰年补偿
  • 两个日历闹钟
  • 可编程方波输出
  • 高速(400kHz) I2C接口
  • 支持工作电压范围2.3 - 5.5V(推荐工作电压:3V-3.3V
  • 数字温度传感器输出:精度为±3°C
  • 老化修正寄存器
  • /RST输出/按钮复位去抖输入

Arduino 库

RTC DS1307 DS3231库

应用示例


电路连接


RTC3231时钟模块 Arduino UNO
VCC +5V
GND GND
SDA SDA/A4
SCL SCL/A5



示例代码

/* arduino sketch -- arduino print date and time with ds3231 RTC breakout
   SETUP:
    arduino (UNO R3)     ds3231
      A4/SDA          →    SDA
      A5/SCL          →    SCL
      GND             →    GND

   Read More about DS3231: http://www.yfrobot.com/wiki/index.php?title=RTC_3231

   BY YFROBOT
*/

#include <Wire.h>  // must be incuded here so that Arduino library object file references work
#include <RtcDS3231.h>

RtcDS3231 Rtc;

void setup ()
{
  Serial.begin(115200);

  Serial.print("compiled: ");
  Serial.print(__DATE__);      // 编译文档的日期
  Serial.println(__TIME__);    // 编译文档的时间

  //--------RTC SETUP ------------
  Rtc.Begin();

  // if you are using ESP-01 then uncomment the line below to reset the pins to
  // the available pins for SDA, SCL
  // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL

  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); // 存储当前编译的时间及日期并打印
  printDateTime(compiled);
  Serial.println();

  if (!Rtc.IsDateTimeValid())
  {
    // Common Cuases:
    //    1) first time you ran and the device wasn't running yet
    //    2) the battery on the device is low or even missing

    Serial.println("RTC lost confidence in the DateTime!");

    // following line sets the RTC to the date & time this sketch was compiled
    // it will also reset the valid flag internally unless the Rtc device is
    // having an issue

    Rtc.SetDateTime(compiled);
  }

  if (!Rtc.GetIsRunning())
  {
    Serial.println("RTC was not actively running, starting now");
    Rtc.SetIsRunning(true);
  }

  RtcDateTime now = Rtc.GetDateTime();  // 获取当前DS3231时间及日期
  if (now < compiled)
  {
    Serial.println("RTC is older than compile time!  (Updating DateTime)");
    Rtc.SetDateTime(compiled);
  }
  else if (now > compiled)
  {
    Serial.println("RTC is newer than compile time. (this is expected)");
  }
  else if (now == compiled)
  {
    Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  }

  // never assume the Rtc was last configured by you, so
  // just clear them to your needed state
  Rtc.Enable32kHzPin(false);
  Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}

void loop ()
{
  if (!Rtc.IsDateTimeValid())
  {
    // Common Cuases:
    //    1) the battery on the device is low or even missing and the power line was disconnected
    Serial.println("RTC lost confidence in the DateTime!");
  }

  RtcDateTime now = Rtc.GetDateTime();
  printDateTime(now);
  Serial.println();

  //输出温度 -- 温度每64秒更新一次 / 精度±3°C
  RtcTemperature temp = Rtc.GetTemperature();
  Serial.print("Temp:");
  Serial.print(temp.AsFloat());
  Serial.println("C");

  delay(10000); // ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

// 串口打印日期时间
void printDateTime(const RtcDateTime& dt)
{
  char datestring[20];

  snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
             dt.Month(), dt.Day(), dt.Year(),
             dt.Hour(), dt.Minute(), dt.Second() );
  Serial.print(datestring);
}

程序下载地址:passiveBuzzer_test


参考文档

[DS3231芯片手册]




返回首页

更多建议和问题欢迎反馈至 YFRobot论坛

购买方式:YFRobot 电子工作室