DS1307 RTC programming - 1. stopping starting

I am programming the "Tiny RTC DS1307 " as sold on ebay.

It has two chips, one is the Maxim DS1307. I have some questions not clear from the manual.

  1. If you try to start/stop the clock you need to write to register 0 which is also the seconds register. But if you zap it then you lose your seconds count. I presume it is implied that when you do a stop, well you do not care what the seconds count is, and when you do a start, you should also send a new, valid seconds count.

But if you instead wanted to use it as a stopwatch, then you'd need to stop and start while maintaining the seconds count. I have therefore written my Stop() and Start() functions to first get the current value of seconds so that the subsequent write does not trash the seconds count. This works but it requires extra transfers on the I2C bus. Have I got it right or am I missing a clever trick? Here is my Stop and Start code:

void Tiny_RTC_DS1307_24C32::Stop()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  uint8_t bRet = Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  byte bReg0 = Wire.read();
  bReg0 |= 0x80;
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(bReg0);
  Wire.endTransmission();
}

void Tiny_RTC_DS1307_24C32::Start()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  uint8_t bRet = Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  byte bReg0 = Wire.read();
  bReg0 &= 0x7f;
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(bReg0);
  Wire.endTransmission();
}

What do you expect to happen when the seconds register rolls over to zero? Or are all your timings going to be less than a minute?

I am not sure I understand your question. We are trying to stop/start the RTC, like a stopwatch, and we must avoid corrupting the seconds counter during our manipulation of the stop bit. Otherwise what value should we set the seconds counter to, every time we stop() and start()? Some random value? 0 perhaps ?

Maybe save the seconds value, stop, start, set seconds to the saved value, assuming that you can do this, of course.

We are trying to stop/start the RTC, like a stopwatch,

Are you trying to use the RTC as an elapsed time timer? Or a lap timer? You are better off using the internal millis clock.

You can also simply determine elapsed time by reading the rtc at start and stop times and making the subtraction.

Bit 7 of Register 0 is the clock halt(CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. Enabling and disabling the oscillator will corrupt the real time timekeeping.