Page 1 of 1

Problem with PWM

Posted: Tue Dec 04, 2012 3:36 am
by oattao
I have a STM32L-Discovery kit. I want to control the brightness of Led4 (connect with pin PB6). I used this code:

Code: Select all

#include "ch.h"
#include "hal.h"

static PWMConfig pwmcfg = {
  10000,                                    /* 10kHz PWM clock frequency.   */
  10000,                                    /* Initial PWM period 1S.       */
  NULL,
  {
   {PWM_OUTPUT_ACTIVE_HIGH, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
};

int main(void) {

  halInit();
  chSysInit();
   
  pwmStart(&PWMD2, &pwmcfg);
  palSetPadMode(GPIOB, 6, PAL_STM32_MODE_OUTPUT);
  chThdSleepMilliseconds(2000);

  //  PWM channel 0 to 50% duty cycle.
  pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
  while (TRUE) {
  }
}

But it doesn't work. Led4 is off. What's wrong with my code?
Please help me. Thanks in advance.

Re: Problem with PWM

Posted: Tue Dec 04, 2012 8:53 am
by Giovanni
Hi,

The problem is that you programmed the pin as output instead of alternate(n), look into the DISCOVERY-MEMS demo, there is an example about setting up LED pins as PWM outputs.

Giovanni

Re: Problem with PWM

Posted: Tue Dec 04, 2012 3:39 pm
by oattao
Giovanni, thank you very much. I can solve problem now. :D