Page 1 of 1

PWM on ServoMotor (STM32F3DISCOVERY)

Posted: Wed Dec 18, 2019 5:33 pm
by meyelo2462
Hello,
I'm trying to use a servo motor on the STM32F3DISCOVERY board. I read on the web that it must be controlled by PWM. My problem is that I tried to use it but without success. Below you can find the code that I have used. Can you help me with that?

Code: Select all

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

static PWMConfig pwmcfg = {
   200000,
   1000,
   NULL,
   {
      {PWM_OUTPUT_ACTIVE_HIGH, NULL},
      {PWM_OUTPUT_ACTIVE_HIGH, NULL},
      {PWM_OUTPUT_ACTIVE_HIGH, NULL},
      {PWM_OUTPUT_ACTIVE_HIGH, NULL}
   },
   0,
   0
};

int main(void) {
   halInit();
   chSysInit();

   palSetPadMode(GPIOE, 6, PAL_MODE_ALTERNATE(2));

   pwmStart(&PWMD1, &pwmcfg);

   while (1) {
      pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 1000));   // 10% duty cycle
      chThdSleepMilliseconds(2000);
      pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 9000));   // 90% duty cycle
      chThdSleepMilliseconds(2000);
   }

   return 0;
}

Re: PWM on ServoMotor (STM32F3DISCOVERY)

Posted: Wed Dec 18, 2019 6:00 pm
by Giovanni
Hi,

Probably you are using the wrong GPIO for that timer, check on the data sheet alternate table.

Giovanni