- 接线:
- TB6600驱动盒的详细说明:
- 程序设计:
- 工程文件:
做毕业设计要用到57步进电机,所以花了两天时间做了一下电机函数
从淘宝查的资料:
接线如图:
DIR- && PUL- 接 单片机GND
DIR+ 接 PA1
PUL+ 接 PA0
驱动盒TB6600 VCC 24V
驱动盒TB6600 GND 24V的GND
我测出来的 A组(绿+蓝- ) B组(黄+红-)
如何确定AB两组看这个:
关于42步进电机驱动——基于STM32 HAL库实现
接线方法
TB6600驱动盒的详细说明:
先冲英说明:
Microstep Driver 微步驱动程序
Microstep 微步
Pulse 脉冲
我开关SW设置成这个样:
1 off
2 off
3 on
4 on
5 on
6 off
对应下图:
代码我参考我之前用arduino写的驱动程序
hal库设置:
main.c:
#include "main.h" #include "tim.h" #include "usart.h" #include "gpio.h" #include "my_lib.h" int QuanShu; int ITjishu; int fputc(int ch, FILE *f){ HAL_UART_Transmit (&huart1,(uint8_t *)&ch,1,0xffff); return ch; } void SystemClock_Config(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_TIM2_Init(); MX_USART1_UART_Init(); HAL_TIM_base_Start_IT(&htim2);//启动定时器中断 HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);//启动定时器中断 __HAL_TIM_SetAutoreload(&htim2,500); //设置速度快慢 __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 250);//设置脉冲 用不到 dianji_kongzhi(up,90,800); HAL_Delay(5000); // dianji_kongzhi(down,90,800); // HAL_Delay(5000); while (1) { // dianji_kongzhi(up,200,800); // HAL_Delay(5000); // dianji_kongzhi(down,200,800); // HAL_Delay(5000); // dianji_kongzhi(up,100,800); // HAL_Delay(5000); // dianji_kongzhi(down,100,800); // HAL_Delay(5000); HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOC, LED_Pin, GPIO_PIN_SET); HAL_Delay(500); } } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } 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_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) //中断溢出服务函数,每一个周期进入一次中断 { if (htim == (&htim2)) { ITjishu++; if(ITjishu>=3200) //3200个脉冲=1圈 { __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 0);//设置脉冲为0,转完一圈就停 ITjishu=0; } } } void Error_Handler(void) { __disable_irq(); while (1) { } } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t *file, uint32_t line) { } #endif
my_lib.h:
#ifndef __MY_LIB_H__ #define __MY_LIB_H__ #include "main.h" #include "tim.h" #include "usart.h" #include "gpio.h" #include "dian_ji.h" #include "stdio.h" extern int ITjishu; //中断计数 #endif
dian_ji.c:
#include "dian_ji.h" #include "tim.h" void dianji_init(void) { } void dianji_kongzhi(int fangxiang,int sudu,int zhuoqi) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, fangxiang); __HAL_TIM_SetAutoreload(&htim2,sudu); //设置速度快慢 __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, (sudu/2));//设置脉冲 用不到 QuanShu=zhuoqi; }
dian_ji.h
#ifndef __DIAN_JI_H__ #define __DIAN_JI_H__ #include "main.h" #include "gpio.h" #include "my_lib.h" #define up 1 #define down 0 extern int AutoReload_data; //自动重装载值 extern int MaiChong; //脉冲 extern int QuanShu; //圈数 void dianji_init(void); void dianji_set(void); void dianji_kongzhi(int fangxiang,int sudu,int zhuoqi); //方向,速度 #endif
tim.c(没改,给自己留着做参考):
#include "tim.h" TIM_HandleTypeDef htim2; void MX_TIM2_Init(void) { TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; htim2.Instance = TIM2; htim2.Init.Prescaler = 72-1; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 800-1; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 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 = 400; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } HAL_TIM_MspPostInit(&htim2); } void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle) { if(tim_pwmHandle->Instance==TIM2) { __HAL_RCC_TIM2_CLK_ENABLE(); HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); } } void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(timHandle->Instance==TIM2) { __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } } void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle) { if(tim_pwmHandle->Instance==TIM2) { __HAL_RCC_TIM2_CLK_DISABLE(); HAL_NVIC_DisableIRQ(TIM2_IRQn); } }
理论上角度是可控的了,
3200个脉冲为一个周期,映射成360度
1600个脉冲,为180度
800个脉冲,为90度
400个脉冲,为45度
45°x=400
x约等于9(8.888888888888889)
得出角度系数约等于9
945=405
9*8.888888888889=400.000000000005
(小偏差,我能接受,但是做项目不行,你们有更好的算角度的,可以留言说一下)
我的工程文件
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)