I am a beginner in stm32 and new in this community I am trying this following program :
-run a DC motor (using PWM command) with speed "1" and wait 5 seconds then run with speed "2" and wait for 5 seconds then the motor stops .
The problem is that the motor stays in a loop : starts turning about 1 second and stops .
#include "main.h"
TIM_HandleTypeDef htim3;
int puls ;
float duty ;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM3_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
while (1)
{puls=150 ; // motor with speed 1
duty =(puls*100)/31999;
MX_TIM3_Init();
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
HAL_Delay(5000);
puls=300 ; //motor with speed 2
duty =(puls*100)/31999;
MX_TIM3_Init();
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
HAL_Delay(5000);
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
}
}
static void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 31999;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = puls;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
}
I am using stm32f4.
motor pin C7 .
I've configured my project with STM32CubeIDE .
Thanks for your help.
dutyorpulsor make any attempt to set a duty cycle at all. Moreover you assigndutywith an integer expression which in both cases will result in zero. In both cases the values could be a compile time constant. If the motor moves at all, it is not because this code is in any meaningful way controlling it.dutyor the value ofpuls. A PWM signal is characterised by frequency and duty cycle, it is anybody's guess here what your intent is with respect to either.HAL_TIM_PWM_ConfigChannel()is you want to set the duty cycle. It is a shame you have chosen to to use the HAL interface, it suck and does very little for you. It is by no means a "PWM command", it is simply an abstracted interface to the PWM generation hardware - the abstraction is only enough to make all STM32 PWM capable timers look more-or-less alike - you are supposed to build an API on top of that.