Timers accuracy

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
MySOL
Posts: 8
Joined: Wed Mar 04, 2015 10:40 pm

Timers accuracy

Post by MySOL »

Hello! I never checked the accuracy of the "Sleeps" functions, but today there was one problem , when I wrote PWM programm

Code: Select all

Confgi funcion is:

static PWMConfig pwmcfg = {
  10000,                                    /* 10kHz PWM clock frequency.   */
  1000,                                    /* Initial PWM period 100 ms.       */
  pwmpcb,
  {
    {PWM_OUTPUT_DISABLED, NULL},
    {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_ACTIVE_LOW, pwmc1cb},            // 3nd channel // PA1
   {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
  0
};

And "Enable function" is

pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
chThdSleepMilliseconds(2000);
pwmDisableChannel(&PWMD2,2);


The main problem is that in my understand "chThdSleepMilliseconds(2000);" - its a 2 seconds delay, but in real it was around 1.2 second. Hence the question: its mistake in my code or understanding or poor accuracy in this function? Thx
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Timers accuracy

Post by Giovanni »

That function is as accurate as the system clock is. If you try any provided demo you can see it is accurate. There must be a problem in your setup.

Giovanni
MySOL
Posts: 8
Joined: Wed Mar 04, 2015 10:40 pm

Re: Timers accuracy

Post by MySOL »

Giovanni wrote:That function is as accurate as the system clock is. If you try any provided demo you can see it is accurate. There must be a problem in your setup.

Giovanni


Thank you!
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Timers accuracy

Post by Giovanni »

A possible reason could be your board having a different crystal frequency from the board used by the example you used as source.

Are you using some kind of custom board? if so some parameters in mcuconf.h should be adjusted depending of the crystal frequency.

Giovanni
Post Reply