Precision of Software Timers

danielrimaa wrote on Saturday, April 13, 2019:

Hi,
I’m developing a system basen on FreeRTOS and schedulling periodic task with software timers.
I have a tick off 100Hz generated with a 8MHz on the LIRC.
The timers have a period of 30s and a total count of 15 mins for resume a task, but every 15 minutes it delays 4 more seconds mor or less.
How could I increase the precision of the timer?

rtel wrote on Saturday, April 13, 2019:

Four seconds in fifteen minutes would be a 0.4% inaccuracy.

How accurate is the LIRC? You have not said which chip you are using or
what the LIRC is - but some low power clocks or clocks generated
internally in the MCU rather than using a precision crystal are
comparatively inaccurate.

Have you measured the tick frequency with a scope or frequency meter to
see how close to 100Hz it is?

danielrimaa wrote on Saturday, April 13, 2019:

Hi, thank you for your reply
The MCU It’s the MKL43Z256VLH4 of NXP, In the data sheet saids that the IRLC has a 3% acuracy. The tick has a frecuency of 99.717 measured with a scope.

rtel wrote on Saturday, April 13, 2019:

In which case it looks like you are actually exceeding the accuracy
requirements of the chip ;o)

danielrimaa wrote on Saturday, April 13, 2019:

danielrimaa wrote on Saturday, April 13, 2019:

It’s seems,
so do you think to reach the acuracy I need trough the 15 minutes, an RTC based on a 32.768kHz crystal is needed?

rtel wrote on Saturday, April 13, 2019:

I don’t think the resolution of such a slow crystal would give you
exactly the tick frequency you need. If you need high accuracy, you
will need a more expensive crystal.

richarddamon wrote on Saturday, April 13, 2019:

just to be clear, it sounds like you are saying that you are geneating a 15 minute delay by counting 30 30 second delays, and are finding the timing to be off by 4 second in 15 minutes (a 0.4% error) but your timer is specifiec at only 3%.

A 32,768 Hz crystal would have much better accuracy, but being an ‘odd’ frequency, you likely would want to set the dividers to generate something like a 128 Hz tick (and only do conversions using the tick-rate-hz value and not ms per tick which will be rounded.

If you dropped the tick rate to 64 Hz, then you could ask for a 900 second delay as 57,600 ticks which is within a 16 bit number for ticks,

heinbali01 wrote on Sunday, April 14, 2019:

KL43 : the reference says that the internal 48MHz reference clock has a precision of 0.5%, which is better than the 3 % you have now. I think it is possible to connect an external crystal to get a better precision.

The precision of crystals is expressed in PPM ( Parts Per Million ). A PPM of 10 or lower is good, but that may still deviate up to 5 minutes per year. The difficulty is that the actual frequency depends on the temperature.

I sometimes use an external 32,768 Hz crystal just to feed an RTC, which keeps track of date and time. A frequency of 32768 is odd indeed, you can not create a 100 or a 1000 Hz clock.
For an RTC, it will normally be divided by 32768 to get a 1 Hz clock: a second counter.