RTC time running twice the speed with Time Library

Hi

I have a problem getting the time speed right when using the Time library. When using the RTC libraries alone and RTC.now the time speed is correct ( i.e. 30s timed as 30s). However when using the Time library and setSyncProvider the time is twice as fast ( i.e 30s on the arduino are 15 seconds in real time ). I am using Pro Mini 328 8Mhz. Also timeStatus() is correctly returing timeSet.

The sketch uploaded combines both examples.

Thanks

wtftime.c (1.98 KB)

#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS1307.h>
#include <Time.h>
#include <TimeAlarms.h>

RTC_DS1307 RTC;

time_t syncProvider()
{
return RTC.now().unixtime();
}

void setup()
{
Serial.begin(9600);

RTC.begin();
Wire.begin();
setSyncProvider(syncProvider);
RTC.now();

if(timeStatus() == timeSet)
Serial.println(“RTC OK”);
else if (timeStatus() == timeNotSet)
Serial.println(“Time’s clock has not been set”);
else if (timeStatus() == timeNeedsSync)
Serial.println(“Time’s clock is set, but the sync has failed, so it may not be accurate”);
}

void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
RTCTIME();
Alarm.delay(1000); // wait one second between clock display
}

void digitalClockDisplay()
{

// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits);
}

void RTCTIME()
{
DateTime now = RTC.now();

Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.day(), DEC);
Serial.print(’ ‘);
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.println();
}

Sample Output

2014/4/18 11:36:37
11:37:03
2014/4/18 11:36:38
11:37:05
2014/4/18 11:36:39
11:37:07
2014/4/18 11:36:40
11:37:09
2014/4/18 11:36:41
11:37:11
2014/4/18 11:36:42

Conflicts between Arduino libraries are common.

I suspect you could easily manage the timing without using the Timer library.

...R

The Time library uses the timer interrupt to track time between sync intervals.
You are using the M328 at 8Mhz vs the "normal" 16 Mhz.
The processor does not know how fast it is running.
The IDE tells the core code the speed of the processor at compile time
based on the board type selected.
My guess is that you have selected a board type that uses a 16Mhz clock
but your AVR is actually running at 8mhz.
When you do this, the core s/w will be confused because it was told
incorrect information.
The result is twice as many interrupts per second will happen than it was told would happen,
and so time will appear to run twice as fast.

My guess is that this is your problem.
I'd also bet that all your delay() calls would also finish twice as fast as they should.

Make sure to select a board type that has the F_CPU set to 8mhz vs 16Mhz.
You can verify the speed that the IDE is communicating to the core code by
turning on verbose output:
[File]->[Preferences]
and check the box

Show verbose output during: [x] compilation

When you build the code look at the output and
look for the F_CPU define.
It will look like:
-DF_CPU=8000000L

If you see this:
-DF_CPU=16000000L

Then you have a problem and need to pick another board type
or create one that for your board.

--- bill

Hi Bill,

Thank you very much for your help. It seems to have resolved the problem. I was infact using 8Mhz rather than 16Mhz.
My AVR is Atmel Mega 328P-AU.

This AVR is 20Mhz on the datasheets. Do you suggest I do anything custom about it or just continue using the standard 16Mhz option in my IDE?

Thanks Again !

Simon

The speed you see on the datasheet is the maximum.
The core code must be told the actual speed that the part is running.
The speed you tell the code doesn't modify the actual speed it merely
tells the core code what the speed is.
This is why they must agree.

If things are now working when you are telling the core code that the AVR
is running at 16Mhz, it must be running 16Mhz rather than 8mhz.

Are you sure the board you have was running 8mhz?

--- bill

Hi Bill

On Arduino IDE 1.0.5-r2 both options work.

Arduino Pro or Pro Mini (5V,16Mhz)/w AtMega328
Arduino Pro or Pro Mini (5V,8Mhz)/w AtMega328

I had resorted to 8Mhz by mistake whilst debugging other issues with USB to TTL RS232.
Since it worked for me with 8Mhz I had stayed that way. Now my RS232 issues are resolved and so seem the 16Mhz.

Great suggestions Thanks !

Simon

sjxuereb:
Hi Bill

On Arduino IDE 1.0.5-r2 both options work.

Arduino Pro or Pro Mini (5V,16Mhz)/w AtMega328
Arduino Pro or Pro Mini (5V,8Mhz)/w AtMega328

Both will allow code to be created and uploaded to the AVR,
but only only works correctly.
As you have seen, if the core code is told the incorrect clock speed, anything
timing related will not work correctly.
That includes delays, baud rates, h/w timers, PWM frequencies etc…

— bill