2019-03-22 03:09 AM
Hi, I'm trying to drive five stepper motors with PWM. But I need to count the pulses from the PWM to command the positions of each stepper motor and control the speed with a trapezoid form. So i was wondering, how to count PWM pulse with timer?
I'm working in a STM32F4 discovery board with a STM32F407VG and i use CubeMx and Keil.
another question : Can I have only one timer that count steps for all motors and turn off the ones that reached their position ?
2019-03-22 04:03 AM
Well, it's you who generate the PWM, so you know the period, don't you? Then, it's enough to measure time and then divide it by period to know the number of PWM pulses. It's enough to have one timer to measure time.
JW
2019-03-22 05:10 AM
If you want to count pulses use channel 1 or 2 of a timer, and try to use your channel as timer clock which will increment each pulse. If you use 32 bit timer, you can count a lot of them by hw...
2019-03-22 05:48 AM
I'm scared that with the acceleration and deceleration phases might lead to small error like missing a step. I was thinking more about something like unslaving the pwm to a timer that will trigger the pulses ( or another way to synchronyse them) with an interruption that update the step count. But I don't really know how to do it.
2019-03-22 01:27 PM
What does acceleration/deceleration mean? Does it involve change of the PWM period?
JW
2019-03-22 03:53 PM
what about setting timer as interrupt
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_Base_Start_IT(&htim2);
and then using
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
UNUSED(htim);
if (htim -> Instance == TIM2)
{
count = count + 1;
}
}
2019-03-25 01:38 AM
Yes, for acceleration/deceleration I'll change the PWM period
2019-03-28 05:34 AM
Thanks labtor, I've done something like that and the stepper is moving from the angle i want. But unfortunately i didn't managed to change the PWM frequency during run time. Any ideas ? Here is my interruption with the acceleration update :
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
UNUSED(htim);
if(htim->Instance == TIM1){
// Count the number of times the ISR has been triggered
ticksPassed++;
if(ticks_to_make >= ticksPassed ){
if(useAcceleration){
// Accelerate
if(numberOfTicksMade < ticksToAccelerate){
current_period -= (uint16_t)accelerationPerTickInteger;
__HAL_TIM_SET_AUTORELOAD(&htim1,current_period);
// activate timer
HAL_TIM_Base_Start_IT(&htim1);
//activating the PWM
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
}
else{
// Decelerate
if(ticksPassed > decelerationTick){
current_period += (uint16_t)accelerationPerTickInteger;
__HAL_TIM_SET_AUTORELOAD(&htim1,current_period);
// activate timer
HAL_TIM_Base_Start_IT(&htim1);
//activating the PWM
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
}
}
}
// When the steps have been made, stop the timer interrupt, unless the PERPETUAL_MOTION is activated
}
else{
stepperStop(&htim1,TIM_CHANNEL_1);
}
}
}
2020-11-23 10:31 PM
Hi ,
i am new to stm32, even i got the same project to rotate the motor using timer interrupt.
i need to set my timer to 100khz in interrupt mode. and my acceleration and deacceleration frequency is 300 hz . so if i divide 100khz by 300 hz i would get 333 . so i need to get one pulse for every 333 interrupts and my motor should drive . if i decrease my interrupt value my speed should increase and motor should run . and same for deacceleration.
can you help me how to deal with this and what modification i need to do in my code as you have idea about this concept so let you guide me please.
The code what i have written is attached below . please help me.
Thank you