Based on STM32CUBEMX driving low-voltage stepper motor driver STSPIN220 (3) ---- timer interrupt generates a specified number of pulses

Based on STM32CUBEMX driving low-voltage stepper motor driver STSPIN220----3. Timer interrupt generates a specified number of pulses

overview

In the stepper motor control process, in order to achieve precise position and speed control, it is often necessary to output a specified number of pulses. This needs to use the timer function to generate the PWM pulse signal. This article will introduce in detail how to use STM32CUBEMX to configure the timer to output a specified number of PWM pulses.
The timer is an important functional module of the STM32 microcontroller, which can be used to generate various timing and counting operations. By configuring the parameters and modes of the timer reasonably, we can achieve precise pulse output.

I am currently taking ST courses, and those who need samples can join the group application: 615061293.
insert image description here

sample application

https://www.wjx.top/vm/PpC1kRR.aspx

video teaching

https://www.bilibili.com/video/BV1k94y1C7Gw/

Based on STM32CUBEMX driving low-voltage stepper motor driver STSPIN220 (2) ---- timer interrupt generates a specified number of pulses

STM32CUBEMX configuration

A relatively simple way is to use a timer interrupt to generate a fixed number of pulses. In this method, we can configure the timer as PWM output mode and perform counting operation in the interrupt of PWM output. When the count reaches the set number of pulses, we can stop the PWM output to achieve precise control. The following takes channel 4 of timer 1 as an example to introduce the specific steps:
In STM32CUBEMX, select timer 1 and configure it as PWM output mode. Make sure the correct timer channel (channel 4) is selected.
insert image description here

Configure the clock source and prescaler factor of Timer 1. According to the requirements of the application and the system clock frequency, select the appropriate clock source and prescaler factor to obtain the required pulse frequency. Configure the clock source and prescaler factor for Timer 1 to values ​​appropriate for your application.
The PWM frequency calculation is shown below.
insert image description here

In the above configuration, the prescale factor of Timer 1 is set to 48-1, and the auto-reload value is set to 1000-1. From these configurations, the frequency of the PWM can be calculated as 48,000,000 / ((48-1+1) * (1000-1+1)) = 1000Hz, which is 1kHz.
In timers, the "pulse" of a channel refers to a characteristic of the signal output by the timer. Each timer channel can be used to generate a pulse signal, and "pulse" usually refers to the duration of a single pulse. In this setup, we configure the pulse to have a duty cycle of 50%, so the setting is 500-1.
insert image description here
We need to trigger a callback function when the PWM pulse is complete. The HAL_TIM_PWM_PulseFinishedCallback function is a callback function used to handle PWM pulse completion in non-blocking mode.

insert image description here

In order to trigger the HAL_TIM_PWM_PulseFinishedCallback callback function, the Capture Compare Interrupt interrupt needs to be enabled.

insert image description here

Generate a fixed number of PWM

First, you can define a global variable, which is used to control the number of output pulses. By manipulating this variable, we can flexibly control the required number of pulses in the program.

/* USER CODE BEGIN 0 */
void MX_GPIO_Init_mode3(void);
uint16_t STSPIN220_PwmNum;
uint8_t	STSPIN220_flag=0;//电机完成步数标志位
uint8_t	STSPIN220_Dir_flag=0;//方向
/* USER CODE END 0 */

First, initialize Timer 1 using MX_TIM1_Init().
Next, when output pulses are required, assign the required number of pulses to the variable STSPIN220_PwmNum.
Finally, use HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_4) to start the PWM interrupt output of timer 1.

STSPIN220_PwmNum = 20;
MX_TIM1_Init();
HAL_TIM_PWM_Start_IT(&htim1,TIM_CHANNEL_4);

