cancel
Showing results for 
Search instead for 
Did you mean: 

HI, i am new to stm32, i am working on the project stepper motor.

Lchal.1
Associate II

i am confused how to write a code to drive the stepper motor selecting the 2 timers with interrupt method. By setting one timer = 100khz, and another timer = 3khz. i need to display one pulse after 33 counts each on my oscilloscope. can you please help me with it. I have displayed some code below. can you please say me the mistakes i have done and what i need to add to it to work. 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if (htim->Instance == htim2.Instance)           //100KHZ

{

pulse_count =__HAL_TIM_GET_COUNTER(&htim2);

if(pulse_count == 33)

    {

HAL_GPIO_WritePin(GPIOA, dir_Pin, GPIO_PIN_SET);             //pin PA3

 for(int i=1; i<=300; i++)

 {

 HAL_GPIO_WritePin(GPIOA, step_Pin, GPIO_PIN_SET);         //pin PA4

 HAL_Delay(0.1);

 HAL_GPIO_WritePin(GPIOA, step_Pin, GPIO_PIN_RESET);        //pin PA4

 HAL_Delay(0.1);

 }

  //HAL_Delay(500);

  HAL_GPIO_WritePin(GPIOA, dir_Pin, GPIO_PIN_RESET);    //  anticlockwise

        for(int i=1; i<=300; i++)

        {

     HAL_GPIO_WritePin(GPIOA, step_Pin, GPIO_PIN_SET);        //pin PA4

     HAL_Delay(0.1);

     HAL_GPIO_WritePin(GPIOA, step_Pin, GPIO_PIN_RESET);        //pin PA4

     HAL_Delay(0.1);

        }

    pulse_count=0;

  }

  pulse_count++;

  }

}

Thank you

4 REPLIES 4
Nikita91
Lead II

I don't read your code thoroughly, but I have some remarks:

HAL_TIM_PeriodElapsed() is called from ISR, so it have to be very short.

Waiting should not be used in an ISR: do not use HAL_Delay() in ISR.

HAL_Delay() argument is milli-seconds expressed as uint32_t. If you use HAL_Delay(0.1) I think the delay will be 0.

You don't say what doesn't work...

Ala
Senior

Nikita91 is right!

HAL_Delay() is not precise for arguments less than 1, use the timer of a for loop, though as Nikita91 says, it is better not to use delays in ISR. another thing is that, as I can recall, for stepper motor you should turn on and off four coils, I only see one pin in your code, and yes, what doesn't work?

i am not able to drive my motor . if i debug and run my code i am not able to drive my motor. can you say me the reason why the motor is not rotating please.

Thank you

i am not able to drive my motor . if i debug and run my code i am not able to drive my motor. can you say me the reason why the motor is not rotating please. As you said i should use four coils to turn on and off but i didn't understand what you mentioned can you say me in detail please.

Thank you