I'm making an alarm clock with the DS3231 precision time module. Setting the time works fine, but I'd like to trigger an alarm for a certain combination of hours, minutes and seconds (so just for a given time , the day doesn't matter).
I've been trying for a loooong time to get this to work, but it still doesn't work and does weird stuff sometimes.
I wrote a code to show you what I'm doing:
#include <Wire.h>
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setAlarm1(byte second, byte minute, byte hour)
{
// sets time and date data to DS3231
Wire.beginTransmission(0x68);
Wire.write(0x07); //alarm second location
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.endTransmission();
}
void setup()
{
Serial.begin(9600);
Wire.beginTransmission(0x68);
Wire.write(0x0F);
Wire.write(0b00000000); //set flag from alarm1 to 0
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x0E);
Wire.write(0b00000101); //set INTCN to 1, set alarm interrupt enabled
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x0A);
Wire.write(0b10000000); //set A1M4 to 1 so that the alarm reacts on hours, minutes and seconds
Wire.endTransmission();
setAlarm1(0,41,21);
attachInterrupt(0,functie,RISING);
}
void loop()
{
Wire.beginTransmission(0x68);
Wire.write(0x0F);
Wire.endTransmission();
Wire.requestFrom(0x68,1);
byte data = Wire.read();
delay(15);
Serial.println(data,BIN);
delay(1000);
}
void functie()
{
Serial.println("alarm");
}
Here is the datasheet: http://datasheets.maximintegrated.com/en/ds/DS3231.pdf
The output:
A lot of interrupts every cyclus and I read 11111111 from the register every cyclus...
Can any of you guys figure out what I'm doing wrong?