Calibrate Systick timer with 32 kHz crystal

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Calibrate Systick timer with 32 kHz crystal

1,238 Views
rawsewage
Contributor III

I am using an LPC1850 running at 180 MHz via the internal oscillator. According to the datasheet, the oscillator has a frequency tolerance range of 1.5%.

I have the system tick timer configured to interrupt at 1 ms intervals. Sometimes I use the interrupts to measure elapsed time. Given the tolerance of the internal oscillator, my time calculations become less accurate as the duration increases.

I also have a 32 kHz crystal that drives the internal RTC. The crystal has a frequency tolerance of 20 ppm, which is much more accurate than the internal system clock. I realize that for longer periods of time, I can just use the RTC to measure elapsed time. However, what about using the 32 kHz crystal to make the system tick timer more accurate??

Here's what I'm thinking:

  • The MCU has a Downcounter register that counts down at a 1024 Hz rate, driven by the 32 kHz oscillator.
  • The MCU has also the systick register that counts down at the same rate as the system clock, 180 MHz in my case.
  • In a 250 ms period, I know that the Downcounter should decrement 256 ticks, while the systick register should decrement 45000000 ticks.
  • With some careful coding to maximize accuracy, I can capture the starting systick register value, wait for the downcounter to drop by 256, and then capture the ending systick register value.
  • Now I know how many systicks have occurred during the 250 ms period, and I can compare it against the expected number of 45000000.
  • Using the difference in ticks, I can configure my system tick timer to interrupt at more accurate 1 ms intervals.

Aside from being an interesting exercise, is this ever done in practice? Is it a terrible idea?

Labels (4)
Tags (2)
3 Replies

1,090 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Rawsewage,

Regarding your question, if you want to calibrate the systick module, I suppose that you'd better use hardware capture solution in order to increase the accuracy.

But the systick module has not output pin, it has not capture function either. In other words, I do not think your solution is feasible. If you use software solution, for example have the systick generate an interrupt, in the ISR, you can read the Timer counter value, but because the ISR latency is not fixed and difficult to estimate, so software solution is not feasible.

I think what you calibrate is the clock source accuracy instead of Timer module itself, you can use the external 32KHz crystal clock to calibrate the internal 12MHz with Timer.

As you know that the Ctimer and SCT modules have hardware capture function, you can use one module for example MCPWM to counts the internal 12MHz which functions as a tick. You can use the CTimer to count the 32KHz crystal clock, which can be a standard clock source. You can use the MCPWM signal as capture signal and connect it to CTimer capture pin, when the rising edge of capture signal is detected, the CTimer counter value is loaded to capture register immediately, in this way, you can calibrate the 12MHz clock with the 32Khz crystal clock. This is the basic idea.

After you calibrate the 12MHz internal clock, you can write the correct value to systick module.

Hope it can help you

BR

XiangJun Rong

1,090 Views
rawsewage
Contributor III

Hi XiangJun Rong,

Thank you for taking the time to respond. Your information is very helpful.  I have a follow-up question for you.

You mention calibrating the 12MHz internal clock. Is there a setting or register in the LPC1850 that allows me to calibrate the internal clock?  I've looked but I don't see anything, perhaps I missed it.  Or are you referring to some type of hardware calibration?

Thanks again.

0 Kudos

1,090 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, rawsewage,

You are right, the 12MHz IRC can not be trimmed by user. I suppose that you can output the 12MHz IRC to an output pin and use a frequency meter to test the actual frequency, this is the direct method.

Hope it can help you

BR

XiangJun Rong

0 Kudos