halconf and mcuconf for pwm: #error compile time

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

Moderator: RoccoMarco

User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: halconf and mcuconf for pwm: #error compile time

Post by Giovanni »

It is a good idea to reduce the tick rate.

Giovanni
enrico.dallavia
Posts: 105
Joined: Thu Mar 12, 2015 10:15 am
Has thanked: 2 times

Re: halconf and mcuconf for pwm: #error compile time

Post by enrico.dallavia »

So, i have to change
#define CH_CFG_ST_FREQUENCY 10000
to
#define CH_CFG_ST_FREQUENCY 1000

right?
enrico.dallavia
Posts: 105
Joined: Thu Mar 12, 2015 10:15 am
Has thanked: 2 times

Re: halconf and mcuconf for pwm: #error compile time

Post by enrico.dallavia »

Now that the pwm on TIM2 in working I have a little double about the duty cycle

I need a pwm from 1Hz to 20kHz.
I set the frequency of the timer to 1MHz

Code: Select all

static PWMConfig pwmcfg = {
  1000000,                                    /* 1MHz PWM clock frequency.   */
  50,                                    /* Initial PWM period .       50  is for 20kHz , 1000000 is for 1 Hz*/
  NULL,//no callback
  {
    {PWM_OUTPUT_DISABLED, NULL},//CH 1
    {PWM_OUTPUT_ACTIVE_HIGH, NULL},//CH 2
    {PWM_OUTPUT_DISABLED, NULL},//CH 3
    {PWM_OUTPUT_DISABLED, NULL}//CH 4
  },
  0,
  0
};


with a constant duty cycle of 50%

from the example

Code: Select all

 pwmStart(&PWMD2, &pwmcfg);

 pwmEnableChannel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));//DC 50%


the 5000 parameter should be constant, since the formula is
tick = period * 5000/10000
so with period of 50 I need (I suppose) a result of 25 so the argument must be 5000
with a period of 1000000 I need a result of 500000 so the argument must be 5000
but with a 1Hz I got a duty cycle far shorter than 50%. Could it be the result of 500000 causes an overflow of the results?

but the argument of the width is typedef uint32_t pwmcnt_t; so I don't think it is an overflow
enrico.dallavia
Posts: 105
Joined: Thu Mar 12, 2015 10:15 am
Has thanked: 2 times

Re: halconf and mcuconf for pwm: #error compile time

Post by enrico.dallavia »

I think in my particular case of 50% fixed, I can use:

Code: Select all

pwmStart(&PWMD2, &pwmcfg);

pwmEnableChannel(&PWMD2, 1, pwmcfg.period/2);


it seems to work with an oscilloscope
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: halconf and mcuconf for pwm: #error compile time

Post by Giovanni »

Even if pwmcnt_t is 32 bits, the timer is not necessarily 32 bits.

Giovanni
Post Reply