DS1307 Woes

GDSever

Member
I just recently started playing around with my DS1307 clock IC and have been completely foiled trying to read from the I2C bus. I've posted all the details and some screenshots on my blog at:

http://brewznet.wordpress.com/2008/06/04/ds1307-woes/

The problem has got to be right infront of me and I'm just not seeing it, but after 3 hours I am really frustrated. Anyone out there willing to take a quick glance to see if its something obvious (to everyone but me)?

Thanks a ton in advance...
 
Last edited:

hippy

Ex-Staff (retired)
Circuit and wiring looks okay to me. I believe the 32kHz crystal is only used for the clock and won't affect I2C operation so I'd assume it's not that causing the problem.

Reading the DS1307 datasheet ...

"3 VBAT - Backup Supply Input for Any Standard 3V Lithium Cell or Other Energy Source. Battery voltage must be held between the minimum and maximum limits for proper operation ... If a backup supply is not required, VBAT must be grounded".

That could be your problem; no connection between Leg 3 and 0V.
 

GDSever

Member
Thanks Hippy - grounding leg 3 helped clear up the 255:255:255: problem - now its displaying nothing but 0:0:0 - So I guess the IC bus is OK, but the clock isn't actually counting up.

Ideas? Seems like the oscillator / crystal between legs 1 and 2 isn't doing its job?
 

tarzan

Senior Member
DS1307

Looking at your breadboard circuit I don’t see a backup battery (DS1307) or capacitors for each chip. The crystal my not be making contact in the breadboard, I suggest soldering it too a two pin header. You should also be using in your code an interrupt in conjunction with the SQW output of the DS1307.
 
Last edited:

lbenson

Senior Member
Your posting didn't show initialization--if you didn't do it, something like:

i2cslave %11010000, i2cslow, i2cbyte
writei2c 0, ($00, $49, $21, $03, $04, $06, $08, $10)

Also, as Tarzan suggests, 100nf cap next to pins 19 & 20 (tho probably not your problem). I found when starting right in on I2C at the top of a program, it was sometimes necessary to put a short pause before issuing the i2c commands (tho that may have been a different issue).
 

westaust55

Moderator
When the RTC is not responding, the i2c bus is held high by the pull up resistors so the values read back are all 255 ($FF).

I recall reading that until the DS1307 RTC is initialised it just “sits” there and the values read out are all 0 ($0). So from what I have gleaned seems the i2c bus is working but the DS1307 is doing nothing.

As IBenson has suggested, you need to initialise the clock and set it counting/timing. Then you will/may have more success.

Note also that in your program, while you are reading data into variables b0, b1 and b2, you are not displaying b0, instead you are displaying b2 twice.
 

GDSever

Member
Actually, I want it to count up from a value of 0, so initiation is not an issue - I am actually using it to measure elapsed time, not the absolute date/time.

Not grounding leg 3 was definitely part of the problem, and now I've found that my output was actually not outputting seconds at all (I have #b2 specified twice in the sertxd command - D'OH!) The timer is counting up, although I need to do a little more reading to understand why it jumps from 9 to 16, etc. I know its because of the byte encoding, I just have to figure out how to make sense of that in my code for counting elapsed seconds / minutes.

Thanks to everyone for your help. It is much appreciated.
 

GDSever

Member
Heh. Looks like someone else found my B2/B0 error while I was typing the reply... Thanks for looking. Any insight you have to decoding the bytes into actual second values would be appreciated - things appear to get funky at intervals of 10 due to the encoding...
 

westaust55

Moderator
The DS1307 values are BCD encoded. Think in terms of the 8-bit byte as 2 4-bit numbers side by side.
So when the value gets to $09 = %0000 1001 (normally no space – added for demo purposes)
If then increments up to $10 = %0001 0000
Only the X1 parts have the BCDTOBIN command so you will need to do some number crushing to get back to a value that can be used.
Have a search for other posts with examples for conversion to and from BCD format

EDIT:
try this to get decimal value into b5 from BCD coded value in b0
b5 = b0/16*10 ; get tens part
b5 = b0 //16 +b5 ; then add units part
 
Last edited:
Top