原贴
一、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的时间。 |