目录
0 参考资料出处
1 实验预期效果
2 硬件学习
2.1 TB6600驱动器
2.1.1 通过拨码开关设定细分与电流
2.1.2 共阴共阳接线法
2.2 开关电源(AC转DC变压器)& 电源线
2.3 42步进电机
2.3.1 基本知识
2.3.2 转速细分控制
2.3.3 满步、半步、微步驱动原理
3 软件配置与硬件接线
3.1 STM32CubeMX的配置
3.1.1 配置定时器TIM2 编辑
3.1.2 配置引脚PA1
3.2 驱动器的接线方式
3.3 细分与电流调节
编辑
4 代码编写
5 最终效果
6 附录
6.1 TB6600驱动器
6.1.1 驱动器功能
6.1.2 接线
6.1.3 常见故障及对策
6.2 42步进电机尺寸图与内部结构
参考博客:
1、利用STM32F103精确控制步进电机_jl_mlh的博客-CSDN博客_stm32控制步进电机;
2、图文介绍步进电机的满步、半步、微步驱动原理-深圳东么川伺服控制有限公司;
3、【STM32】stm32驱动TB6600控制42/57步进电机的案例(TIM中断和PWM实验)_创客协会的阿蛋°的博客-CSDN博客_stm32驱动42步进电机程序;
4、STM32之TIM定时器驱动42步进电机-驱动器型号TB6600_为丽力的博客-CSDN博客_stm32驱动42步进电机;
参考论文:
1、侯艳霞. 混合式步进电机工作原理及其PLC控制;
完成步进电机的正转(不通过串口控制、非可移动供电)。
共阴:
驱动器 —— stm32
DIR- 与 PUL- —— GND
DIR+ —— 方向引脚
PUL+ —— 脉冲引脚共阳:
驱动器 —— stm32
DIR+ 与 PUL+ —— +5V
DIR- —— 方向引脚
PUL- —— 脉冲引脚
由商家提供资料可知:42步进电机的相数为2(A、B两相,每相并联支路数为2,每条支路串联2个线圈)、步距角为1.8°(满步时)、转子齿数为50。
每拍只有一相绕组通电,四拍构成一个循环。上图中,定子的B相通电,磁极2的5个齿与转子齿对齿(如下图4),由分析可知,磁极6也同转子齿对齿,而磁极4和8是齿对槽(如下图2)。旁边的A相绕组的磁极3的5个齿和转子齿有1/4齿距的错位(如下图3),即1.8°(360°/50/4)。
当B相断电A相通电时,磁极3产生N极性,吸合离它最近的S极转子齿,使得转子沿顺时针方向转过1.8°,实现磁极3和转子齿对齿,此时磁极4和转子齿有1/4齿距的错位。
依次类推若继续按四拍的顺序通电,转子就按顺时针方向一步一步地转动,每通电一次(即每来一个脉冲)转子转过1.8°,即称步距角为1.8°,转子转过一圈需要360°/1.8° =200个脉冲。与表相符。
【参数及公式可参阅文章:利用STM32F103精确控制步进电机_jl_mlh的博客-CSDN博客】
① 拍数(N=km)——每一次循环所包含的通电状态数(电机转过一个齿距角所需脉冲数);
单拍制(k=1)——拍数 = 相数;双拍制(k=2)——拍数 = 相数的两倍;② 相数(m)——即电机内部的线圈组数。如果使用细分驱动器,则相数将变得没有意义,只需在驱动器上改变细分数,就可以改变步距角;
③ 转子齿数(Zr);
④ 步距角(θs)——步进机通过一个电脉冲转子转过的角度;
⑤ 转速(n)
电机的转速与脉冲频率成正比,电机转过的角度与脉冲数成正比。所以控制脉冲数和脉冲频率就可以精确调速。
f:脉冲频率;θs:步距角;X:细分值;n:转速(rad/s)。
若已知步距角=1.8°,细分值=32,想要达到1rad/s的转速(每秒转一圈),则脉冲频率=1*32*360/1.8=6400,即6400个脉冲为一转,与所给表相符合。
【该部分摘自文章:图文介绍步进电机的满步、半步、微步驱动原理】
满步驱动(单拍满步驱动和双拍满步驱动 ):
半步驱动:
半步驱动的好处是提高分辨率,但是缺点是扭矩只有满步驱动的70%,当然,也可以通过优化线圈中电流大小,来提高半步驱动扭矩。
微步驱动:
图上英文为:With maximum power in phase A, phase B is at zero. The rotor will line up with phase A. As current to phase A decreases, it increases to phase B. The rotor will take small steps towards phase B until phase B is at its maximum and phase A is at zero. The process then continues around the other phases.
翻译过来大致意思就是:A线圈开始有最大电流,B线圈电流为0,转子指向A线圈。如果A线圈电流慢慢减小,B线圈电流慢慢增大,转子就会慢慢的转向B线圈。
宏观来看,A、B线圈的电流变化都接近于三角函数曲线。电流每一步变化的大小,则决定了微步运动的大小。虽然步数越多,运动越平滑,但是扭矩也会相应的减小。
电流每一步的变化的大小,决定了微步运动的大小,右上图为1/4,1/8,1/16微步电流变化示意图。
微步运动举例:在图1中,A线圈通满电流,图2中A线圈通最大电流的0.92倍,而B线圈通最大电流的0.38倍,实现22.5°旋转。同理,图3中,A和B线圈同时通最大电流的0.71倍,可以实现45°旋转。
步数越多,可以获得越平滑的运动,噪声也越小,不容易失步(丢步),但是代价就是扭矩大大减小。
【基础配置见【STM32】1—零基础硬件软件配置 & 完成LED的闪烁_ココの奇妙な冒険的博客-CSDN博客_stm32配置软件】
原理图:
其输出引脚为PA0。
因希望其正转,则将电平拉高:
采用共阴接法:
相关见2.1.1
【代码参考博客STM32之TIM定时器驱动42步进电机-驱动器型号TB6600_stm32驱动42步进电机】
在自动生成代码的基础上需在main.c中增加:
定义变量:
int count;
开启PWM中断并拉高引脚(后者可省去):
HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_1);
HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_SET);
计数1000次后停转:
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim2)
{
if(count <1000)
{
count++;
}
else
{
HAL_TIM_PWM_Stop_IT(&htim2, TIM_CHANNEL_1);
count = 0;
}
}
}
总代码如下:
main.c:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
int count;
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_1);
HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim2)
{
if(count <1000)
{
count++;
}
else
{
HAL_TIM_PWM_Stop_IT(&htim2, TIM_CHANNEL_1);
count = 0;
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
gpio.c:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "gpio.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_SET);
/*Configure GPIO pin : PtPin */
GPIO_InitStruct.Pin = DIR_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DIR_GPIO_Port, &GPIO_InitStruct);
}
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
tim.c:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.c
* @brief This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim2;
/* TIM2 init function */
void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 83;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 500;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&htim2);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspInit 0 */
/* USER CODE END TIM2_MspInit 0 */
/* TIM2 clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
/* TIM2 interrupt Init */
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_MspInit 1 */
/* USER CODE END TIM2_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspPostInit 0 */
/* USER CODE END TIM2_MspPostInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM2 GPIO Configuration
PA0-WKUP ------> TIM2_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN TIM2_MspPostInit 1 */
/* USER CODE END TIM2_MspPostInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspDeInit 0 */
/* USER CODE END TIM2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM2_CLK_DISABLE();
/* TIM2 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_MspDeInit 1 */
/* USER CODE END TIM2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
42步进电机的转动
某宝店家提供资料如下: