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

Timer not correct?

Now,I use a 1s timer to calculate the clock. At first,I use the telephone to adjust the time to fit the telephone time. but finally(after a night ) the result runs fastly compared with the telephone. For example,the telephone time is 8:40,but the devices are 8:42 or 8:46. The code is:

#define APP_TIMER_PRESCALER    0
#define CLOCK_MEAS_INTERVAL    APP_TIMER_TICKS(1000,APP_TIMER_PRESCALER)

static void ble_stack_init(void)
{
  SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM,NULL);
}
create the timer:
app_timer_create(&m_clock_timer_id,APP_TIMER_MODE_REPEATED,clock_meas_timeout_handler);
start the timer:
app_timer_start(m_clock_timer_id,CLOCK_MEAS_INTERVALL,NULL);


static void clock_meas_timeout_handler(void *p_context)
{
 UNUSED_PARAMETER(p_context);
 clock_process();//calculate the time
}

The function of clock_process() is dealling with the clock time,but the result as describe above.

  • The maximum drift over 12 hours using xtal with 20ppm should be 12x60x60*20/1000000 ~= 1 second, which is way lower than what you measure. You are sure it is not caused by the conversion, for example round off errors, overflow or that you for example count 61 seconds in a minute instead of 60?

  • Hello,Ole Bauck

    My Function of clock_process() is:

    void clock_timer_process(ClkTime_t *p_time)
    
    {
       p_time -> seconds++;
    
      if(60<= p_time->seconds)
    
      {
        p_time->seconds = 0x00;
        p_time->minutes++;
        if(24<= p_time->hour)
        {
          p_time ->hour = 0x00;
          p_tiime ->day++;
          ....
        }
      }
    
    
    }
    
  • I don't see the minutes conversion.

    I can test it on monday to see if I get the same error.

  • I have tested for 6 hours now and the drift is under 1 second.

    Could you only run the timer code (no BLE or other application code) and see if the timing still is incorrect? Are you using the DK or some custom hardware?

  • Except the clock timer,I have defined other five timers,

    #define APP_TIMER_MAX_TIMERS    (6+BSP_APP_TIMERS_NUMBER+2)
    #define APP_TIMER_OP_QUEUE_SIZE  (6+BSP_TIMERS_NUMBER+2)
    #define APP_TIMER_PRESCALER       4
    

    I have tested the code by using the DK,meanwhile using our custom hardware,the result is the same.

1 2 3