极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9976|回复: 1

Tiny RTC 时钟模块用法求助

[复制链接]
发表于 2013-12-13 21:37:03 | 显示全部楼层 |阅读模式
  1. #include <WProgram.h>
  2. #include <Wire.h>
  3. #include <DS1307.h>

  4. int rtc[7];
  5. byte rr[7];
  6. int ledPin =  13;
  7. void setup()
  8. {
  9.   DDRC|=_BV(2) |_BV(3);  // POWER:Vcc Gnd
  10.   PORTC |=_BV(3);  // VCC PINC3
  11.   pinMode(ledPin, OUTPUT);  
  12.   Serial.begin(9600);
  13.   RTC.get(rtc,true);
  14.   if(rtc[6]<12){
  15.     RTC.stop();
  16.     RTC.set(DS1307_SEC,1);
  17.     RTC.set(DS1307_MIN,27);
  18.     RTC.set(DS1307_HR,01);
  19.     RTC.set(DS1307_DOW,7);
  20.     RTC.set(DS1307_DATE,12);
  21.     RTC.set(DS1307_MTH,2);
  22.     RTC.set(DS1307_YR,12);
  23.     RTC.start();
  24.   }
  25.   //RTC.SetOutput(LOW);
  26.   //RTC.SetOutput(HIGH);
  27.   //RTC.SetOutput(DS1307_SQW1HZ);
  28.   //RTC.SetOutput(DS1307_SQW4KHZ);
  29.   //RTC.SetOutput(DS1307_SQW8KHZ);
  30.   RTC.SetOutput(DS1307_SQW32KHZ);
  31. }

  32. void loop()
  33. {
  34.   int i;
  35.   RTC.get(rtc,true);

  36.   for(i=0; i<7; i++)
  37.   {
  38.     Serial.print(rtc[i]);
  39.     Serial.print(" ");
  40.   }
  41.   Serial.println();
  42.         digitalWrite(ledPin, HIGH);
  43.         delay(500);
  44.         digitalWrite(ledPin, LOW);
  45.         delay(500);
  46. if (Serial.available() > 6) {
  47.      for(i=0;i<7;i++){
  48.        rr[i]=BCD2DEC(Serial.read());
  49.      }
  50.      Serial.println("SET TIME:");
  51.        RTC.stop();
  52.     RTC.set(DS1307_SEC,rr[6]);
  53.     RTC.set(DS1307_MIN,rr[5]);
  54.     RTC.set(DS1307_HR,rr[4]);
  55.     RTC.set(DS1307_DOW,rr[3]);
  56.     RTC.set(DS1307_DATE,rr[2]);
  57.     RTC.set(DS1307_MTH,rr[1]);
  58.     RTC.set(DS1307_YR,rr[0]);
  59.     RTC.start();
  60. }
  61. }
  62. char BCD2DEC(char var){
  63.   if (var>9){
  64.      var=(var>>4)*10+(var&0x0f);
  65.   }
  66.   return var;
  67. }
复制代码

上面是代码
不清楚怎么设定时间,请大侠们帮小生一把

回复

使用道具 举报

发表于 2013-12-13 21:56:35 | 显示全部楼层
为什么不用专用的类呢?    RTClib  这个类很不错啊。

这个是他的示范代码,设定时间为当前电脑的时间。

[pre lang="arduino" line="1" file="RTCexample"]// 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);
}
[/code]
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 18:09 , Processed in 0.033923 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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