如何使RTC DS3231设置适当的时间?

时间:2019-06-12 18:39:24

标签: arduino real-time-clock

我们正在尝试运行一个代码,该代码要求RTC设置RTC中使用的实时警报。

我们尝试从arduino网站和其他编码器添加代码,但似乎没有任何效果或修复错误。该代码以某种方式破坏了,我们不知道在哪里修复。在其他时候,我们尝试过,代码运行完美,现在突然不起作用了。电池和RTC的电源连接都通电并且有电压通向它们。代码刚刚中断,我不知道为什么。请帮忙!

// RTC setup
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(__DATE__, __TIME__));                                                                   
// sets date/time of RTC to time of compilation
  rtc.armAlarm(1, false);
  rtc.clearAlarm(1);
  rtc.alarmInterrupt(1, false);
  rtc.armAlarm(2, false);
  rtc.clearAlarm(2);
  rtc.alarmInterrupt(2, false);
  rtc.writeSqwPinMode(DS3231_OFF);

sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  temp = sensors.getTempCByIndex(0);
}

//note pull all other pins low to reduce consumption!

// ------------------- MAIN -------------------
void loop() {
  if (bt_conn && !collecting) {
    while (!getCommand());                                                                                    
// wait for a command to be processed
  }
  if (collecting) {
    // This is run to put the device to sleep
    DateTime curtime = rtc.now();                                                                             
// get current time
    Serial.println (curtime.minute());
    rtc.setAlarm(ALM1_MATCH_HOURS, (curtime.minute() + 1) % 1, 
(curtime.hour()) % 24, 0);                    // set wake up alarm 
changed from 60 to 1 for trial runs**
    rtc.alarmInterrupt(1, true);
    attachInterrupt(digitalPinToInterrupt(RTCINT), rtcInt, LOW);                                              
// enable interrupt for data collection
    Serial.println("Alarm set for: " + String(curtime.hour()) + ":" + 
String((curtime.minute() + 1) % 60));
    bt.println("Alarm set for: " + String(curtime.hour()) + ":" + 
String((curtime.minute() + 1) % 60));
    waitForSerial();
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);                                                      
// sleep until alarm wakes up the device
    get_data = true;
    detachInterrupt(digitalPinToInterrupt(RTCINT));                                                           
// remove alarm interrupt to prevent multiple calls
  }
  if (get_data) {
    // when the device wakes up, get_data is True, enabling a one time 
sample. This configuration prevents a bluetooth interrupt from triggering 
a sample event
    Serial.println("getting datapoint");
    bt.println("bt: getting datapoint");
    writeLine();
    get_data = false;
  }
}

理论上,此代码应运行时间并为一天中的正确日期和时间设置警报。

0 个答案:

没有答案