Arduino篇—实时时钟

电子说

1.2w人已加入

描述

学习目标

  • DS1307时钟模块的原理与应用
  • I2C接口读写的工作过程
  • 学会用I2C编程控制时钟模块

相关知识

DS1307时钟模块: DS1307串行实时时钟(RTC)是低功耗,全二进制编码的十进制(BCD)时钟/日历以及56字节的NV SRAM。地址和数据通过I2C双向总线串行传输。时钟/日历提供秒,分钟,小时,日期,日期,月份和年份信息。

RTC模块的接线:

实时时钟

I2C总线协议: 集成电路总线,它是一种串行通信总线,使用多主从架构。I2C总线只有两根双向信号线。一根是数据线SDA,另一根是时钟线SCL。SCL:上升沿将数据输入到每个EEPROM器件中;下降沿驱动EEPROM器件输出数据。(边沿触发)

实时时钟

BCD码: 用4位二进制数来表示1位十进制数中的0~9这10个数码,是一种二进制的数字编码形式,用二进制符号来表示十进制数。BCD码有:8421码、5421码、2421码等多种类型。

8421码示例:

实时时钟

电路搭建

所需材料

ArduinoUNO * 1
DS1307 RTC模块 * 1
LCD1602液晶显示屏 * 1
电位器 * 1
杜邦线若干

电路连接

实时时钟

程序编写

练习一:时间设置

如图连接电路后,编写程序实现将RTC模块进行当前时间设置。

这里我们直接调用了DS1307RTC.h这个库文件里面的例子实现校准时间的功能。

#include < Wire.h >
#include < TimeLib.h >
#include < DS1307RTC.h >
const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
  bool parse=false;
  bool config=false;


  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }


  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time="");
    Serial.print(__TIME__);
    Serial.print("", Date="");
    Serial.print(__DATE__);
    Serial.println(""");
  }
}


void loop() {
}


bool getTime(const char *str)
{
  int Hour, Min, Sec;


  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}


bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;


  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

练习二: 可显示的时钟

在LCD1602上实时显示时间。

/*项目名称:实时时钟
 *项目时间:2022.03.14
 *项目作者:MRX
 */
#include < Wire.h >
#include < TimeLib.h >
#include < DS1307RTC.h >
#include < LiquidCrystal.h >
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
lcd.begin(16,2);
}


void loop() {
tmElements_t tm;
lcd.clear();
if (RTC.read(tm))
{
if(tm.Hour >=12)
{
lcd.setCursor(14,0);
lcd.print("PM");
}
if(tm.Hour< 12)
{
lcd.setCursor(14,0);
lcd.print("AM");
}
lcd.setCursor(0,0);
lcd.print("TIME:");
lcd.print(tm.Hour);
lcd.print(":");
lcd.print(tm.Minute);
lcd.print(":");
lcd.print(tm.Second);
lcd.setCursor(0,1);
lcd.print("DATE:");
lcd.print(tm.Day);
lcd.print("/");
lcd.print(tm.Month);
lcd.print("/");
lcd.print(tmYearToCalendar(tm.Year));
} else {
if (RTC.chipPresent()) {
lcd.setCursor(0,0);
lcd.print("RTC stopped!!!");
lcd.setCursor(0,1);
lcd.print("Run SetTime code");
} else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Read error!");
lcd.setCursor(0,1);
lcd.print("Check circuitry!");
}
delay(500);
}
delay(500);
}
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分