Does frequent reading slows down DS3231 RTC???

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

Hi freaks

 

Recently i was building a precise timeclock with mega328 and GLCD and DS3231.I was using a Casio G-shock(Pretty accurate I think) as my reference.I found that my DS3231 clock was about 17sec slow as compared to my reference Casio after a 12hr runtime.At the whole time,DS3231 as well as the whole circuit was powered by external 5v(not by internal battery of DS3231).I was reading the DS3231 every second and was updating the reading to the GLCD.Then i thought to sync after every 5 mins.This time i ran the clock for nearly 24hr and found a delay of nearly 2s with my reference clock.

Now I upgraded the hardware(pulled up the sqw line and changed the cpu to mega32 for higher pincount) and set the DS3231 to give a square wave output of 1hz.Now the Idea is to sync every second for update without reading the RTC using level change interrupt.I have planned to read the RTC only two times a day(every am to pm conversion or vice-versa).

 

Is it normal for the DS3231 to get slow if reading it frequently??I have read the datasheet but found no such illustration.Someone faced this sort of things??Previously I have used DS1307,but in that case also,it was getting damn slow.So i thought t ask the experts......and here I am.

Last Edited: Thu. Jun 23, 2016 - 10:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you sure you have a 'real' ds3231? The cheap ones are Chinese copies which might explain the problem. As such, reading the chip should not slow it down.

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

Is the OSF bit set in the Status register ?
Are you writing anything into the Crystal Aging Offset register ?

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

No...I simply uploaded the Time related datas once and was reading it back every 1sec.I didn't write anything else anywhere.

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

I am not pretty sure if it is a chinese crap........

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

Just look at the price of a genuine DS3231 chip from a Maxim Distributor like Digikey, Farnell, ...

 

Then look at price of the Ebay modules from China including delivery.

I bought a Chinese module to see how it performs.    It works but is NOT accurate.

 

Likewise,   a lot of the DS1307 modules from Ebay are not as accurate as you would expect.   i.e. have crap crystals.

 

David.

Last Edited: Fri. Jun 24, 2016 - 08:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am in India and here Digikey/Farnell/Mouser doesn't sell to individuals/hobbyists.Although i have found an online store who imports from Adafruit & Sparkfun and sell breakout boards(The price is costly though).Please take a look at two breaouts regarding DS1307 & DS3231(Chronodot).

 

https://www.mgsuperlabs.co.in/estore/Real-Time-Clock-Module?search=RTC

 

https://www.mgsuperlabs.co.in/estore/ChronoDot-Ultra-Precise-Real-Time-Clock?search=Chronodot

 

Are they any worthy to give a try???Please reply..............

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

They might not sell to you. But you can still visit a US or European Distributor's website and see the dollar price.

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

Ok...but I asked if those modules from sparkfun/adafruit are any good ore not????

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

Everything seems incredibly expensive in India.   $48 for the Sparkfun DS3231 module.

 

I have only tried the cheap Ebay DS3231 modules.  e.g. http://www.ebay.co.uk/itm/PCS-DS3231-AT24C32-IIC-precision-Real-time-clock-module-memory-module-Arduino-/172183832510?hash=item2816f4c7be:g:RV4AAOSw-tNXIX1x

 

As you can see,  it is about $1.50 delivered to my door all the way from China.

 

In the US,  Digikey has http://www.digikey.com/product-detail/en/maxim-integrated/DS3231MZ-V-/DS3231MZ-V--ND/3133910

which is $6.50 for one chip.   And many more dollars for Tax and Delivery.

 

Sparkun products are generally very good.   I would assume that they use genuine Maxim chips.   I would assume that it is 100% accurate,  but only an owner can confirm this.

I can NOT believe that the $1.50 Ebay module has genuine chips.    My Ebay module is not very accurate.

 

David.

 

Last Edited: Fri. Jun 24, 2016 - 02:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply......

 

