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.