极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11971|回复: 4

DS1307 问题

[复制链接]
发表于 2017-8-7 16:24:25 | 显示全部楼层 |阅读模式
#include <Wire.h>
#include <Time.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;
}
请问
tmElements_t tm;的问题怎么处理,我是按照例程实现的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2017-8-9 18:12:35 | 显示全部楼层
定义一个结构体就行了啊,你看看你用到到结构体元素,照着写一个结构体即可。
回复 支持 1 反对 0

使用道具 举报

发表于 2017-8-8 17:17:52 | 显示全部楼层
缺少库文件
回复 支持 反对

使用道具 举报

发表于 2017-8-9 18:18:34 | 显示全部楼层
本帖最后由 Damn_intuition 于 2017-8-9 18:26 编辑

还有就是,tmElements_t的定义在time.h(不支持1.6),你可以尝试更换一下IDE版本。
这个应该可以帮助你。http://playground.arduino.cc/code/time
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-12 15:41:15 | 显示全部楼层
Damn_intuition 发表于 2017-8-9 18:18
还有就是,tmElements_t的定义在time.h(不支持1.6),你可以尝试更换一下IDE版本。
这个应该可以帮助你。 ...

         谢谢
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 18:45 , Processed in 0.049092 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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