detecting the 1hz square wave using interrupt method is doing fine still now(still no drift wrt my Casio for nearly 24hr).I will put the clock in a long run and tell you whats happening further.But surely,reading the I2C frequently was making the counterfeit DS3231(as i assume from it's behaviour) slow.But i will order some real DS3231 breakout board or some bare chip (As i found a company who redistributes Mouser products in India) and also put them to test.

 

Another thing i would like you to ask that are you reading the DS3231 frequently or using some Interrupt driven method(like mine)??????

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

Reading I2C will not affect the timekeeping in any way.
Personally, I use the 1Hz output to tell the Arduino that the RTC has changed. I read the whole set of RTC registers. And display them on LCD or Serial.
There are many different RTC chips from every i.c. maker that use a 32kHz crystal.
Genuine RTC chips from ST, TI, Microchip, ... are pretty cheap, even from Distributors.
Their accuracy depends on the crystal. Genuine crystals are pretty cheap too.
.
Even so, Chinese Ebay vendors are so cheap that I suspect that some or all of the components are QC rejects. Or even blatant counterfeit. They work ok for hobbyist purposes but I would not want to use them in a commercial product.
.
I am fascinated how Farnell or Digikey work in India.
In the UK, I or any humble punter can buy anything from the big Distributors. They are quite happy to take my money and add a handling charge for small orders. I pay VAT in the usual way.
.
Obviously Farnell would prefer me to spend $1000s. They treat a small customer just as well as a large customer.
l suspect that if I spent $100000, they might offer me special terms.
.
I can understand that India will want to encourage / protect its home industries. E.g. you pay import duties or higher rate of VAT for imported goods. That is fine. You just weigh up the costs for imported or domestic electronic products. Choose what suits you. Pay for it.
.
Unlike other counties that have US restrictions, surely India is free to import what it likes. Your government just applies taxes.
.
David.

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

Hi Sharan. Same from my side. Upto 30min of delay every 24h. I am reading every sec via serial com. I would say that this method of readings too often induced a delay.

/jp

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

Rubbish. The RTC depends on the accuracy of the crystal.
The chip should oscillate reliably with a good crystal.
.
Cheap modules from Ebay probably contain reject crystals and reject chips
Replace the crystal if it is external e.g. DS1307. Replace the whole chip if it has integrated crystal e.g. DS3231.
Even a QC reject chip will work in most respects e.g. timekeeping ok but power is out of spec.
.
David.

Last Edited: Fri. Dec 8, 2017 - 08:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SHARANYADAS wrote:
i found a company who redistributes Mouser products in India

For the benefit of other readers, could you post a link to this distributor, or how to contact them.

We get a lot of India(n) visitors here and they may find that helpful!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

Why not go direct to https://www.mouser.in ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jp_costarica wrote:
Hi Sharan. Same from my side. Upto 30min of delay every 24h. I am reading every sec via serial com. I would say that this method of readings too often induced a delay.

 

I configured the DS3231 to give a square wave output of 1hz.Upon each pin change(configure the interrupt either on falling or on rising),avr will count 1s.Also I configured the avr in such a way so that it will only read the ds3231 in 12hr interval.Now using this method,my clock is running successfully over a year without almost no significant delay like i described before!!Still I am using the counterfeit chinese replica of the ds3231.....

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

Please post a program that gives the "wrong" time.

Please post the program that gives the "correct" time.

 

Make sure that you provide something that is buildable e.g. ZIP up an AS7 project or Arduino sketch.

It is easier to attach a ZIP than to paste long files into separate CODE windows.

 

I possess an Ebay DS3231 module.    I can probably replicate your AVR hardware.

 

David.

Last Edited: Tue. Dec 12, 2017 - 08:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW the 22pF caps don’t ‘decouple’ the clock signal - they are load capacitors for the crystal oscillator.

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

SHARANYADAS wrote:

Jp_costarica wrote:
Hi Sharan. Same from my side. Upto 30min of delay every 24h. I am reading every sec via serial com. I would say that this method of readings too often induced a delay.

 

I configured the DS3231 to give a square wave output of 1hz.Upon each pin change(configure the interrupt either on falling or on rising),avr will count 1s.Also I configured the avr in such a way so that it will only read the ds3231 in 12hr interval.Now using this method,my clock is running successfully over a year without almost no significant delay like i described before!!Still I am using the counterfeit chinese replica of the ds3231.....

 

Interesting.

In theory, reading a RTC should not affect the counter, but anything is possible....

eg If you say it was running faster, then maybe extra clock edges are being injected from the bus crosstalk.

Read should capture counters, but capture is not simple on ripple counters, as you need to be sure you do not capture while values overflow.

 

If you have access to a 1pps GPS system, you should be able to connect GSP-1pps and RTC 1pps, and report those two, and then vary a reading rate to see how that affects the two 1-second timings.

Capture of a time stamp would allow you to watch the exact phase of those 1pps, if the RTC does disturb, maybe it drops one or more 32khz edges to run slower as you report ?

 

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

Well, you're just repeating the claim there without giving any evidence.

 

Rather than just spreading unsubstantiated rumours on the interwebs, please do as as David requested in #18:

david.prentice wrote:
Please post a program that gives the "wrong" time.

Please post the program that gives the "correct" time.

 

Make sure that you provide something that is buildable e.g. ZIP up an AS7 project or Arduino sketch.

It is easier to attach a ZIP than to paste long files into separate CODE windows.

 

EDIT

 

typo

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Dec 13, 2017 - 09:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is true that rapidly reading an RTC should not cause timing errors.  Should not.  They should also latch all the time registers simultaneously at the very start of the read operation, so overflowing counters should be a non-problem.  While reading, the counters themselves continue to run, while the output data is held stable to read.

 

I'd like to add to 'Who-Me' in #21 who mentioned using a GPS time reference.  A Casio G-Shock is a perfectly good watch, but the atomic clocks of GPS are better.

 

Note that your cellphone probably uses GPS timekeeping, and if the phone doesn't, the phone network certainly does.  You won't be able to get sub-second precision, but averaged over a few hours should give useful results.

 

My other major suspicion would be a lousy 32kHz crystal.  If the chip has one built-in, like the DS3231, then it's a bum chip.  Note that even the best from the best vendors can still show up broken.  It's rare, but it happens.  They'll replace them if you can prove it.

 

S.

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

In his instructable, SHARANYADAS states that it is due to

SHARANYADAS wrote:
cheap chinese modules. These suckers stops the counting while you access the registers of the chip in order to read.

and

SHARANYADAS wrote:
because of cheap chinese crap chips

 

And states that this information came from AVRFreaks!

 

surprise

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What's the old proverb? 'a watched kettle never boils'. Thus the solution is to stop reading the time from the device and it will work properly.

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

Schrodinger's RTC ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you read it too fast, it might cause time dilation and a vortex to another dimension appears...... or i’ve been watching too much Regular Show.

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

I built the code as an AS7 project and ran it on a DS1307 because I could not find my DS3231 module.

The AVR counts the 1Hz SQW output from the DS1307.   And only reads the RTC at midnight.

 

Life is much simpler if people post projects as a ZIP.   It was painful to copy-paste each individual file from the Instructables site.

 

Update.   I have just found my DS3231.

I will try continuous I2C reads to see if it makes any difference.

 

I had forgotten just how ugly KS0104 displays are.

 

David.

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

Scroungre wrote:
I'd like to add to 'Who-Me' in #21 who mentioned using a GPS time reference. A Casio G-Shock is a perfectly good watch, but the atomic clocks of GPS are better.

I'll agree for e.g. calibration purposes.  But OP is getting gross errors that could be measured with a sundial.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

david.prentice wrote:
I will try continuous I2C reads to see if it makes any difference.

That will indeed be interesting.

 

Some years ago we were doing an app with a DS1305.  Strange RTC readings -- very very slow.  It turned out that the watch crystal wasn't crystalizing, and the chip would clock itself off the SPI "SCK".  (I can't see how that could happen with the symptoms here...)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Scroungre wrote:

