CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

RTC slow, using LPRC source

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

RTC slow, using LPRC source
PostPosted: Tue Feb 26, 2019 9:01 am     Reply with quote

Hi,

I am using the PIC24FJ128GA308, with CCS v5.082.

I am wondering if there is any way to calibrate the RTC when I am
running off the LPRC source, since it's only 31 kHz instead of the 32768 Hz
of a normal clock crystal. I read somewhere that using the LPRC source
will cause the RTC to run slow. The 127 ticks per fifteen minutes afforded
by the RTC calibration is not enough.

It turns out that my RTC is about 3 seconds slow per minute (95%). And
31 kHz is about 95% of 32768 Hz. Hrmmm...

Here's my setup code:
Code:

rtc_time_t realtime;

setup_rtc(RTC_ENABLE | RTC_CLOCK_INT, 127);
realtime.tm_year = 18;
realtime.tm_mon = 2;
realtime.tm_mday = 8;
realtime.tm_hour = 23;
realtime.tm_min = 58;
realtime.tm_sec = 00;
rtc_write(&realtime);


I have a certain idea... something like this to compensate for the
slowness:
Code:

uint8_t timerSecondCount = 0;

void t3_isr() // T3 runs at 100 Hz
{
    if (++timerSecondCount >= 100) // 1 second * 100 Hz
    {
        timerSecondCount = 0;

        rtc_read(&realtime);
        if (realtime.tm_sec == 11)
        {
            realtime.tm_sec = 14;
            rtc_write(&realtime);
        }
    }
}


I do the time skip at 11 seconds, so that I can avoid any rollover
problems. Do you guys see any problem with doing this?
gaugeguy



Joined: 05 Apr 2011
Posts: 288

View user's profile Send private message

PostPosted: Tue Feb 26, 2019 10:14 am     Reply with quote

You do realize that the LPRC can vary by +/- 20% over temperature and between devices? If you plan to use it you must understand its limitations.
Variations over temperature will probably be quite large.

From the data sheet:

Running the RTCC from LPRC will result in a loss of
accuracy in the RTCC, of approximately 5 to 10%. If a
more accurate RTCC is required, it must be run from
the SOSC clock source.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 26, 2019 10:21 am     Reply with quote

Ah... yes I did see the +/- 20% but must have missed the part about
temperature.

I at least have the RTC syncing over network every 6 hours for now...
And the accuracy of the RTC is not TOO critical for me.

I will need to live with this for now I guess.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 26, 2019 10:47 am     Reply with quote

2 options..

there's SW RTC in the code library, works for any PIC, any speed.....zero drift...
or use a HW RTC
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 26, 2019 10:53 am     Reply with quote

Jay, I assume you are talking about this one?
http://www.ccsinfo.com/forum/viewtopic.php?t=26177

That looks useful. I will keep that in my back pocket.
I think that I will implement this SW realtime clock later.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 26, 2019 11:36 am     Reply with quote

yes, that's it...
I've used it a few times(no pun) and it worked fine. I now use a DS3231 RTC though as it offloads the timekeeping from the PIC, and is battery backed.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 26, 2019 12:47 pm     Reply with quote

Well that was easy.

Nice little piece of code there...

Note to self: get a real crystal next time I use the RTC.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 26, 2019 7:50 pm     Reply with quote

For ALL accurate timings you need a crystal ! This includes UART style communications, ultra precise delays and of course RTC use.
While some 'RC' oscillators will work OK at room temperatures, most go 'funny' when it gets kinda hot or chilly. 30 years ago I had to field replace 250-300 remote units as the RC clock didn't like the cold. Upgrade required xtal, caps and 3 4000 CMOS chips. Bang on dead accurate after that, and no 2AM phone calls !
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Feb 27, 2019 1:23 am     Reply with quote

First question I have to ask, is why you want to use the LPRC?.

The RTCC on your chip draws exactly the same current when running off
the SOSC, as when using the LPRC. So why?.

On other threads about this, people have suggested calling this mode
URTCC for 'UnReal Time Clock Counter', since the accuracy will be so bad.
The RTCC is really meant to have a crystal. and on many chips the LPRC
can't be connected to this module. It just happens that the connection
is available on this chip but it really is 'accidental', rather than something
that is meant to be used. Sad

It is strange, that given the calibration effort that they put into the
main internal oscillator, they don't make the LPRC rather more accurate,
but they key is that it is really designed for the WDT, and a 20% error
on this is not seen as that important. Given the wide voltage range
supported by the LPRC, it is not really practical to try to get this more
accurate. The cost of trying to do so, would be uneconomic.
At heart, if you want to use the RTCC, spend a few cents and add a
crystal. Otherwise I can see you forever cursing when timings do not
work as expected.....
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 27, 2019 7:44 am     Reply with quote

I'm afraid the reason is pure simple ignorance. I'm just learning as I go :p
Luckily, it's a prototype and nothing is set in stone for me yet.

For now, the software RTC that's in the code library works alright.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 27, 2019 9:09 am     Reply with quote

There's 2 details about any SW RTC...
1) you need battery backup to keep it working.
Problem can be the PIC and stuff needs power, far more than just timekeeping.
2) you need SW to keep it running 'on time'. If the PIC corrupts the RTC registers...oopsy

For $2 CDN you can get an RTC/EEPROM module that's self battery backed, I2C interface. Might be an option ??
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 27, 2019 1:01 pm     Reply with quote

I'm gonna superglue a crystal to my PCB and solder some wires from the crystal pads onto my micro. I will try and keep the wires short.

We will see how it goes after this :D

I haven't got much to lose anyway at this point.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 27, 2019 1:40 pm     Reply with quote

Playing with PICs is more fun than shoveling that white stuff we're getting !
Though I've got an 1822 here, 1Hz LED and it goes way too fast...
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 27, 2019 1:47 pm     Reply with quote

temtronic wrote:
playin with PICs is more fun than shovelling that white stuff we're getting !
though I've got an 1822 here, 1hz LED and it goes way too fast...


What's the point of shoveling when it's still coming down by the bucketload eh?

The stuff is just piling outside my work. Just gonna wait til tomorrow when the construction company next door will just take care of it all with their bobcat.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Feb 28, 2019 2:00 am     Reply with quote

temtronic wrote:
There's 2 details about any SW RTC...
1) you need battery backup to keep it working.
Problem can be the PIC and stuff needs power, far more than just timekeeping.
2) you need SW to keep it running 'on time'. If the PIC corrupts the RTC registers...oopsy

For $2 CDN you can get an RTC/EEPROM module that's self battery backed, I2C interface. Might be an option ??


If he wants really good time, he doesn't just need an XTAL... but one with a TXCO at a minimum.

And they're out there.. I know Maxim/Dallas has some fun TCXOs for 32.768KHz operation that weren't terribly pricey.

They needed a battery and then just plugged into the PIC someplace. They had I2C for comms (for control/status).

Just a thought...

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group