极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16872|回复: 2

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

[复制链接]
发表于 2015-9-22 19:54:45 | 显示全部楼层 |阅读模式
原贴  
一、enc28j60+NTP更新DS3231时钟 兼容DS1307  

http://www.geek-workshop.com/for ... &highlight=1307

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

二、http://www.geek-workshop.com/for ... ght=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的时间。
回复

使用道具 举报

发表于 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 编辑

谢谢关心,贴出程序很好用,通过串口设置很方便。想让初学者了解一下,但少了个设置“周”。你的程序非常棒,很简洁。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-19 20:55 , Processed in 0.033586 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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