...

I'd like to add to 'Who-Me' in #21 who mentioned using a GPS time reference.  A Casio G-Shock is a perfectly good watch, but the atomic clocks of GPS are better.

 

Note that your cellphone probably uses GPS timekeeping, and if the phone doesn't, the phone network certainly does.  You won't be able to get sub-second precision, but averaged over a few hours should give useful results.

 

sub-second checking would be possible with some timer-capture code in a small MCU - that would report the skew between GPS edges and RTC edges, and show the quanta of the errors.

( eg A missed Xtal edge, would change RTC dT by +30.517us )

 

david.prentice wrote:

Update.   I have just found my DS3231.

I will try continuous I2C reads to see if it makes any difference.

Cool. Look forward to some independent tests. 

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

I had a similar experience with one of my boards and the DS3231, the clock was running fast from memory.

 

After many hours of debugging, changing chips etc, I discovered some pulses, maybe 1V, ABOVE the 5V VCC, The pulses seems to change with the varying load.

 

I had omitted a large capacitor required on the 5V regulator I had never used before and it would overcompensate for the current varying requirement, once the PCB surgery was performed and added the cap all worked perfectly.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Ok,  I wrote a small sketch to run an an Arduino SMD clone.   Connect SQW pin to INT0 (digital #2).

I compared two Ebay DS1307 modules with my Ebay DS3231 module:

Compare DS3231 timing.   LED should toggle in ISR()
Thu 14 Dec 2017 01:39:11 PM
call I2C continuously for 300 secs
Elapsed time : 299985 ms,  288866 calls to read DS3231
Thu 14 Dec 2017 01:44:13 PM
do nothing for 300 secs
Elapsed time : 300002 ms
Thu 14 Dec 2017 01:49:15 PM.

Compare DS3231 timing.   LED should toggle in ISR()
Thu 14 Dec 2017 02:16:31 PM
call I2C continuously for 100 secs
Elapsed time : 99994 ms,  96287 calls to read DS3231
Thu 14 Dec 2017 02:18:13 PM
do nothing for 100 secs
Elapsed time : 100000 ms
Thu 14 Dec 2017 02:19:55 PM

Compare DS1307 timing.   LED should toggle in ISR()
Thu 14 Dec 2017 02:27:33 PM
call I2C continuously for 100 secs
Elapsed time : 99944 ms,  96265 calls to read DS1307
Thu 14 Dec 2017 02:29:15 PM
do nothing for 100 secs
Elapsed time : 99944 ms
Thu 14 Dec 2017 02:30:57 PM

Compare DS1307 timing.   LED should toggle in ISR()
Thu 14 Dec 2017 02:31:22 PM
call I2C continuously for 100 secs
Elapsed time : 99962 ms,  96282 calls to read DS1307
Thu 14 Dec 2017 02:33:04 PM
do nothing for 100 secs
Elapsed time : 99962 ms
Thu 14 Dec 2017 02:34:46 PM

 

 

You can see that the Arduino reports different times for each module.   The Arduino clone has an accurate 16mHz HF crystal.    Official UNOs have crap ceramic resonators.

In theory it should take 900us to read the RTC on a 100kHz bus.   The Arduino only manages 1038us with the Wire library.    The important point is that I have done 24 hours worth of RTC traffic with no noticeable difference.   

The SQW loop has a granularity of 1.04ms i.e. the read_DS3231() time.

The millis() has a granularity of 1-2ms.

 

So any difference of 5ms in 100000ms is acceptable.   The 300 second run shows a bigger difference i.e. 15ms

 

DS3231_busy.ino

#include <Wire.h>

const byte DS1307 = 0x68;
const byte DS3231 = 0x68;
const byte ledPin = 13;
const byte interruptPin = 2;
const int MAXSQW = 100;
volatile int16_t SQW_count;
volatile bool edge;
uint8_t hour, min, sec, wkday, day, mon, year;

char printbuf[80];
#define PRINTF(fmt ...)  { sprintf(printbuf, fmt); Serial.print(printbuf); }

// it is easy to use PCINTx instead of the Arduino's INT0
void init_pinchange(void)
{
    PORTC |= (1 << 3);       //pullup on PC3 (prevents floating)
    PCMSK1 = (1 << 3);       //monitor   PC3
    PCICR = (1 << PCIE1);    //enable IRQ
}

ISR(PCINT1_vect)                   //regular pin change on PC3
{
    if (PINC & (1 << PC3)) return; //ignore rising edge
    PORTB ^= (1 << 5);       //toggle LED
    SQW_count++;
    edge = true;
}

void sqw(void)                   //Arduino INT0 interrupt service call
{
    static uint8_t state;
    state = !state;
    digitalWrite(ledPin, state);
    SQW_count++;
    edge = true;
}

uint8_t write_reg_block(uint8_t slave, uint8_t reg, uint8_t *block, int8_t n)
{
    Wire.beginTransmission(slave);
    Wire.write(reg);
    Wire.write(block, n);
    return Wire.endTransmission();
}

uint8_t write_reg_val(uint8_t slave, uint8_t reg, uint8_t val)
{
    return write_reg_block(slave, reg, &val, 1);
}

void read_DS3231(uint8_t slave)
{
    Wire.beginTransmission(slave);
    Wire.write(0);
    Wire.endTransmission();
    Wire.requestFrom(slave, 7);  // request 7 bytes from slave device #2
    sec = Wire.read();
    min = Wire.read();
    hour = Wire.read();
    wkday = Wire.read();
    day = Wire.read();
    mon = Wire.read();
    year = Wire.read();
}

void show_time(void)
{
    char *daynm[] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
    };
    char *month[] = {
        "bum", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    read_DS3231(DS3231);
    uint8_t pmam = (hour & 0x60);   //weird 12 hour for Americans
    uint8_t hour12 = hour & ((pmam & 0x40) ? 0x1F : 0x3F);
    char *mode = (pmam & 0x40) ? (pmam & 0x20) ? "PM" : "AM" : "";
#if 1
    uint8_t idx = (mon >> 4) * 10 + (mon & 15);
    PRINTF("%s %02x %s 20%02x %02x:%02x:%02x %s\n",
           daynm[wkday - 1], day, month[idx], year, hour12, min, sec, mode);
#else
    PRINTF("%02x/%02x/20%02x %02x:%02x:%02x %s\n",
           day, mon, year, hour12, min, sec, mode);
#endif
}

void setup()
{
    //                       sec   min    hr wkday   day   mon  year
    uint8_t init_regs[] = { 0xD0, 0x47, 0x49, 0x05, 0x14, 0x12, 0x17, };
    int32_t read_count = 0;
    int32_t t;
    uint8_t status;
    Wire.begin();
    //    write_reg_block(DS3231, 0, init_regs, sizeof(init_regs));
    write_reg_val(DS1307, 0x07, 0x10);   //enable 1Hz SQW on DS1307
    status = write_reg_val(DS3231, 0x0E, 0x40);   //enable 1Hz SQW
    Serial.begin(9600);
    PRINTF("Compare DS3231 timing.   LED should toggle in ISR()\n");
    if (status != 0) {
        PRINTF("Failed to enable SQW:%d\n", status);
        while (1);
    }
    delay(1000);             //allow Serial to drain
    show_time();
    pinMode(ledPin, OUTPUT);       //LED
    pinMode(interruptPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(interruptPin), sqw, FALLING);
    PRINTF("call I2C continuously for %d secs\n", MAXSQW);
    delay(1000);             //allow Serial to drain
    for (edge = false; edge == false; ) ; //wait till next SQW
    t = millis();

    for (SQW_count = 0; SQW_count < MAXSQW; read_count++) {
        //        if (PINC & (1<<PC3)) PORTB |= (1<<5); else PORTB &= ~(1<<5);
        read_DS3231(DS3231);     //continuous reads
    }
    t = millis() - t;  //elapsed time
    PRINTF("Elapsed time : %ld ms,  %ld calls to read DS3231\n", t, read_count);
    show_time();
    PRINTF("do nothing for %d secs\n", MAXSQW);
    delay(1000);             //allow Serial to drain
    for (edge = false; edge == false; ) ; //wait till next SQW
    t = millis();
    for (SQW_count = 0; SQW_count < MAXSQW; ) {
        //nuffink
    }
    t = millis() - t;  //elapsed time
    PRINTF("Elapsed time : %ld ms\n", t);
    show_time();
}

void loop()
{

}

I thought hard about whether an example should be written for Arduino or bare AVR.

I concluded that everyone has an Arduino.    It should build out of the box.

 

If I posted an AS7 project with separate UART, I2C, timing functions for any AVR,   punters would be unlikely to try it for themselves.

 

David.

 

p.s. Civilised people can change the 12/24 format bit in the hour register (reg=2).

 

Edit.   Changed read_count from int to int32_t.   This makes sense now

Edit.   Updated reads for DS1307 comparisons.   Changed my conclusion.

Last Edited: Thu. Dec 14, 2017 - 02:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[LOL -- for the purposes of below, 120 seconds instead of 100 would have made my mental math easier.]

 

So what is the conclusion?  Doing mental math on the 100 second interval timestamps gives more than +/- 1 second readings, doesn't it?  And in the same direction for both types of devices.  But it seems to "catch up" so that the 200 seconds for both tests is OK?

 

Hmm--could it be that the timekeeping of the module is OK, but some update is being delayed?

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

david.prentice wrote:
The important point is that I have done 8 hours worth of RTC traffic with no noticeable difference

So any difference of 5ms in 100000ms is acceptable.   It would represent 15ms in a 24 hour period.

 

So nothing like the alleged

Jp_costarica wrote:
 Upto 30min of delay every 24h. 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you find the maths easier for 120 SQW pulses,   you only have to edit one line.

 

My conclusion was that the DS3231 does have a noticeable difference.    In a 24 hour period normal RTC programs would read the I2C every second.  i.e. 86400 reads in 24 hours.    My 96k reads seem to actually go faster by 5-6ms.

 

Either the RTC Silicon adjusts its internal compensation for the massive I2C activity.

Or the Arduino is somehow missing a few milliseconds.

 

I can't see where any RTC can lose 30 minutes in a day.

The Arduino clone has a good HF crystal.   It will have good short term stability.

The Arduino libraries handle Serial and Wire with interrupts.   The Timing is done with interrupts.    The AVR hardware Timers are 100% reliable.

 

I suggest that punters try the Arduino sketch for themselves.    I suspect that many people have DS1307.   And several have DS3231.

 

The real mystery is:   Why would anyone understand or want 12 hour format?

 

David.

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

david.prentice wrote:

My conclusion was that the DS3231 does have a noticeable difference.    In a 24 hour period normal RTC programs would read the I2C every second.  i.e. 86400 reads in 24 hours.    My 96k reads seem to actually go faster by 5-6ms.

 

Either the RTC Silicon adjusts its internal compensation for the massive I2C activity.

Or the Arduino is somehow missing a few milliseconds.

Good to see some numbers, but I did not see the reports for almost-no-readings ?

The 15ms in 300s is 50ppm, which is not great as drift, but with no A/B comparison, it may include absolute crystal ppm differences.

ie I'd expect a crystal to hold sub 10ppm in a short-term stable room, but out of the box, cheapest crystals sold are 50ppm, tho most give ±30ppm or ±20ppm, that does assume someone took the trouble to 'centre the caps' to remove circuit errors (few bother)

 

 

david.prentice wrote:

I can't see where any RTC can lose 30 minutes in a day.

The Arduino clone has a good HF crystal.   It will have good short term stability.

The Arduino libraries handle Serial and Wire with interrupts.   The Timing is done with interrupts.    The AVR hardware Timers are 100% reliable.

 

Yes, 30 minutes a day is very large, and seems due to 'some other effect', it may be cross talk, or voltage/power etc - that's why I suggest to the OP they connect a GPS 1 pps, and report/log the RTC-GPS time difference.

If they read in bursts then pause, such a log should show any difference effects due to the reading.

Just how much/how evenly the error jumps, should give good clues to just what is causing it.

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

I concluded that everyone has an Arduino.  

What? Lee doesn't have one.....

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

david.prentice wrote:

Either the RTC Silicon adjusts its internal compensation for the massive I2C activity.

Or the Arduino is somehow missing a few milliseconds.

 

I can't see where any RTC can lose 30 minutes in a day.

The Arduino clone has a good HF crystal.   It will have good short term stability.

The Arduino libraries handle Serial and Wire with interrupts.   The Timing is done with interrupts.    The AVR hardware Timers are 100% reliable.

 

The Arduino Xtal will also be temperature-sensitive.  Hammering the communication hardware will heat up the chip a bit more than doing nothing.  Real-Time Clock Chips are supposed to have compensation for temperature.  I don't think the Arduino does.  The difference will be small, but as Who-Me noted, we're talking tens of ppm here.  Not half an hour a day.  Something else is wrong with the OPs RTC.

 

Quote:

The real mystery is:   Why would anyone understand or want 12 hour format?

David.

Beats me!   And I don't have an Arduino either!  S.

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

Scroungre wrote:
 Something else is wrong with the OPs RTC.

and/or elsewhere in the OP's system ...

 

 

EDIT

 

typo

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Dec 14, 2017 - 08:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
The real mystery is: Why would anyone understand or want 12 hour format?

Does that include Henry Blake?

Fox Home Entertainment presents
M*A*S*H: Season Two (Collector's Edition) (1973)

"But general, you promised us help by noon! Well, what time is it now? 1300 hours? Well, uh... now, that's um... after noon, isn't it, sir? I mean, isn't it 12 up until it gets to be noon, and then you start all that hundred hour stuff? ...I wish the army would tell time like everybody else!"
- Lieutenant Colonel Henry Blake (McLean Stevenson)

http://www.digitallyobsessed.com...
 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

and the favourite of Ernie Wise in the plays what he wrote; eg,

 

Our story take place in 1748 (which is nearly ten to six) ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No,   I can't see why any interrupts would ever get missed in the regular Arduino platform.

But when I was walking my dog,  I thought that I would produce a non-interrupt version for AS7.

 

############# AS7 100kHz #####################################

Compare DS3231 timing.   LED should toggle in ISR()
Thu 14 Dec 2017 09:07:01 PM
call I2C continuously for 100 secs
Elapsed time : 99994 ms,  104994 calls to read DS3231
Thu 14 Dec 2017 09:08:43 PM
do nothing for 100 secs
Elapsed time : 100001 ms
Thu 14 Dec 2017 09:10:25 PM

############# AS7 400kHz #####################################

Compare DS3231 timing.   LED should toggle in ISR()
Thu 14 Dec 2017 09:22:43 PM
call I2C continuously for 100 secs
Elapsed time : 99973 ms,  394010 calls to read DS3231
Thu 14 Dec 2017 09:24:25 PM
do nothing for 100 secs
Elapsed time : 100001 ms
Thu 14 Dec 2017 09:26:07 PM

I get a similar result.   Ok, the millis() work much the same as Arduino.  i.e. I don't sync them.   So there is still a 1-2ms granularity.

But I still get 6-7ms difference with DS3231.    The DS1307 gave identical times.

I get 28ms difference when I run the I2C bus at 400kHz achieving almost 4x activity.

 

The DS1307 will only go at 100kHz.   So there is no point in trying at 400kHz.

 

Yes,   I suppose that the CMOS in the DS3231 will heat up with the extra activity.   Which will warm up the integrated crystal.

The DS1307 silicon will equally warm up.   But the external crystal will not.   Likewise,  the UNO crystal is external.

 

If my heating theory is correct,   I should get different results if I do nothing before doing continuous.

 

David.

Attachment(s): 

Last Edited: Thu. Dec 14, 2017 - 09:33 PM