cancel
Showing results for 
Search instead for 
Did you mean: 

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

Mani.V
Associate II

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.

4 REPLIES 4
KnarfB
Principal III

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.

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.

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

I guess this helps me lil bit as now on i can rotate motor without skipping any step and smoothly. The point is when i was changing the duty cycle of timer i missed a part in reference manual where it describes that the PWM pulse value should be equal to the period value, above that value will be count as 100% duty which causing issue in my case. Moreover there is more thing that effects is the combination of +2 -2 and -1 +1 will be in sequence always, as i was missed that part the motor moves but skip steps. hence remembering these points i solved my issue. Thanks for you help that SINE look up table helped me alot.