Skip to main content
kkhli.1
Associate III
December 18, 2023
Solved

Motor Pilot

  • December 18, 2023
  • 3 replies
  • 2640 views

I have an issue when using both PWMs generated by timers TIM2 and TIM3 to control two motors simultaneously. Specifically, when I call the function DC_MOTOR_Init(0), it activates PWM1, and when I call DC_MOTOR_Init(1), it activates PWM2. However, if I call both DC_MOTOR_Init(0) and DC_MOTOR_Init(1) simultaneously, they don't seem to work together. When I call either function alone, it works correctly, but when I attempt to call them simultaneously, there is an issue. Despite the fact that the two timers do not share common resources..

 

#include "main.h"
#include "DC_MOTOR.h"

void SystemClock_Config(void);
static void MX_GPIO_Init(void);


int main(void)
{

HAL_Init();

SystemClock_Config();
MX_GPIO_Init();


DC_MOTOR_Init(1);

DC_MOTOR_Init(0);

/
while (1)
{

}

This topic has been closed for replies.
Best answer by kkhli.1

thank you it works correctly now

3 replies

ST Employee
December 19, 2023

Hello @kkhli.1

I cannot see what could have caused the conflict 

DC MOTOR 1 uses TIM2_CH1 to generate the PWM signal

DC MOTOR 2 uses TIM3_CH1 to generate the PWM signal

the code you provided does not cover the GPIO configuration though, but I suppose you're using different GPIOs to control the motors? 

Meanwhile, I will look more in-depth 

kkhli.1
kkhli.1Author
Associate III
December 19, 2023

the GPIO configuration  is covered by the file 

ST Employee
December 19, 2023

Hello,

please try to null htim variable in function DC_Motor_Init() before htim variable will be used:

TIM_HandleTypeDef htim = {0};

It should works correctly.

kkhli.1
kkhli.1AuthorBest answer
Associate III
December 19, 2023

thank you it works correctly now