DS3234 RTC issue with reading the control registers while interrupt is enabled

I have setup a DS3234 to produce a 1Hz SQW to GPIO10 on a NodeMCU ESP8266 12-E (same issue with Arduino Uno). The ISR is:

void timeInterruptFunction() {
  timeTriggers++;
  if (timeTriggers < 5) {
    return;
  }
  timeTriggers = 0;
  readSensors = true;
}

and the loop reads registers every 5s as per timeInterruptFunction()

void loop() {
  if (readSensors) {
    byte cr1 = cbDS3234->getControlRegister();
    byte b2 = cbDS3234->getControlRegister2();
    cbDS3234->readDateAndTime(&timeValues);
    displayTime();
    readSensors = false;
  }
}
byte CBDS3234::getControlRegister() {
  SPI.beginTransaction(spiSettings);
  digitalWrite(_chipSelectPin, LOW);
  SPI.transfer(READ_CONTROL_REGISTER);
  byte controlRegister = SPI.transfer(0x00);
  digitalWrite(_chipSelectPin, HIGH);
  SPI.endTransaction();
  delay(100);
  return controlRegister;
}

The issue is, with this way of reading the registers, the first time through the loop, cr1 and cr2 are zero. From then on, i.e. every 5secs cr1 is correct and cr2 is always zero.

Disabling the interrupt and cr1 and cr2 return correct values every time. By the time the ISR has finished I would have thought it wouldn't interfere for another second. Plenty time to read registers but it seems the interrupt means register reads are unstable. Reading the date and time is always correct even with the interrupt enabled.

I was wondering whether the SQW function of the DS3234 somehow 'disables' the control registers for a period. Simply going through loop() with a delay(1000); and reading the registers works fine. Control the loop via the ISR and the registers are unstable.

Please edit the title of the thread so that it makes sense

oops sorry about that. Title sorted.

I had another look at the datasheet and the SQW is:

Active-Low Interrupt or Square-Wave Output

so I changed the interrupt setup from RISING to:

  pinMode(_interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(_interruptPin), _sqwFunction, FALLING);

and the register reads are now fine when controlling the loop() from the ISR.

Why do you need to use interrupt to establish 5-second timing? You could just poll the signal from the RTC and look for rising edge. Note, you're already polling your 'readSensors' variable. So, using the interrupt is not making anything more "precise"

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.