DS1307 CH Bit

Dear Forum,

I have setup DS1307 and I am able to set time and read time using the library at GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library All seems well apart from the fact the seconds do not advance. After much hunting around it seems that I must set BIT 7 of register 0 to 0.

I am not sure if the library that I am using does this.

Could somebody possible help me out with some code so that I can set the CH Bit?

I have tried many I2C tutorials but I must be doing something wrong.

Thanks

Setting the time should clear CH. Do you know for a fact that the time is set and CH=1?

Jack,

That is what I was thinking.

I do not know for certain that the CH bit is set to 1, I am just going on what I have been reading and other people having the same problem. I have tried new crystals and still no luck.

Just an observation, when I put this in the freezer it starts to advance the time at about 0 degrees (I was testing the temperature part of my project) I did think this was crystal related but as I said I purchased new crystals and still the same result.

I have been trying to read the 0 register but had much luck.

Correction

  • I have been trying to read the 0 register but not had much luck.

Try this

#include <Wire.h>        //http://arduino.cc/en/Reference/Libraries
#include <Time.h>        //http://www.arduino.cc/playground/Code/Time
#define rtcAddr 0x68	//DS3231 RTC I2C address
#define RTC_BUF_SIZE 8
byte rtcData[RTC_BUF_SIZE];

void setup(void) {
    Serial.begin(57600);
    Wire.begin();
}

void loop(void) {
    Wire.beginTransmission(rtcAddr);
    Wire.send(0);
    Wire.endTransmission();
    Wire.requestFrom(rtcAddr, RTC_BUF_SIZE);
    for (int i=0; i<RTC_BUF_SIZE; i++) {
        rtcData[i] = Wire.receive();
    }
    dumpBuffer("RTC registers", rtcData, RTC_BUF_SIZE);
    delay(10000);
}

void dumpBuffer(char *tag, byte *buf, int nBytes) {

	int nRows, ix;
    byte bufByte;

	nRows = nBytes / 8;
	if (nBytes % 8 != 0) nRows++;
    Serial.println();
    Serial.print(tag);

    for (int r=0; r<nRows; r++) {
        Serial.println();
        Serial.print("0x");
        if (r*8 < 16) Serial.print("0");
        Serial.print(r*8, HEX);
        for (int b=0; b<8; b++) {
            ix = r * 8 + b;
            if (ix >= nBytes) break;
            Serial.print(" ");
            bufByte = buf[ix];
            if (bufByte < 16) Serial.print("0");
            Serial.print(bufByte, HEX);
        }
    }
    Serial.println();
}

void printI00(int val)
{
  //Print an integer in "00" format (with leading zero).
  //Input value assumed to be between 0 and 99.
    if (val < 10) Serial.print('0');
    Serial.print(val);
    return;
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10) Serial.print('0');
  Serial.print(digits);
}

void sPrintI00(int val)
{
  //Print an integer in "00" format (with leading zero).
  //Input value assumed to be between 0 and 99.
    if (val < 10) Serial.print('0');
    Serial.print(val);
    return;
}

void sPrintDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
    Serial.print(":");
    if(digits < 10) Serial.print('0');
    Serial.print(digits);
}

void printTime(time_t t) {		//print time to serial monitor
	sPrintI00(hour(t));
	sPrintDigits(minute(t));
	sPrintDigits(second(t));
	Serial.print(" ");
	Serial.print(dayShortStr(weekday(t)));
	Serial.print(" ");
	Serial.print(day(t));
	Serial.print(" ");
	Serial.print(monthShortStr(month(t)));
	Serial.print(" ");
	Serial.print(year(t));
	Serial.println();
}

void printVersionInfo() {
    Serial.println(__FILE__);
    Serial.print(__DATE__);
    Serial.print(' ');
    Serial.println(__TIME__);
}

jpablo:
Just an observation, when I put this in the freezer it starts to advance the time at about 0 degrees (I was testing the temperature part of my project) I did think this was crystal related but as I said I purchased new crystals and still the same result.

Well that certainly indicates some kind of issue. Sounds like the oscillator may not be running. How is it constructed, breadboard?

I finally got to read register 0x00 using this :

#include <Wire.h>
#include <LiquidCrystal.h>

int DS1307 = 0x68;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {

 Serial.begin(9600);
 Wire.begin();
 lcd.begin(16, 2);
 lcd.setCursor(2, 0);

}

void loop() {
  
  lcd.clear();
  
  Wire.beginTransmission(DS1307);
  Wire.send(0x00);
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 1);
  while(Wire.available() == 0);
  int val = Wire.receive();
  
  lcd.print(val);
  
  delay(2000);
  
  
}

but as you said there must be another issue as the CH bit seems to be set to 0 :frowning:

The clock was set to 50 secounds and the output from the above code from register 0x00 to the LCD is 80 (see pic) converting that to binary gives me 01010000 which is correct. (I think)

It such a simply chip but yet it defeats me. I have tyied soldering the crystal directly to chip. I have attached a few pics

jpablo:
The clock was set to 50 secounds and the output from the above code from register 0x00 to the LCD is 80 (see pic) converting that to binary gives me 01010000 which is correct. (I think)

It such a simply chip but yet it defeats me. I have tyied soldering the crystal directly to chip. I have attached a few pics

Yes, I think that is correct too. I'd still bet the oscillator isn't running. What board is that, I don't recognize it. I have half a dozen of Adafruit's DS1307 breakout boards, all work fine. I have a few loose DS1307s in 8-pin DIPs and crystals to go with, but haven't tried using them yet. Not sure how critical the layout is, the frequency being relatively low. There are some layout suggestions in the DS1307 datasheet, I might review those.

Jack,

Finally I got this to work, see pic. You were right the oscillator was not running correctly. It must have been picking up some noise from the board.

The board is my own that I had made up. It was my first attempt, I need to work on component placement more.

Do you think that it’s the regulator making the noise?

IMG_1030.JPG

Not sure. Switching regulator? It works up in the air like that? If so, I'd think it's fairly immune to noise. Not sure what to say, just use general good practice. Keep leads short, put a 100nF bypass near the RTC chip, etc. See the DS1307 data sheet, they recommend a ground plane under the crystal I think. I haven't breadboarded a DS1307, but if I can find time I might try and let you know if it works. Anyhoo glad you got it working.

Thanks for your help on this.