极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12324|回复: 3

DS3231时钟编译出错,请高手指教

[复制链接]
发表于 2018-1-1 10:56:45 | 显示全部楼层 |阅读模式


这怎么解决?




以下是代码:
#include <EtherCard.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>

#define SECONDS_IN_DAY          86400
#define START_YEAR              1900
#define TIME_ZONE               +8
static int days_in_month[] = {
  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

static byte mymac[] = {
  0x00,0x1A,0x4B,0x38,0x0C,0x5C};
byte Ethernet::buffer[700];

static byte ntpServer[] = {
  193,204,114,232};
static byte srcPort = 0;

uint32_t timeStamp;
boolean requestSent;

void setup () {
  Serial.begin(115200);
  Serial.println("Update RTC time thru NTP");

  printRTCTime();

  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
  else
    Serial.println("Ethernet controller initialized");

  if (!ether.dhcpSetup())
    Serial.println("Failed to get configuration from DHCP");
  else
    Serial.println("DHCP configuration done");

  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.mymask);
  ether.printIp("Gateway:\t", ether.gwip);

  Serial.println();
  Serial.println("Press n to send NTP request");
  requestSent = false;
}

void loop() {

  ether.packetLoop(ether.packetReceive());

  if(requestSent && ether.ntpProcessAnswer(&timeStamp, srcPort)) {
    Serial.println("NTP answer received");
    Serial.println();
    Serial.print("Timestamp: ");
    Serial.println(timeStamp);
    Serial.println();
    printDate(timeStamp + 3600 * TIME_ZONE);
    requestSent = false;
  }

  if(Serial.available() > 0) {
    int incomingByte = Serial.read();
    if(incomingByte == 'n') {
      ether.ntpRequest(ntpServer, srcPort);
      Serial.println("NTP request sent");
      requestSent = true;
    }
  }
}

void printDate(uint32_t timeStamp) {

  unsigned int year = START_YEAR;
  while(1) {
    uint32_t seconds;
    if(isLeapYear(year)) seconds = SECONDS_IN_DAY * 366;
    else seconds = SECONDS_IN_DAY * 365;
    if(timeStamp >= seconds) {
      timeStamp -= seconds;
      year++;
    }
    else break;
  }

  unsigned int month = 0;
  while(1) {   
    uint32_t seconds = SECONDS_IN_DAY * days_in_month[month];
    if(isLeapYear(year) && month == 1) seconds = SECONDS_IN_DAY * 29;
    if(timeStamp >= seconds) {
      timeStamp -= seconds;
      month++;
    }
    else break;
  }  
  month++;

  unsigned int day = 1;
  while(1) {
    if(timeStamp >= SECONDS_IN_DAY) {
      timeStamp -= SECONDS_IN_DAY;
      day++;
    }
    else break;
  }  

  unsigned int hour = timeStamp / 3600;
  unsigned int minute = (timeStamp - (uint32_t)hour * 3600) / 60;
  unsigned int second = (timeStamp - (uint32_t)hour * 3600) - minute * 60;

  tmElements_t tm;
  tm.Year = CalendarYrToTm(year);
  tm.Month = month;
  tm.Day = day;
  tm.Hour = hour;
  tm.Minute = minute;
  tm.Second = second;
  RTC.write(tm);

  Serial.println("Current date and time:");

  if(day < 10) Serial.print("0");
  Serial.print(day);
  Serial.print("/");

  if(month < 10) Serial.print("0");
  Serial.print(month);
  Serial.print("/");  

  Serial.println(year);

  if(hour < 10) Serial.print("0");
  Serial.print(hour);
  Serial.print(":");

  if(minute < 10) Serial.print("0");
  Serial.print(minute);
  Serial.print(":");

  if(second < 10) Serial.print("0");
  Serial.println(second);

  Serial.println();
  printRTCTime();
}

boolean isLeapYear(unsigned int year) {

  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

void printRTCTime()
{
  tmElements_t tm;
  Serial.println("Time from RTC:");
  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  }
  else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    }
    else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
  }
}
void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2018-3-15 20:09:57 | 显示全部楼层
只看不懂不说。
回复 支持 反对

使用道具 举报

发表于 2018-3-16 14:02:31 | 显示全部楼层
换一个办卡型号编译看看,再就是库文件有问题
回复 支持 反对

使用道具 举报

发表于 2018-3-17 22:33:54 来自手机 | 显示全部楼层
IDE 和 库 的版本不同,导致有些语法不再被支持。两个解决办法,研究c语言,或者多保留几个不同的IDE版本 。当然网页复制的代码有可能被添加些乱码。
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 14:36 , Processed in 0.040518 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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