This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to Calibrate Timer?

I use NRF52832 and sdk13.0.0 to develop my own application. I want to generate 1000 Hz Timer to trigger the ADC. I use the timer 1 to generate 1000 Hz. But when I use GPIOTE to test the frequency, I find that the frequency is 500.014 Hz(500.014 x 2 = 1000.028 Hz).I have open the crystal oscillator as HFCLK.

Because I need two devices to generate the same frequency to sample the sensor data, so I need the accurate frequency. So at least I need the two device to generate the same frequency. But when I download the same program to two device ,the frequency is different: one is 500.014 Hz ,the other is 500.012Hz. What I should do?

My timer code is:
//========================
//timer initial
//========================
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
err_code = nrf_drv_timer_init(&m_timer1, &timer_cfg, timer_dummy_handler);
APP_ERROR_CHECK(err_code);
/* setup m_timer1 for compare event every 1ms */
uint32_t ticks = nrf_drv_timer_us_to_ticks(&m_timer1, 1000);//ticks = 0x3E80
nrf_drv_timer_extended_compare(&m_timer1,
                               NRF_TIMER_CC_CHANNEL0,
                               ticks,
                               NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                               false);

The question one is: how can I calibrate the timer to generate accurate 1000 Hz? The question two is: how to make sure the two device to generate the same frequency?

  • There is no need nor capability for calibrating the nRF timer driver. The only reason for the frequency to be off is either your 32MHz clock is incorrectly loaded or you failed to request HFEXT or essentially turn on HFEXT and instead it is running on HFINT which is not an accurate clock source. You always need to request HFEXT in your code if you require its accuracy.

  • Hello, thank you for your anwser. I use sd_clock_hfclk_request() function to request the HFEXT, and using the oscilloscope to ensure the HFEXT running. When using the HFINT the frequency is unstable, than the frequency is stable but having offset after calling the sd_clock_hfclk_request() function. So I can ensure the HFEXT is running, but the frequency still have offset. So I want to know how to fix this problem.

  • I would suggest next you verify the accuracy of your 32MHz crystals. You can't probe directly since the probe capacitance will load the crystal. Easiest way is to measure the RF channel accuracy with a spectrum analyzer. Or you can route PClk to GPIO and measure that. Pclk is 16MHz for nRF52 series.

    Some of the reference designs spec 10ppm crystals with 10ppm stability. So at room temp you should see up to 10ppm error if loaded correctly. Functionally you can view this as 1000Hz +/- 0.010Hz

    What is the ppm spec for the crystals you are using?

  • Yes, I using 10 ppm 32mHz crystals. I will try the way of routing PClk to GPIO and check the 32MHz crystals. But how can I route PClk to GPIO, I don't find the way? I read the GPIO register,but not find the way. Do you think the offset of 1000 +/- 0.026 Hz is normal ? Because the two user board almostly have the same offset. Maybe the 32mHz is not accuracy or the 12pF capacitance is not accuracy.

  • By the way I should point out that you need to consider the accuracy of the equipment you are using to measure the 1000Hz signal. Most O-scopes have terrible clocks and 100ppm is not unheard of. Whereas my Spectrum Analyzer has a 2.5ppm clock since that is what I use most.

    Test labs often use a GPS based 10 or 13MHz reference. These have an accuracy of 0.00001 PPM. Yes that is a 1 with 4 zeros in front of it. Very, very accurate.

    I think what you'll find is that between your nominal 32MHz offset and your test equipment everything is pretty normal. Likely you just have to measure each board and adjust the register settings to get them close to each other.

    Also you might want to come up with load caps for the xtal that are better suited to your board.

Related