by wild » Fri Jul 06, 2012 12:42 pm
The INT/SQW is updated once per second. p. 12 of the data sheet:
When the RTC register values match alarm register settings,
the corresponding Alarm Flag ‘A1F’ or ‘A2F’ bit is
set to logic 1. If the corresponding Alarm Interrupt
Enable ‘A1IE’ or ‘A2IE’ is also set to logic 1 and the
INTCN bit is set to logic 1, the alarm condition will activate
the INT/SQW signal. The match is tested on the
once-per-second update of the time and date registers.
I do not believe in this case it needs to be clear.
My sketch is:
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
// clear /EOSC bit
// Sometimes necessary to ensure that the clock
// keeps running on just battery power. Once set,
// it shouldn't need to be reset but it's a good
// idea to make sure.
// set initial time and date
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x01); // select register, change the minute register
Wire.write(0b01010101); // set to 55 min
Wire.write(0b00010001); // set to 11 hour
Wire.endTransmission();
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x07); // select alarm registers,
Wire.write(0b00000000); //Set mask bits to 1110, seconds register is set to zero
Wire.write(0b10000000);
Wire.write(0b10000000);
Wire.write(0b10000000);
Wire.endTransmission();
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x0E); // select the control register
Wire.write(0b00000111); // set bit 7 (EOSC) = 0, start counter.
// INTCN, A2IE, and A1IE all set to 1 (enable)
Wire.endTransmission();
}
void loop()
{
// write request to read data starting at register 0
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write(0); // start at register 0
Wire.endTransmission();
Wire.requestFrom(0x68, 3); // request three bytes (seconds, minutes, hours)
while(Wire.available())
{
int seconds = Wire.read(); // get seconds
int minutes = Wire.read(); // get minutes
int hours = Wire.read(); // get hours
seconds = (((seconds & 0b11110000)>>4)*10 + (seconds & 0b00001111)); // convert BCD to decimal
minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert BCD to decimal
hours = (((hours & 0b00110000)>>4)*10 + (hours & 0b00001111)); // convert BCD to decimal (assume 24 hour mode)
Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.println(seconds);
}
delay(1000);
}