cancel
Showing results for 
Search instead for 
Did you mean: 

How to count PWM pulse with timer on the STM32F407VG?

JTurp
Associate II

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 ?

8 REPLIES 8

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

S.Ma
Principal

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...

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.

What does acceleration/deceleration mean? Does it involve change of the PWM period?

JW

MNapi
Senior II

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;

   }

}

Yes, for acceleration/deceleration I'll change the PWM period

JTurp
Associate II

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);
			}
 
	}
		
}

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