So I had a problem with DS3231 not counting time when on VBAT. Also, I had a problem with alarm not resetting after it is triggered. I saw many people on the net having these problems, so below is the solution.
DS3231 has two registers that take care of alarms control and status, and also of oscillator. After the alarm is triggered, you need to clear appropriate registers in order to set new alarm.
There is also a problem with oscillator not working when DS3231 is operating only on VBAT. There are also few bits in Status and Control registers that set behaviour of oscillator.
So in order for the alarms to work properly, you have to clear appropriate bits in
0x0F registers. In
0x0E that would be bits
0. It will turn off both alarms. Then in register
0x0F you need to clear bits
0. This operation will clear any pending alarms. After this, you can set and enable alarms as you please.
If you want RTC to count time properly when VBAT is off, reset bit
0x0E - it ensures oscillator action on VBAT. You also want to be sure, that oscillator isn't stopped - clear bit
7 in register
In my application, before I set any alarm, I simply clear both registers. Here is the code in Arduino for clearing registers, then setting alarm (using DS3231.h library; this library unfortunately does not have a function to clear alarms properly):
DS3231 RTC; Wire.beginTransmission(0x68); Wire.write(0xF); // Address the Status register Wire.write(0x00); // Zero the Status register Wire.endTransmission(); Wire.beginTransmission(0x68); Wire.write(0xE); // Address the Status register Wire.write(0x00); // Zero the Status register Wire.endTransmission(); RTC.setA2Time( ADay, AHour, AMin, 0x00, true, false, false); delay(100); RTC.turnOnAlarm( 2 );