I2C question - unable to wrighte registers

Good evening everyone!

I’m trying to set and arm the alarm on DS3231 RTC.
The following code is mostly based on the example by John Boxall from http://tronixstuff.com

Following Johns example I’ve attempted the following:

  • set seconds in register 7 (“Alarm 1 Seconds”) to 20 and leave bit 7 at 0
  • set bits 7 in registers 8, 9 and 10 to 1 (as per Table 2 on page 12)
  • set bit 0 in register 14 to 1 to arm Alarm 1 interrupt (as per page 13)
Alarm 1 Seconds: 0001 0100 <----Register 07h should be 0010 0000
Alarm 1 Minutes: 0101 0000 <----Register 08h should be 1000 0000
Alarm 1 Hours: 0101 0000 <----Register 09h should be 1000 0000
Alarm 1 Day/Date : 0101 0000 <----Register 0Ah should be 1000 0000
Alarm 2 Minutes : 0
Alarm 2 Hours : 0
Alarm 2 Day/Date : 0
Control : 0001 0111 <----Register 0Eh should be 0001 1101
Control/Status : 0101 1001
Aging Offset : 0
MSB of Temp : 0001 0011
LSB of Temp: 0111 1000 <----Bits 5 to 0 of register 12h should be 0

Am I missing something fundamental? Assuming BCD data format bears no relevance here…

Code below:

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68


void setup() {

  pinMode(13, OUTPUT);

  Wire.begin();
  Serial.begin(9600);

  // LED on pin 13 lights up when this interrupt is called
  // on DUE interrupt can be attached to any pin, here to pin 2
  attachInterrupt(2, ON, FALLING );	


  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(7); // set next input to start at the selected register
  Wire.write(B00100000); // write to register 7 - set seconds to 20 and A1M1 bit to 0
  Wire.write(B10000000); // write to register 8 - set A1M2 bit to 1
  Wire.write(B10000000); // write to register 9 - set A1M3 bit to 1
  Wire.write(B10000000); // write to register 10 - set A1M4 bit to 1
  Wire.endTransmission();

  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(14); // set next input to start at the selected register
  Wire.write(B00011100); // Set register to default walues and bit A1IE to 1
  Wire.endTransmission();

  ReadRegisters();
  
  delay (10000);
}


void loop() {

  digitalWrite(13, LOW); // Turn off LED set high by interrupt

  displayTime(); // display the real-time clock data on the Serial Monitor,

  delay(5000);   // every 5 seconds
}


void ON() {
  digitalWrite(13, HIGH);
}

void RegWrite(byte reg, byte val) {
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(reg); // set next input to start at the selected register
  Wire.write(val); // write to register
  Wire.endTransmission();
}




// Convert normal decimal numbers to binary coded decimal
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) );
}

//Set time - only use once
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second));      // set seconds
  Wire.write(decToBcd(minute));      // set minutes
  Wire.write(decToBcd(hour));        // set hours
  Wire.write(decToBcd(dayOfWeek));   // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth));  // set date (1 to 31)
  Wire.write(decToBcd(month));       // set month
  Wire.write(decToBcd(year));        // set year (0 to 99)
  Wire.endTransmission();
}

void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year) {

  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0);   // set DS3231 register pointer to 00h
  Wire.endTransmission();

  // request seven bytes of data from DS3231 starting from register 00h
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}


void displayTime() {

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");

  if (minute < 10)  {
    Serial.print("0");
  }

  Serial.print(minute, DEC);
  Serial.print(":");

  if (second < 10)  {
    Serial.print("0");
  }

  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");

  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}

void ReadRegisters() {

  byte *y7, *y8, *y9, *ya, *yb, *yc, *yd, *ye, *yf, *y10, *y11, *y12;

  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(7);
  Wire.endTransmission();

  Wire.requestFrom(DS3231_I2C_ADDRESS, 12);
  *y7 = bcdToDec(Wire.read());
  *y8 = bcdToDec(Wire.read());
  *y9 = bcdToDec(Wire.read());
  *ya = bcdToDec(Wire.read());
  *yb = bcdToDec(Wire.read());
  *yc = bcdToDec(Wire.read());
  *yd = bcdToDec(Wire.read());
  *ye = bcdToDec(Wire.read());
  *yf = bcdToDec(Wire.read());
  *y10 = bcdToDec(Wire.read());
  *y11 = bcdToDec(Wire.read());
  *y12 = bcdToDec(Wire.read());


  Serial.print("Alarm 1 Seconds: ");
  Serial.println(*y7, BIN);
  Serial.print("Alarm 1 Minutes: ");
  Serial.println(*y8, BIN);
  Serial.print("Alarm 1 Hours: ");
  Serial.println(*y9, BIN);
  Serial.print("Alarm 1 Day/Date : ");
  Serial.println(*ya, BIN);
  Serial.print("Alarm 2 Minutes : ");
  Serial.println(*yb, BIN);
  Serial.print("Alarm 2 Hours : ");
  Serial.println(*yc, BIN);
  Serial.print("Alarm 2 Day/Date : ");
  Serial.println(*yd, BIN);
  Serial.print("Control : ");
  Serial.println(*ye, BIN);
  Serial.print("Control/Status : ");
  Serial.println(*yf, BIN);
  Serial.print("Aging Offset : ");
  Serial.println(*y10, BIN);
  Serial.print("MSB of Temp : ");
  Serial.println(*y11, BIN);
  Serial.print("LSB of Temp: ");
  Serial.println(*y12, BIN);
}