我尝试在我的 Arduino 项目中使用 RTC DS1307,每次我运行我的代码时都会收到此错误:RTC is not running
. 该代码应每分钟点亮一个 LED:10sc 并每分钟关闭一次:20s。
这是我写的代码:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 rtc;
void setup ()
{
Serial.begin(57600);
pinMode(0, OUTPUT);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
}
if (! rtc.isrunning()){
Serial.println("RTC is not running");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop () {
DateTime now = rtc.now();
lcd.setCursor(0, 2);
Serial.println(now.month(),DEC);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
if (now.second() == 10 )
{
digitalWrite (0, HIGH);
Serial.println("high");
}
else if (now.second() == 20 )
{
digitalWrite (0, LOW);
Serial.println("low");
}
delay(1000);
}
我使用了来自 Arduino 站点的 RTClib.h 库。请有任何想法。