The implementation part of the HAL_TIM_PWM_PulseFinishedCallback callback function.
Inside the function, if the value of STSPIN220_PwmNum is 0, that is, the required number of pulses has been output, then stop the PWM interrupt output of timer 1 by calling HAL_TIM_PWM_Stop_IT(&htim1, TIM_CHANNEL_4) to achieve the purpose of stopping the pulse output.
Secondly, the global variable STSPIN220_PwmNum is decremented, indicating that the output of one pulse is decremented by 1. Next, check if the specified number of pulses have been output by conditionally judging if (STSPIN220_PwmNum == 0).
The purpose of this code is to update the value of the global variable STSPIN220_PwmNum each time the PWM waveform cycle is completed, and stop the PWM interrupt output after reaching the specified number of pulses. In this way, the function of precisely controlling the number of output pulses can be realized.

/* USER CODE BEGIN 4 */
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
    
    
	 if(STSPIN220_PwmNum==0)
	 {
    
    
			HAL_TIM_PWM_Stop_IT(&htim1,TIM_CHANNEL_4);
			STSPIN220_flag=1;//电机完成步数标志位
	 }
	STSPIN220_PwmNum--;
}

/* USER CODE END 4 */

The resulting waveform is shown below, and you can see that there are 20 pulses.
insert image description here

motor settings

The motor used here has a step angle of 18° and a reduction ratio of 1:30. In this case, if the step angle of the stepper motor is 18° and the reduction ratio is 1:30, then the required The number of pulses can be calculated as:
pulses required for one rotation = (360 / step angle) * reduction ratio * subdivision
where the step angle is in degrees, and the reduction ratio is relative to the output shaft of the motor and the rotation in the actual application The ratio between the axes, the number of subdivisions indicates how many microsteps the stepper motor driver divides a step angle into.
Calculate the required number of pulses according to this formula, so as to realize the control of one revolution.

insert image description here

STSPIN220 initialization

The initialization of STSPIN220 after modification is shown below.

  /* USER CODE BEGIN 2 */
	MX_GPIO_Init_mode3();
	HAL_Delay(100);

	STSPIN220_SetStepMode(0);//mode1-mode4都关闭
	STSPIN220_enable(0);//使能操作 1使能0失能
	STSPIN220_Stby(1);//低功耗模式 1开启低功耗0关闭低功耗
	HAL_Delay(100);

	STSPIN220_SetStepMode(2);//细分操作
	STSPIN220_Stby(0);//低功耗模式 1开启低功耗0关闭低功耗,加载mode
	HAL_Delay(100);//等待电平稳定
	STSPIN220_setDirection(0);//0反1正
	HAL_Delay(100);//等待电平稳定
	STSPIN220_enable(1);//使能操作 1使能0失能
	HAL_Delay(100);

	STSPIN220_PwmNum = 600*2;//步进角为18°,1:30的减速比,细分2则需要走(360/18)*30*2为一圈	

	MX_TIM1_Init();
//	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
	HAL_TIM_PWM_Start_IT(&htim1,TIM_CHANNEL_4);
  /* USER CODE END 2 */

main program

The code to realize the motor cycle forward rotation 1 circle and reverse rotation 1 circle is as follows.

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
		if(STSPIN220_flag)
		{
    
    
				HAL_Delay(1000);
//				STSPIN220_Stby(1);//低功耗模式 1开启低功耗0关闭低功耗
				STSPIN220_flag=0;		
			if(STSPIN220_Dir_flag==0)
			{
    
    
				STSPIN220_Dir_flag=1;
				STSPIN220_setDirection(1);//0反1正
				HAL_Delay(100);
				STSPIN220_PwmNum = 600*2;//步进角为18°,1:30的减速比,细分2则需要走(360/18)*30*2为一圈	
				HAL_TIM_PWM_Start_IT(&htim1,TIM_CHANNEL_4);
			}
			else
			{
    
    
                
				STSPIN220_Dir_flag=0;
				STSPIN220_setDirection(0);//0反1正
				HAL_Delay(100);
				STSPIN220_PwmNum = 600*2;//步进角为18°,1:30的减速比,细分2则需要走(360/18)*30*2为一圈	
				HAL_TIM_PWM_Start_IT(&htim1,TIM_CHANNEL_4);
			}
		}
		HAL_Delay(10);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132157295