Problem with PWM

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
oattao
Posts: 9
Joined: Mon Dec 03, 2012 5:04 am

Problem with PWM

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Problem with PWM

Post 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
oattao
Posts: 9
Joined: Mon Dec 03, 2012 5:04 am

Re: Problem with PWM

Post by oattao »

Giovanni, thank you very much. I can solve problem now. :D
Post Reply