DS1307 runs slow

ed1066

Member
Folks

I can't see where I'm going wrong with my DS1307 project. The clock reports time very slow - about 40 secs reported for every actual min passed.

Here is a sketch of the circuit:

Circuit sketch.jpg

And here is the code. It's a 20X2 chip. I used the wizard under 'AXE110 Datalogger' to set the clock then ran the code below.

Code:
symbol secs = b10
symbol mins = b11
symbol hours = b12
 
 	HI2CSETUP I2CMASTER, %11010000, I2CSLOW, I2CBYTE                'prog time ds1307

main:


hi2cin 0,(secs,mins,hours,b0,b1,b2,b3,b4)

LET secs = BCDTOBIN secs                                   'Time from ds1307 needs converting                                   
LET mins = BCDTOBIN mins                                  
LET hours = BCDTOBIN hours                                      
 

      sertxd ("DATA,DATE,TIME,", #hours,",", #mins, ",", #secs, ",",  #b0, ",", #b1, ",",  #b2, ",", #b3, ",", #b4, cr) 
'debug b1
pause 1000

GOTO main
the impact was the same with both sertxd and debug used to monitor the time. I did notice tho' that the clock ran correctly when I took the power from the circuit and so the clock used its backup battery.

The value in pause affects the error. So I guess the interogation by hi2cin is upsetting the DS1307. I have checked the hardware for shorts and faulty connections with no obvious errors found - the various resistances all seem spot on.

The 5V comes from a 78L05 regulator with a 12V input. The 12V is from a switched mode power supply, but I checked using a 12V battery and it was the same.

There is an extra 1m on the download cable, I soldered a 3 core 1m lead to the circuit board then on the other end I soldered the stereo jack for the download cable .

I have not shown on the sketch an output I have ready on pin C.5, (that's 5th pin down from top left, 6th up from bottom). The pin is pulled down to ground with a 10K resistor and then is conected to the signal input of a FET (TRT003 - FET IRF520). There is nothing on the FET output.

Any suggestions welcomed

Cheers

Ed
 

westaust55

Moderator
Are you using a 12 pf crystal (not a 6 pf type).
The DS1307 internal oscillator circuitry is designed for operation with a crystal having a specified load capacitance (CL) of 12.5pF.

Is your circuit constructed on a project board, a protoboard or on a breadboard?
Breadboards due to the parallel conductors within the board can introduce capacitance. There may not be good connections if the crystal leads are directly into a breadboard
Are the leads as close as possible to the DS1307 to minimise external influences.
 
Last edited:

hippy

Ex-Staff (retired)
It does seem odd and I first thought it could be crystal, but that it runs okay on backup battery when powered-down suggest that's not it.

The DS1307 should not be affected by I2C comms or it would not be a viable product and I've not noticed any issues in that respect myself.

The only conclusion I can come up with is that the crystal / oscillator is being affected when the PICAXE and rest of the circuit is powered up. That could be cross-talk with the crystal and its oscillator or something similar as Westaust55 suggests, and most likely with a breadboard circuit.
 

srnet

Senior Member
The DS1307 datasheet does give specific layout requirements for the crystal, and there is this comment;

"AVOID ROUTING SIGNAL LINES IN THE CROSSHATCHED AREA (UPPER LEFT QUADRANT) OF THE PACKAGE UNLESS THERE IS A GROUND PLANE BETWEEN THE SIGNAL LINE AND THE
DEVICE PACKAGE"
 

DamonHD

Senior Member
I had to plug my watch crystal into a header plugged into the breadboard to get a decent contact.

I've had no trouble with DS1307 at 5V or DS1306 at 5V or 3.3V with a (6pF) watch crystal on a couple of breadboards.

Rgds

Damon
 

nick12ab

Senior Member
Try straightening the DS1307 crystal pins (so that they don't go in the breadboard) and soldering the crystal directly to these pins.

Also that 3.3V cell is connected backwards and there is only one decoupling capacitor for the whole circuit - perhaps those are affecting things. Actually the entire DS1307 is backwards but that must be a mistake as it wouldn't work at all if that was the case.
 

ed1066

Member
Thanks for all the help. So quick too.

The watch crytal is the 12pF - "as required by DS1307" from techsupplies.co.uk

Its a breadboard. I'll try shortening the parallel connector strips from those pins.

The crystal was as close as I could get it to the DS1307. I cannot measure any resistance between the chip pins and the wire leads from the crystal.
I have tried removing the crystal and re-soldering another one, but there was no effect. So its now one hole away from the chip.

No signal lines anywhere near the upper left quadrant of the chip.

Damon - for me a 'header' is meaty pins coming out of the board. I can solder the crystal to such a header and then the header to the board. the connection should improve.

There are 2 signal connections between the DS1307 and the PICAXE, is one of them to write to the clock and one to read the time? If so a useful test might be to cut the writing connection after I've set the clock. Is that a plan? If so which is which?

Another test might be to power the DS1307 completely independently of the PICAXE. Is that worth a go?
 

ed1066

Member
Try straightening the DS1307 crystal pins (so that they don't go in the breadboard) and soldering the crystal directly to these pins.
Good idea - I'll try that

Also that 3.3V cell is connected backwards and there is only one decoupling capacitor for the whole circuit - perhaps those are affecting things. Actually the entire DS1307 is backwards but that must be a mistake as it wouldn't work at all if that was the case.
Yes - 3.3V cell is correct on board - sorry. Another decoupling capacitor right next to the DS1307 is an easy test. I'll report back
 

westaust55

Moderator
Another test might be to power the DS1307 completely independently of the PICAXE. Is that worth a go?
I do not believe using a separate supply will help if you have a good regulated 5V supply.
I believe the vast majority (read all) foru members using the DS1307 have it powered from the same supply as the PICAXE chip.

Damon - for me a 'header' is meaty pins coming out of the board. I can solder the crystal to such a header and then the header to the board.
It would be worth trying that. Crystal leads are known to be thin and cause problems with good contact.
In the past even soldering to the off-cut leasts from a resistor has been suggested to achieve a better connection when using a breadboard.
 

Buzby

Senior Member
... There are 2 signal connections between the DS1307 and the PICAXE, is one of them to write to the clock and one to read the time? ...
No, they are both need for reading and writing. One is the I2C control clock line, and the other is the I2C serial data line.
The clock line controls the I2C timings, and the data line is bi-directional for read/write.

Loads of info on I2C here in the forum, in the manuals, and all over the Net !.

Cheers,

Buzby
 

DamonHD

Senior Member
By header I mean the female turned-pin strip.

I can try and find you a link if you need but I'm stuck on a roof and my hands are freezing!
 

fernando_g

Senior Member
It could be the 32.768 Khz crystal...which, it is not a crystal but a tuning fork.

Tuning forks drastically change their frequency when overdriven. Some tuning forks are designed to be operated with sub-microwatt signals, like those found in single or dual cell apps.

Many, many moons ago, I built a time-lapse camera timer using exclusively CD40xx CMOS logic. The oscillator was the classic unbuffered-inverter (CD4069U) circuit, with a 1 Meg bias resistor between input and output. It was powered by a pair of alkaline AA batteries and worked great. Since sometimes the time lapses spanned a couple of weeks, I decided to add an external power supply option, which in this case, would output 5 volts. To my surprise, the timing went berzerk!! With a scope, I noticed that the oscillator would hiccup on and off.

I did EVERYTHING I could think of, to no avail. To make a long story short, I had to lower the external supply to about 3.3 volts, and the thing worked just fine.

Later, I lost interest in time lapse photography and forgot about this circuit.

One day browsing the web, I found an app note from Seiko or someone else, where they indicated that there are nano-power tuning forks, and that those should not be driven from 5 V logic. A solution they offered, is that on the inverter's output a series resistor between 10k and 47k would do the trick. Incredulous, I dug up the old project, modified it and was surprised that it would work just fine at 5 volts!!
 

marks

Senior Member
Hi ed1066,
try lowering your I2c pullups 2.2k is a much better choice will ensure a much cleaner signal especialy on a breadboard.
 

Armp

Senior Member
It works OK when you are on the battery - right? And the effect is dependent on the pause parameter and hence I2C activity?

So 2 obvious questions:
What is the actual measured voltage of the battery?
How long are the 2 signal wires between the picaxe and the clock chip?
 

oracacle

Senior Member
if its any help, i found that a clock i had built gave erratic reading from the DS1307 when the batteries started getting low, around the 3.8 volt mark.
 

nick12ab

Senior Member
if its any help, i found that a clock i had built gave erratic reading from the DS1307 when the batteries started getting low, around the 3.8 volt mark.
That's either because:
  • The DS1307 is only rated for 4.5V or above - so you won't always get away with less.
  • When Vdd is less than 1.25*Vbat, i2c access is inhibited - for Vbat of 3V that means the power supply needs to be more than 3.75V so your symptoms support this suggestion.
 

ed1066

Member
Reporting back

Thanks for all the replies, its wonderful to have such a helpful Forum

I've done more work using the guidance.

First I tried it on another PC with another download cable, and it was largely fine. For the first PC & download, I noticed plugging and unplugging a connection to an LCD (AXE133) briefly upset the timing.

So firstly I added another 100nF capacitor next to the DS1307, I also shortened the breadboard strips by the crystal and I soldered the crystal to a header and then to the board.

After all that I was amazed to find the problem reversed! Time keeping was correct when powered by the 5V from the main circuit, but lagged by about 30% when running from the 3.3V backup battery.

However, the problem only occurred when the download cable remained connected when the power to the board was switched off. Also no problems at all with the 2nd PC and download cable. In each case there is my 1m of cable from the board to the stereo socket where the download cable plugs in. Might this be related to the symptoms?

So I've now got a working board. I'll start adding more components and I may be scurrying back here if they trip in the error again. (But first there are some suggestions above that I haven't needed to try). Meanwhile, if you see reasons for the symptoms, don't hestate to respond.

Now some replies to questions above in the thread :

DamonHD - thanks - I understand the header issue now

Armp - actual measured voltage of the battery - 3.14V (& 4.99V from regulator)
- length of the 2 signal wires between the picaxe and the clock chip - 2cm & 3cm

Cheers, Ed
 

nick12ab

Senior Member
However, the problem only occurred when the download cable remained connected when the power to the board was switched off. Also no problems at all with the 2nd PC and download cable. In each case there is my 1m of cable from the board to the stereo socket where the download cable plugs in. Might this be related to the symptoms?
When the RS232 download cable is used, a negative voltage is present on the Serial In pin which is current limited by the download circuit resistors and clamped to the power rail by the internal clamping diodes in the PICAXE. This insignificant current will not damage anything but it can interfere with sensitive operations, such as the DS1307 running on backup power.
 

Armp

Senior Member
When the RS232 download cable is used, a negative voltage is present on the Serial In pin which is current limited by the download circuit resistors and clamped to the power rail by the internal clamping diodes in the PICAXE. This insignificant current will not damage anything but it can interfere with sensitive operations, such as the DS1307 running on backup power.
From the DS1307 Spec:
WARNING: Negative undershoots below -0.3V while the part is in battery-backed mode may cause loss of data.
 

ed1066

Member
Nick & Armp - Thanks - this is good to know.

It removes any background worry that my system has dodgy wiring (except I can never obliterate that worry for my projects)
 

ed1066

Member
I guess that I expected things would get tricky as I added more to the board. In particular more temperature probes, switches and the LCD.

The clock lost time again in a fairly unpredictable manner, but the common factor was the download cable. So following the help from Nick and armp I thought a diode between the 5V at the PICAXE and the 5V input to the DS1307 would help. And it did. All works solidly both with and without the download cable connected. The voltage to the DS1307 is well in excess of the 3.75V limit, about 4.5V

Thanks again, Ed
 

russbow

Senior Member
a diode between the 5V at the PICAXE and the 5V input to the DS1307 would help. And it did.
Then what mistake has this covered up then? Your sketch in post #1 shows the two supplies as commoned, as they should be. An additional diode would be shorted out.

Can you confirm that you measure +5 volts at the Vcc pin of the DS1307 without the diode. If not, you need to have a look at your wiring.
 

westaust55

Moderator
The voltage to the DS1307 is well in excess of the 3.75V limit, about 4.5V
The 3.75 V is the point where the DS1307 changes to the back up battery.
For normal operation with working i2c comms, by the Datasheet the minimum voltage is 4.5 V and the typical working voltage is 5.0 V.

While some folks may have had their DS1307 working at lower voltages, other have had problems when using 3 x AA cells then were not at peak voltage.

There should be no need for a voltage dropping diode so as already suggested, it would seem there is something wrong in the circuit that still needs to be found.

Could be time to post a photo of your circuit for folks here to review. Such photos need to be clear with a clear background, taken vertically/perpendicular over the board and the wiring needs to be easily traced end to end.
 

MartinM57

Moderator
Clutching at straws, where was the DS1307 obtained from? I have some eBay sourced DS1307 that I'm not convinced are genuine/first grade items...but their symptons is that they don't always come up on the "bus" when Vcc is restored - but when they do, they keep good time
 

ed1066

Member
Photo of circuit

Could be time to post a photo of your circuit for folks here to review. Such photos need to be clear with a clear background, taken vertically/perpendicular over the board and the wiring needs to be easily traced end to end.
Here we go. As I said I've now added more. The circuit sketch is updated here, additions are in red and the "TS" connections are the signals to external temperature probes, DS18B20's. The diode can be seen.

Circuit sketch2.jpg

And here is an annotated photo.

BOARD annotated.jpg

I will post again with 2 more photos to show it connected up to LCD etc
 

ed1066

Member
BOARD 1.jpgBOARD 2.jpg

2 more photos to show it connected up to LCD etc

Martin - its a Kosher techsupplies DS1307. The DS18B20's are from China and they've performed well so far in my other projects.
 

MartinM57

Moderator
Don't understand the diode on the DS1307 Vcc pin - why do you think you need it? I've never seen any other circuit with it present.

Pull-ups look OK, even though they look like pull-downs :)

Your 3.3v Vbat is the wrong way round on the schematic - check how you've wired it
 

westaust55

Moderator
Its a breadboard. I'll try shortening the parallel connector strips from those pins.
If you are still talking the same "board" then more correctly it is a proto-typing board.
Ideally we also need to see the under-side where the soldering has been done as well since it is a proto-board.

As I said I've now added more.
IMHO, it would be far better to resolve the problem with the DS1307 before adding additional parts to the circuit.


As a matter of convention when drawing a schematic/circuit diagram the positive bus/wire is drawn at the top and the ground (0V) bus/wire is drawn at the bottom of the circuit.
If that is followed then readers do not get confused as easily when trying to review a schematic diagram.
 

russbow

Senior Member
The heading for this thread was DS1307 runs slow

It is time to go back to basics. I refer you to posts #23, 28,29 and the comments throughout the thread that imply change of operation when the download cable is disconnected. This should make no difference

I suspect a basic wiring fault. As you have soldered up a stripboard identifying a fault is much more difficult.

Check all your component terminations. Check all the "cut strips" are really cut. Run a screwdriver blade down between each copper strip. Get you meter out and check for continuity ( especially post # 23 ) and check for short circuits between strips and across cuts. Use a magnifying glass.

Until the basic problem is resolved, you are wasting you time developing the circuit further.
 

ed1066

Member
The heading for this thread was DS1307 runs slow

It is time to go back to basics. I refer you to posts #23, 28,29 and the comments throughout the thread that imply change of operation when the download cable is disconnected. This should make no difference

I suspect a basic wiring fault. As you have soldered up a stripboard identifying a fault is much more difficult.

Check all your component terminations. Check all the "cut strips" are really cut. Run a screwdriver blade down between each copper strip. Get you meter out and check for continuity ( especially post # 23 ) and check for short circuits between strips and across cuts. Use a magnifying glass.
I checked carefully for shorts and continuity after each operation on the board. I confirmed above that the actual measured voltage of the battery was 3.14V & the Vss pin was 4.99V from the regulator.

I have now run the screwdriver blade (which is a great tip) and there was some crud in places. So I re-introduced a wire in parallel with the diode (i.e. to make it redundant) and repeated the tests with the problematic download cable. I couldn’t then reproduce the error. So you’ve nailed it, I guess.

I think it’s the filings when I use that hand held twist drill thingy to cut a strip. I tried to clear it with a good blow and then checked for shorts with the meter. I’ll now do the screwdriver blade clean out as well, and look at it with a magnifying glass for good measure.

Until the basic problem is resolved, you are wasting you time developing the circuit further.
From my point of view I had resolved it - with the diode. I suppose it was cutting out noise which might have been tripping the Vss to below the 3.75V threshold, or perhaps its effect in reducing Vss from 5V to 4.5V might have been the effect.

Thanks russbow and all other help.

Ed

PS while you’re all still here … do people have experience of the “insulation-displacement connectors” which plug into the headers? (see photos in post #27 above). Mine are TE CONNECTIVITY / AMP - 3-640441-3 – HOUSING bought from CPC. I really like them, except the wire to blade connection is not always reliable. There is a handheld tool to push in the wires to blades I’ve been using. Any thoughts?
 

nick12ab

Senior Member
I suppose it was cutting out noise which might have been tripping the Vss to below the 3.75V threshold, or perhaps its effect in reducing Vss from 5V to 4.5V might have been the effect.
Vss? That is another name for the Gnd pin. Don't you mean Vcc* or Vdd?

*Vcc (collector) should usually be used for ICs using bipolar transistors and Vdd (drain) for ICs using MOSFETs.

"CMOS ICs have generally borrowed the NMOS convention of VDD for positive and VSS for negative even though both positive and negative supply rails connect to source terminals (the positive supply goes to PMOS sources, the negative supply to NMOS sources). ICs using bipolar transistors have VCC (positive) and VEE (negative) power supply pins."
 

russbow

Senior Member
From my point of view I had resolved it - with the diode. I suppose it was cutting out noise which might have been tripping the Vss to below the 3.75V threshold, or perhaps its effect in reducing Vss from 5V to 4.5V might have been the effect.
Unlikely, but if it was then you need to look at your de-coupling capacitors

I re-introduced a wire in parallel with the diode
Snip them both off - not needed. Then check Vcc on time chip. This will prove supply cct OK

As a side comment, IDC terminations were / are pretty standard in the UK telephone network.
 

john2051

New Member
Hi, just a thought. Could you add a trimmer cap to ground, and measure the frequency across
the crystal with a high impedance frequency counter?
regards john
 

premelec

Senior Member
Freq test Cap adjust method

If you have the 1 second output DS1307 pulse and an oscilloscope and a time receiver with one second tick sounds [WWV is what I've used] - you can feed the receiver into the scope horizontal and use external trigger from the 1 second pulse and use your trimmer capacitor to adjust that the tick pulse image on the scope doesn't drift from one position - then you are in sync...
 

ed1066

Member
Apologies

Don't you mean Vcc* or Vdd?
Apologies all - I should have checked. Its (according to the DS1307 datasheet) Vcc

John & premelec - sorry I have no facilities to measure frequency.

russbow - OK, I'll snip out the diode and just leave the 5V from the regulator supplying Vcc

Cheers, Ed
 

hippy

Ex-Staff (retired)
PS while you’re all still here … do people have experience of the “insulation-displacement connectors” which plug into the headers?
IDC connections are industry standard, most ribbon cable uses them in their connectors, and are very reliable when assembled correctly. The 'tuning fork' end of the pin slices through the wire insulation and then grips the wire.

I've had some problems manually assembling them a wire at a time if the tool used is not the one for the job and it does take practice. 'Krone' tools for RJ45 are not the same as 'BT' tools for telephone connectors, but both better than trying to push the wire in with the end of a screwdriver. If the prongs are spread too far apart they don't spring back and grip the wire.

John & premelec - sorry I have no facilities to measure frequency.
You do if you have a PICAXE ! Simply use COUNT for a suitable period of time and then scale the result.
 

Armp

Senior Member
If you have the 1 second output DS1307 pulse and an oscilloscope and a time receiver with one second tick sounds [WWV is what I've used] - you can feed the receiver into the scope horizontal and use external trigger from the 1 second pulse and use your trimmer capacitor to adjust that the tick pulse image on the scope doesn't drift from one position - then you are in sync...
My 1sec output reads 999,983us on a cheap logic analyzer - confirmed with the clock running about 1.4 secs/day fast.
Which end of the Xtal should the trimmer go, or does it need both ends?
I think DS1307 pin 2 is the driver output, and pin 1 is the osc input.
 

g6ejd

Senior Member
here's my clock routine, notice the first few lines, where I subtract a second from each day, to counteract a clock that gains 1-sec day! Works great for me. Been running since 03-Mar-13 and has kept good time, it's about 1.5 secs fast at present, but over 23-days that's a lot better than the +23-secs it would have been. I determined the offset by trial and error. One could do an update every set number of days, or whatever to correct a drifting clock.

Code:
display_time:
      i2cslave %11010000, i2cslow, i2cbyte
      readi2c 0,(secs,mins,hours,day,date,month,year)
      read 30,i 'Read last day time was adjusted
      if i <> day THEN
        secs = secs - 1
        writei2c 0,(secs,mins,hours,day,date,month,year)
        write 30,day
        i = 255
      ENDIF 
 	data0     = hours & $0F
	samp_time = hours & $F0
	samp_time = samp_time / 16 * 10
	samp_time = samp_time + data0
	'hours = hours - 1
	BCDTOASCII hours,htime,ltime
	SerOut LCD,BAUD,("TP",0,0,"TT")
	SerOut LCD,BAUD,(htime, ltime,":")
	BCDTOASCII  mins,htime,ltime
	SerOut LCD,BAUD,(htime, ltime,"Hr",$00)
	Serout LCD,BAUD, ("TP",0,1,"TT")
	Select Day
	Case 1
	 SerOut LCD,BAUD,("Sun")
	Case 2
	 SerOut LCD,BAUD,("Mon")
	Case 3
	 SerOut LCD,BAUD,("Tue")
	Case 4
	 SerOut LCD,BAUD,("Wed")
	Case 5
	 SerOut LCD,BAUD,("Thu")
	Case 6
	 SerOut LCD,BAUD,("Fri")
	Case 7
	 SerOut LCD,BAUD,("Sat")
	Endselect
	 BCDTOASCII  date,htime,ltime
	 SerOut LCD,BAUD,(" ",htime, ltime,"/")
	 BCDTOASCII  month,htime,ltime
	 SerOut LCD,BAUD,(htime, ltime,"/")
	 BCDTOASCII  year,htime,ltime
	 SerOut LCD,BAUD,("20",htime, ltime)
	 if i = 255 THEN SerOut LCD,BAUD,("*",0) ELSE SerOut LCD,BAUD,(0) ENDIF
	Return
 
Top