PWM on ServoMotor (STM32F3DISCOVERY)

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

Moderators: barthess, RoccoMarco

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

PWM on ServoMotor (STM32F3DISCOVERY)

Postby meyelo2462 » Wed Dec 18, 2019 5:33 pm

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: 13125
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 769 times
Been thanked: 653 times
Contact:

Re: PWM on ServoMotor (STM32F3DISCOVERY)

Postby Giovanni » Wed Dec 18, 2019 6:00 pm

Hi,

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

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 6 guests