Skip to main content
Mani.V
Associate II
February 20, 2021
Question

Controlling stepper motor (4 Wire), using PWM of STM32f7 series controller.

  • February 20, 2021
  • 2 replies
  • 4615 views

Hello everyone,

I am currently working over stepper motor with STM32F7 series controller and i'm trying to control the stepper using PWM timers. Somehow there is no such support for the same as all i found is controlling servo motor or some other related searches. Main issue is i already created PWM and i can see it over Pins using oscilloscope but when i connect my motor it ain't move a bit, not even vibration which shows that nothing is happening through PWM. I would like to know about how to control motor using PWM. If anyone can help me from scratch that would be a great help.

2 replies

KnarfB
Super User
February 20, 2021

Supposed you have a proper motor driver, I would start generating pulses by bit-banging in software, using the same pins in GPIO output mode.

Mani.V
Mani.VAuthor
Associate II
February 22, 2021

Yes, i have a motor driver IC, and i did rotated motor by bit banging, using the same pins in GPIO output mode. but now i want to control it with PWM using timers. so all i need is to know the concept, as the all i understood by reading web searches and related documents is that i've to change its duty cycle to create a sine-wave, but i don't understand how it will make it run.

KnarfB
Super User
February 22, 2021

As I don't know the type of motor and driver chip you are using, here are some general remarks: There is an app note "AN2820 Driving bipolar stepper motors..." explaining different types of stepper motors. There is also an accompanying firmware, but I don't know how uselful that is for you.

> that i've to change its duty cycle to create a sine-wave 

The basic code pattern is:

/* USER CODE BEGIN 2 */
#define N 1000
 static uint16_t pwm[N];
 for( int i=0; i<N; ++i ) {
 pwm[i] = 500 + 490*sin( (2*3.14159 * i)/N ); // one sine period
 }
 HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_2, (uint32_t*)pwm, N);
 /* USER CODE END 2 */

(Any other pattern you need for the pwm will do as well.)

Configure TIM2 Channel 2 in PWM mode and set Trigger Event to OC2REF. Enable TIM2CH2 circular DMA with half-word -> word transfers from mem to device.

If you need to specify each edge independently, set Timer Channel to OC Toggle mode, use HAL_TIM_OC_Start_DMA and an array with monotonically increasing timestamps for the edges.

hth

KnarfB

Visitor II
June 23, 2024

Hello sir, good morning. I'm doing a project in stepper motor driver code. I have some doubts in this. Could you please send me your email ID sir. My email ID is jagadeeshbalaji21@gmail.com. my WhatsApp number is 8903617327. Thanks :folded_hands: