水镜 发表于 2015-9-22 19:54:45

关于ds3231和DS1307和怎么设定时间

原贴
一、enc28j60+NTP更新DS3231时钟 兼容DS1307

http://www.geek-workshop.com/forum.php?mod=viewthread&tid=7260&highlight=1307

(DS3231,实际上可以和1307的代码通用,程序一样,互相代替。IIC)

二、http://www.geek-workshop.com/forum.php?mod=viewthread&tid=9355&extra=&highlight=1307&page=1

对isilcala 和 hi55234 提供的程序,表示感谢。

设置与显示程序如下:通过ide窗口设置与观察。

--------------------------------------------------------------------------

#include <Wire.h>
#include <RTClib.h>

void printDateTime(DateTime dateTime);

//创建实例
RTC_DS1307 RTC;

void setup (void){
Serial.begin(9600);
//初始化总线
Wire.begin();
//初始化实时时钟
RTC.begin();   

}

void loop() {
if (Serial.available() > 0) {

    int instruct = Serial.read();

    switch (instruct) {
    case 'D': {
      //获取当前日期和时间
      DateTime now = RTC.now();
      //通过串口传送当前的日期和时间      
      printDateTime(now);
      break;
    } case 'S':
   RTC.set(RTC_YEAR, 14);
      RTC.set(RTC_MONTH, 3);
   RTC.set(RTC_DAY, 5);

      RTC.set(RTC_HOUR, 22);
             RTC.set(RTC_MINUTE, 22);
                  RTC.set(RTC_SECOND, 0);
      break;
    }
}
}

void printDateTime(DateTime dateTime) {
    //传送年份
    Serial.print(dateTime.year(), DEC);
    Serial.print('/');
    //传送月份
    Serial.print(dateTime.month(), DEC);
    Serial.print('/');
    //传送月份中的第几天
    Serial.print(dateTime.day(), DEC);
    Serial.print(' ');
    //传送小时
    Serial.print(dateTime.hour(), DEC);
    Serial.print(':');
    //传送分钟
    Serial.print(dateTime.minute(), DEC);
    Serial.print(':');
    //传送秒
    Serial.print(dateTime.second(), DEC);
    Serial.println();
}

----------------------------------------------------------------------------------------------

一、先写入一个比当前时间快30秒的时间,编译写入arduino 主板。
二、然后进入ide串口窗口,当时间接近2秒左右时按下大写的S,就会把时间设置完成。 s只能输入一次。
三、串口窗口输入字母D显示当前3231&1307的时间。

ntgeralt 发表于 2015-9-23 19:00:13

你是问你在串口输入了时间没反应吗

不知道你时钟模块支不支持这个代码
rtc.setDOW(1);             //设定星期开头
rtc.setTime(00, 53, 00);   //时分秒
rtc.setDate(20, 9, 2015);//年月日

水镜 发表于 2015-9-23 22:03:21

本帖最后由 水镜 于 2015-9-23 22:04 编辑

谢谢关心,贴出程序很好用,通过串口设置很方便。想让初学者了解一下,但少了个设置“周”。你的程序非常棒,很简洁。
页: [1]
查看完整版本: 关于ds3231和DS1307和怎么设定时间