Doubt about PWMConfig Structure.

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Doubt about PWMConfig Structure.

Post by sapan »

Hi,

I am using example code for STM32F4 Discovery to generate PWM. It is defined as follow

static PWMConfig pwmcfg = {
10000, /* 10KHz PWM clock frequency. */
10000, /* PWM period 1S (in ticks). */
pwmpcb,
{
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL}
},
/* HW dependent part.*/
0
};

Here first two values are PWM frequency and period, third values is to initiate ADC conversion. Last value is HW dependent part which to be kept at zero. I am not clear about definition of fourth values and what it relates to and why it has four parameters ?? Please explain.

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

Re: Doubt about PWMConfig Structure.

Post by Giovanni »

Hi,

In the STM32 each PWM unit has four PWM channels, those constants define the mode of the four channels:

Channels 1 and 4 are active high, channels 2 and 3 are disabled, the NULL are the pointers to callbacks associated to each channel, in this case no callbacks are specified for channels.

Giovanni
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: Doubt about PWMConfig Structure.

Post by sapan »

Got it now...Thanks...:)
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: Doubt about PWMConfig Structure.

Post by sapan »

Whats the difference between PWM clock frequency which is 10khz in this case and pwm period??
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: Doubt about PWMConfig Structure.

Post by sapan »

PWM period is period in seconds...here 10000 = 1s, 1000 = 100 ms and so on...correct?
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Doubt about PWMConfig Structure.

Post by barthess »

PWM clock frequency is clock frequency to the internal PWM counter. PWM period is value in that counter. So, 10000 counts on 10kHz is exactly 1 second.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: Doubt about PWMConfig Structure.

Post by sapan »

Got it...was just playing around with those two values and checking the results on oscilloscope.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: Doubt about PWMConfig Structure.

Post by sapan »

Hi,

I want to use TIM8_CH2N on PB14 and TIM8_CH3N on PB15 to generate PWM outputs on these pins. Should I follow same procedure as given in sample code or do I need to do something different?

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

Re: Doubt about PWMConfig Structure.

Post by Giovanni »

It is the same, just take care to program the associated I/O pins correctly.

Giovanni
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: Doubt about PWMConfig Structure.

Post by sapan »

I have made following setting to use PB14 and PB15 via TIM8_CH2N and TIM8_CH3N on discovery board.

Code: Select all

/*pwmcfg8 - to be used for PWM8..i.e timer 8 */
static PWMConfig pwmcfg8 = {
  1000000,                                    /* 1MHz PWM clock frequency.   */
  20000,                                    /* 20 ms time period*/

  NULL,
  {                                 /*CH2N and CH3N of Timer 8 are available for PWM. Enable those */
                                                                  /*   disable rest*/
    {PWM_OUTPUT_DISABLED, NULL},
    {PWM_OUTPUT_ACTIVE_HIGH, NULL},
    {PWM_OUTPUT_ACTIVE_HIGH, NULL},
    {PWM_OUTPUT_DISABLED, NULL}
  },
  /* HW dependent part.*/
  0
};



  pwmStart(&PWMD8, &pwmcfg8);
  palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(3));
  palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(3));

  /*This will generate pulse of 5ms on PB14 and 10ms on PB15 */
  pwmEnableChannelI(&PWMD8, 1, PWM_FRACTION_TO_WIDTH(&PWMD8, 6000, 1500)); /*PB14 -> CH2N */
  pwmEnableChannelI(&PWMD8, 2, PWM_FRACTION_TO_WIDTH(&PWMD8, 6000, 3000)); /*PB15 -> CH3N */




I am not getting PWM on PB14 and 15. Do I need to do anything different or have I made mistake somewhere?
Post Reply