Page 1 of 2

Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 12:34 pm
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

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 12:48 pm
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

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 12:51 pm
by sapan
Got it now...Thanks...:)

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 4:29 pm
by sapan
Whats the difference between PWM clock frequency which is 10khz in this case and pwm period??

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 4:31 pm
by sapan
PWM period is period in seconds...here 10000 = 1s, 1000 = 100 ms and so on...correct?

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 4:34 pm
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.

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 4:46 pm
by sapan
Got it...was just playing around with those two values and checking the results on oscilloscope.

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 5:45 pm
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

Re: Doubt about PWMConfig Structure.

Posted: Sun Mar 11, 2012 6:28 pm
by Giovanni
It is the same, just take care to program the associated I/O pins correctly.

Giovanni

Re: Doubt about PWMConfig Structure.

Posted: Mon Mar 12, 2012 8:26 am
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?