PWM on ServoMotor (STM32F3DISCOVERY)

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
meyelo2462
Posts: 1
Joined: Wed Dec 18, 2019 5:27 pm

PWM on ServoMotor (STM32F3DISCOVERY)

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

Re: PWM on ServoMotor (STM32F3DISCOVERY)

Post by Giovanni »

Hi,

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

Giovanni
Post Reply