AnsweredAssumed Answered

timer delay deviation with stm32f051

Question asked by Evert on Jan 24, 2013
Latest reply on Jan 25, 2013 by Evert

I'am struggeling with a timer delay function,
I've made a delay_us() function with help of timer14
However, when I have a delay which is close to zero I have lots of deviation.
Here is my initialisation:

#define SYS_CLK 48000000    /* in this example we use SPEED_HIGH = 48 MHz */
#define DELAY_TIM_FREQUENCY 1000000 /* = 1MHZ -> timer runs in microseconds */

   /* Time base configuration */
   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
   TIM_TimeBaseStructure.TIM_Prescaler = (SYS_CLK / DELAY_TIM_FREQUENCY) - 1;
   TIM_TimeBaseStructure.TIM_Period = UINT16_MAX;
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);

  /* Enable counter */
   TIM_Cmd(TIM14, ENABLE);

This is my delay function:
void delay_us(uint16_t us) {   
  uint16_t start = TIM14->CNT;
   /* use 16 bit count wrap around */
   while((uint16_t)(TIM14->CNT - start) <= us);

These are the deviations measured on an output which is switched before and after the delay:

delay_us(1) -> actual delay = 2,55 us
delay_us(10) -> actual delay = 11,24 us
delay_us(100)-> actual delay = 100,08 us

I've measured the all clock signals on the MCO port and they are what I expected, also the PLL

Does anyone has an idea what is going on?


Evert Huijben