Tiny RTC 时钟模块用法求助
#include <WProgram.h>#include <Wire.h>
#include <DS1307.h>
int rtc;
byte rr;
int ledPin =13;
void setup()
{
DDRC|=_BV(2) |_BV(3);// POWER:Vcc Gnd
PORTC |=_BV(3);// VCC PINC3
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
RTC.get(rtc,true);
if(rtc<12){
RTC.stop();
RTC.set(DS1307_SEC,1);
RTC.set(DS1307_MIN,27);
RTC.set(DS1307_HR,01);
RTC.set(DS1307_DOW,7);
RTC.set(DS1307_DATE,12);
RTC.set(DS1307_MTH,2);
RTC.set(DS1307_YR,12);
RTC.start();
}
//RTC.SetOutput(LOW);
//RTC.SetOutput(HIGH);
//RTC.SetOutput(DS1307_SQW1HZ);
//RTC.SetOutput(DS1307_SQW4KHZ);
//RTC.SetOutput(DS1307_SQW8KHZ);
RTC.SetOutput(DS1307_SQW32KHZ);
}
void loop()
{
int i;
RTC.get(rtc,true);
for(i=0; i<7; i++)
{
Serial.print(rtc);
Serial.print(" ");
}
Serial.println();
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
if (Serial.available() > 6) {
for(i=0;i<7;i++){
rr=BCD2DEC(Serial.read());
}
Serial.println("SET TIME:");
RTC.stop();
RTC.set(DS1307_SEC,rr);
RTC.set(DS1307_MIN,rr);
RTC.set(DS1307_HR,rr);
RTC.set(DS1307_DOW,rr);
RTC.set(DS1307_DATE,rr);
RTC.set(DS1307_MTH,rr);
RTC.set(DS1307_YR,rr);
RTC.start();
}
}
char BCD2DEC(char var){
if (var>9){
var=(var>>4)*10+(var&0x0f);
}
return var;
}
上面是代码
不清楚怎么设定时间,请大侠们帮小生一把
为什么不用专用的类呢? RTClib这个类很不错啊。
这个是他的示范代码,设定时间为当前电脑的时间。
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
页:
[1]