极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14339|回复: 0

【求助】ds1307装电池后不能用

[复制链接]
发表于 2016-3-13 14:58:44 | 显示全部楼层 |阅读模式
代码如下
ds1307模块不加电池时工作正常(输入D能显示正常走的时间)
加电池之后就不能返回时间,而且arduino像卡住一样,也不能返回任何数据(照理起码返回我输入的字符呀)
而一拔了电池数据又在一瞬间返回

#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) {

    char instruct = Serial.read();
  Serial.print(instruct);
    switch (instruct) {
    case 'D': {
      //获取当前日期和时间
      DateTime now = RTC.now();
      //通过串口传送当前的日期和时间      
      printDateTime(now);
      break;
    } case 'S':
      //设置成6月
      RTC.set(RTC_MONTH, 6);
      //设置成16点
      RTC.set(RTC_HOUR, 16);
      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();
}
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 00:58 , Processed in 0.057652 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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