Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with a slow PIC RTC timing

Status
Not open for further replies.

urakhai911

Newbie level 6
Joined
Apr 15, 2010
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,413
Hey EDA World,

My PIC RTC is running on a 32.768 kHz secondary oscillator, yet the timing that it gives me is never one second. Over the course of 20 minutes, the PIC is usually 10-20 seconds behind. I need to have my PIC run for a month while keeping track of real time, so this problem would be a real inconvenience for me. Is there anyone out there that has encountered this problem before? Or could you help me look for reasons why this might happen?

-Urakhai
 

Re: PIC RTC is slow

why this might happen?

the 34th line down in your code, the 2nd variable from the left is wrong.
 

Re: PIC RTC is slow

I guess I deserved that sarcasm. Here is my code.

Some Notes: The __TIME__ variable is getting the compile time and transferring it to the PIC. This is the only way I have been able to transfer the system time to the PIC.

Code:
/****************************************************************************//
//                        RTC_Config                                                                          //
//****************************************************************************//
void RTC_Config(){

//Enables the LP OSC for RTCC operation
asm("mov #OSCCON,W1");
asm("mov.b      #0x02, W0");
asm("mov.b      #0x46, W2");
asm("mov.b      #0x57, W3");
asm("mov.b      W2, [W1]");
asm("mov.b      W3, [W1]");
asm("mov.b      W0, [W1]");       

// OSCCON unlock sequence, setting SOSCEN
asm volatile ("mov #OSCCON,W1");
asm volatile ("mov.b #0x46, W2");
asm volatile ("mov.b #0x57, W3");
asm volatile ("mov.b #0x02, W0"); // SOSCEN =1
asm volatile ("mov.b W2, [W1]");
asm volatile ("mov.b W3, [W1]");
asm volatile ("mov.b W0, [W1]");

_SOSCEN =1;

// RCFGCAL unlock sequence, setting RTCWREN
asm volatile("disi #5");
asm volatile("mov #0x55, w7");
asm volatile("mov w7,_NVMKEY");
asm volatile("mov #0xAA, w8");
asm volatile("mov w8,_NVMKEY");
asm volatile("bset _RCFGCAL, #13"); // RTCWREN =1;
asm volatile("nop");
asm volatile("nop");

__builtin_write_OSCCONL(0x02);

_RTCWREN = 1; 

RtccInitClock();  //Initialize the clock
RtccWrOn();    //Enable Write

rtccTime time;  //Create a time data structure

char hourTens[] = {__TIME__[0], '\0'};  //Get Compile Time
char hourOnes[] = {__TIME__[1], '\0'};

char minTens[] = {__TIME__[3],'\0'};
char minOnes[] = {__TIME__[4],'\0'};

char secTens[] = { __TIME__[6],'\0'};
char secOnes[] = { __TIME__[7],'\0'};

secTime = (( atoi(secTens)<<4) & 0b11110000) | ( (atoi(secOnes) )& 0b00001111);  
minTime = (( atoi(minTens)<<4) & 0b11110000) | (atoi(minOnes) & 0b00001111); 
hoursTime = (( atoi(hourTens)<<4) & 0b11110000) | (atoi(hourOnes) & 0b00001111); 

time.f.sec = secTime; 
time.f.min = minTime;
time.f.hour = hoursTime;

RtccWriteTime(&time,TRUE);

RCFGCALbits.RTCEN=1; //Turn on RTC!

// disable alarm
_ALRMEN = 0;

rtccRepeat repeat = RTCC_RPT_HOUR;
RtccSetAlarmRpt(repeat,TRUE);
RtccSetAlarmRptCount(1,TRUE);
RtccSetChimeEnable(TRUE,TRUE);


rtccTime time2; 
time2.f.sec = 0;
time2.f.min =minTime + 1 ;
time2.f.hour =  hoursTime;

RtccWriteAlrmTime(&time2);

_RTCWREN = 1; 
_ALRMEN = 1; // enable alarm
_RTCIF = 0; // clear interrupt flag
_RTCIE = 1; // enable interrupt

}
 

Re: PIC RTC is slow

Hi,

A 32k RTC should, from the projects I have done, be able to deliver accuracy to about 3-4 seconds per week.
You can either trim that by changing the xtals caps to the next value higher or lower or make a compensation adjustment in your program if you want less error.

Also make sure the xtal and caps as carefully soldered as close to the pic chip as possible.
Breadboarding xtals is a well known cause of such problems, in which case mount the xtal and caps on some strip board then plug the three leads with short good wires as close to the pic chip as possible.

Also have a look at this site for a zero error 1 second timer if you want something like that - though have not tried it myself.
Only do assembler so cannot help with your code

Zero-error 1 second timing algorithm
 

Re: PIC RTC is slow

Did you consider to use real RTC chip and via i2c to connect them to PIC so you get accuracy.


Mr.Cube
 

Re: PIC RTC is slow

Hi,

Did you consider to use real RTC chip and via i2c to connect them to PIC so you get accuracy.

As the RTC chips also need to use at 32k crystal don't see how they can be any more accurate than one on the Pic chip..?
 

Re: PIC RTC is slow

Apart from what's been said about possible crystal circuit issues, you didn't tell about the involved PIC24 type. Some have the option to select LPRC instead of SOSC clock source for the RTCC, which would result in a poor timing accuracy similar to the reported.

Curiously, some sequences in your code are repeated at least twice in different form.
 

Re: PIC RTC is slow

As the RTC chips also need to use at 32k crystal don't see how they can be any more accurate than one on the Pic chip..?

You're right, but, RTC is only frequency divider, simple chip.
I used PCF8583 a few times, and it's has one pin with frequency out. You use variable capacitor to tune frequency to 1Hz, and when you have 1 Hz, RTC is accurate.


Mr.Cube
 

Re: PIC RTC is slow

Hi,
I used PCF8583 a few times, and it's has one pin with frequency out. You use variable capacitor to tune frequency to 1Hz, and when you have 1 Hz, RTC is accurate.

Fair comment, have not used those types of chips as never had the need for such accuracy and always try to keep the chip count down - one to bookmark though.
 

Re: PIC RTC is slow

Hi,



As the RTC chips also need to use at 32k crystal don't see how they can be any more accurate than one on the Pic chip..?
RTC chips can be very accurate:) Please look at Maxim DS3234 chip for example. I have used it in my project and accuracy is very good without any user calibration. It is also easy to implement.
 

Re: PIC RTC is slow

I had a similar problem with a PIC18F46J11 which lost several seconds every minute. The crystal WAS running at 32768Hz exactly as intended. I don't know if the same applies to your PIC24 device because you haven't told us which one it is, check you are using the crystal oscillator as your timing source. Everything seemed to work properly, including the alarm registers but just a little too slow. The problem was one bit in one register which was making the RTC module derive it's source from the internal slow clock. Unfortunately, it runs at 31KHz not 32.768KHz !! Being so close in frequency it's easy to be misled about the problem.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top