DS1307 RTC and Mega64 problems..

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone.

I am trying to add a time clock to my board. I have a DS1307 time chip connected to it. To start debug, I want the 4KHz output to pulse. Well.... It is not happening. I am stumped. With my scope I can see the Mega64 is loading the DS1307 RTC chip. I can see the clock line is active high, clocking away on bootup and the SDA line is loading the same data. I have 2K pull-ups on the SCL and SDA pins. I also have a pull-up on pin 7 (the Fout pin of the DS1307). I can see the CLK and SDA pins loading the chip, but no output. I am using Codevision and I had the wizard produce the code to generate the output pin to toggle at 4KHz. Power and ground are on the chip, decopuled with a .1uF.

Something I noticed was that when I scope the crystal on pins 1&2 of the DS1307, I do not see a sine wave. Hmm... I see it on the Mega 64 crystal, but not on the crystal of the RTC chip. I buzzed out the foil tracks and both pins are connected. I even tried 4 different 32.768KHz crystals! Same results. I verified 5V on the chip and the SDA, SCL lines are pulsing from the Mega64.

Anyone with experience on this chip? I am beginning to think it is a software issue with Codevision, but not so sure. At this point, not sure where to point fingers. I just changed chips, same result. This may be difficult debug via this forum, but I'll keep trying new things.

Thanks guys...

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know for certain on the 1307, but on the 1302 there is a bit on one of the registers which must be set to turn the beastie on - without that bit, the oscillator doesn't oscle although the rest of the chip talks to you. The 1302 ships with that bit disabled.

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Divide and conquer! Are you sure the i2c comms is working? Can you read back the rtc registers and display them?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is the schematic of the connections to the Mega 64: You cannot see the pullup resistors, they are on a different sheet, but they are on the SCL and SDA lines. :) I verified that.

So I am beginning to think this may be software.

Neil.. about the enable bit, I used Codevision's wizard to set up the DS1307 with the output and chip enabled, so I am assuming the compiler's wizard set that all up. There are no bugs reported with the DS1307 wizard from what I can tell. I can see data on the SCL and SDA lines via scope.

[/img]

I am attaching some code:

    // I2C Bus initialization
    i2c_init();

    // DS1307 Real Time Clock initialization
    // Square wave output on pin SQW/OUT: On
    // Square wave frequency: 4096Hz
    rtc_init(1,1,0);

And the code in the library file:

void rtc_init(unsigned char rs,unsigned char sqwe,unsigned char out)
{
   rs&=3;
   if (sqwe) rs|=0x10;
   if (out) rs|=0x80;
   i2c_start();
   i2c_write(0xd0);
   i2c_write(7);
   i2c_write(rs);
   i2c_stop();
}
void rtc_write(unsigned char address,unsigned char data)
{
   i2c_start();
   i2c_write(0xd0);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
}

Anything else I can post to try and get some answers on what is going on here?

I keep saying to myself, its hardware, no its software, no its hardware, etc... :)

Thanks for any help. Much appreciated.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TMBartman wrote:
I have 2K pull-ups on the SCL and SDA pins.
Tom, 2K seems kind of low - I've successfully used the DS1307 with an ATMega32 but use 10K pull-ups on SCL and SDA.

TMBartman wrote:

void rtc_init(unsigned char rs,unsigned char sqwe,unsigned char out)
{
   rs&=3;
   if (sqwe) rs|=0x10;
   if (out) rs|=0x80;
   i2c_start();
   i2c_write(0xd0);
   i2c_write(7);
   i2c_write(rs);
   i2c_stop();
}

For a 4.096 KHz output, you want:
rs &= 3;
rs |= 1;

Also, you want to set the CH bit to zero in register 0. From the DS1307 data sheet:

Quote:
Please note that the initial power-on state of all registers is not defined. Therefore, it is important to enable the oscillator (CH bit = 0) during initial configuration.

Don

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don,

What code did you use to set the CH bit to zero at address 0?

I tried this:

 //force CH bit to zero
   i2c_start(); 
   i2c_write(0x00); //write address 0
   i2c_write(0x80); //write bit 7
   i2c_stop(); 

I tried this with no luck, but perhaps my code is wrong on this. I think it just may be.

Thanks much!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CORRECTION from my last post. Don... It looks like I AM indeed setting the CH bit with my function:

    // I2C Bus initialization
    i2c_init();

    // DS1307 Real Time Clock initialization
    // Square wave output on pin SQW/OUT: On
    // Square wave frequency: 4096Hz
    rtc_init(1,1,0); //Go config the RTC chip

So I pass 1,1,0 to my RTC init function:

void rtc_init(unsigned char rs,unsigned char sqwe,unsigned char out) 
{ 
   rs&=3; 
   if (sqwe) rs|=0x10; 
   if (out) rs|=0x80; 
   i2c_start(); 
   i2c_write(0xd0); 
   i2c_write(7); 
   i2c_write(rs); 
   i2c_stop(); 
} 

That sets the CH bit in line:

if (out) rs|=0x80; 

Starting to lean towards hardware now?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Starting to lean towards reading-the-datasheet trouble, actually :? CH isn't bit 7 of register 7, it's bit 7 of register 0 (seconds), and it has to be cleared to enable the oscillator. Setting it to 1 will stop the clock. All reg7.7 does is set the idle state of the pulse output pin while pulses are disabled.

It's hard to see any crystal activity on the DS1307, as it's very low level.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know what your problem is, but I've used the codevision generated code for a DS1307 before with no problems.

Randy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We use the DS1307 in a product, and have noticed a lot of problems getting the thing to initially start running.

Not sure if we are missing something on initialization, but on some they won't start unless the battery is connected, others won't start unless the battery supply is shorted to ground.

Once started they are fine, and have been running for years.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

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.
Note that the initial power-on state of all registers is not defined. Therefore, it is important to
enable the oscillator (CH bit = 0) during initial configuration.

this is copied straight from datasheet of DS 1307


 { 
   i2c_start();//send start cond
   i2c_write(0xd0);//send slave addr+w
   i2c_write(0x00);//set the reg ptr 2 sec
   i2c_write(0x00);//write 0 to ch bit                                   i2c_start();//send repeated start
      i2c_write(0xd0);//send slave add+w
      i2c_write(7);//
      i2c_write(0x91);
      i2c_stop();
   i2c_stop();

}

try this out im sure this would work...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Desperadogear,

THAT DID IT!!!!

I owe you a beer. Now I need to go back and see what I did wrong. Looks like I was not setting the CH bit. Thank you ALL for the help. Another forum success story.

Thanks again!

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TMBartman wrote:
Desperadogear,

THAT DID IT!!!!

I owe you a beer. Now I need to go back and see what I did wrong. Looks like I was not setting the CH bit. Thank you ALL for the help. Another forum success story.

Thanks again!

Tom

Tom,

Glad to hear you have now got this working.

After the comments on here, I have re-checked our initialisation code, and am 100% sure it is correct.

We have never had a return, so put the start up problems down to an idiosyncrasy with these chips.

Wilko

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Will do. Start-up has been an issue with these chips from what I have read. However, people that have used it, once the design is done, it works 100% of the time. So it is a typical new design / debug. I did need strong 2K pullups to get the clock looking nice. Not sure if that had anything to do with it, but hey..the thing is working on 3 boards now so I'll stick with it.

Thanks again!

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

welcome Tom