风筝
发表于: 2018-11-20 18:56:43 | 显示全部楼层

在本篇文章中,我们将学习如何基于Arduino开发板使用DS3231实时时钟模块。这里第一个问题是,当Arduino本身具有内置计时器时,为什么我们实际上需要为Arduino项目提供单独的RTC。好吧,原因就是RTC模块使用电池运行,即使我们重新编程微控制器或断开主电源,也可以保持时间运行。

DS3231-Real-Time-Clock.jpg


DS3231实时时钟

DS3231是一款低成本、高精度的实时时钟,可以保持小时、分钟和秒,以及日、月和年等信息。此外,它还可以自动补偿闰年和少于31天的月份。

DS3231-Real-Time-Clock-Specifications.jpg


该模块可以在3.3或5 V下工作,这使其适用于许多开发平台或微控制器。电池输入为3V,常见的3V纽扣电池CR2032可为模块供电,并且保持超过一年的信息。


该模块使用I2C通信协议,这使得与Arduino板的连接变得非常容易。


以下是电路原理图:

Arduino-and-DS3231-Real-Time-Clock-Circuit-Schematics.jpg


所以我们需要的是4根线,用于为模块供电的VCC和GND引脚,以及两个I2C通信引脚,SDA和SCL。

本篇文章所需的组件如下所示:

●    DS3231实时时钟

●    Arduino开发板

●    面包板和跳线


程序设计

一旦我们连接模块,我们需要对Arduino开发板进行编程以使用实时时钟。但是,在编写Arduino和I2C模块之间的通信时,代码并不是那么小而且容易。幸运的是,DS3231 RTC已经有几个库,可以在互联网上找到这些库。


在本篇文章中,我选择使用Henning Karlsen制作的库文件,可以在他的网站www.rinkydinkelectronics.com上找到并下载。


因此,一旦我们下载并安装了库,我们就可以使用它的第一个演示示例来初始激活RTC模块的时钟。在演示示例代码的setup函数部分,我们可以注意到有三行代码需要取消注释才能初始设置星期几、时间和数据。

  1. // Code from the Demo Example of the DS3231 Library
  2. void setup()
  3. {
  4.   // Setup Serial connection
  5.   Serial.begin(115200);
  6.   // Uncomment the next line if you are using an Arduino Leonardo
  7.   //while (!Serial) {}
  8.   
  9.   // Initialize the rtc object
  10.   rtc.begin();
  11.   
  12.   // The following lines can be uncommented to set the date and time
  13.   //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  14.   //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  15.   //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
  16. }
复制代码

第一行用于设置星期几,第二行用于设置以小时、分钟和秒为单位的时间,第三行用于设置以日、月和年为单位的日期。


上传该代码后,我们需要重新注释掉这三行代码并重新上传代码。

  1. // Code from the Demo Example of the DS3231 Library
  2. void loop()
  3. {
  4.   // Send Day-of-Week
  5.   Serial.print(rtc.getDOWStr());
  6.   Serial.print(" ");
  7.   
  8.   // Send date
  9.   Serial.print(rtc.getDateStr());
  10.   Serial.print(" -- ");
  11.   // Send time
  12.   Serial.println(rtc.getTimeStr());
  13.   
  14.   // Wait one second before repeating
  15.   delay (1000);
  16. }
复制代码

如果我们看一下代码的loop函数部分,我们可以看到现在使用三个自定义函数,我们从RTC获取信息并在串口监视器中打印它们。 以下是它们在串口监视器中的显示方式。

serial-monitor.png


现在,即使我们断开Arduino电源,然后重新连接并再次运行串口监视器,我们也可以注意到时间不会复位。


所以现在我们的实时时钟启动并运行,可以在任何Arduino项目中使用。 在第二个例子中,我们将LCD显示屏连接到Arduino,并在显示屏上打印显示时间和日期。

Arduino-DS3231-Real-Time-Clock-and-LCD-Example.jpg

以下是这个例子的源代码:

  1. /*
  2. * Arduino DS3231 Real Time Clock Module Tutorial
  3. *
  4. * Crated by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. * DS3231 Library made by Henning Karlsen which can be found and downloaded from his website, www.rinkydinkelectronics.com.
  8. *
  9. */
  10. #include <DS3231.h>
  11. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
  12. DS3231  rtc(SDA, SCL);
  13. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
  14. void setup() {
  15. rtc.begin(); // Initialize the rtc object
  16. lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
  17. }
  18. void loop() {
  19. lcd.setCursor(0,0);
  20. lcd.print("Time:  ");
  21. lcd.print(rtc.getTimeStr());

  22. lcd.setCursor(0,1);
  23. lcd.print("Date: ");
  24. lcd.print(rtc.getDateStr());

  25. delay(1000);
  26. }
复制代码

以上就是本篇文章的全部内容,如果遇到任何问题,可以在本帖下面进行回复。

跳转到指定楼层
12345AsDf
发表于: 2021-5-27 17:05:45 | 显示全部楼层

想问一下详细的接线图例二的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-19 09:00 , Processed in 0.141756 second(s), 11 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表