栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

使用STM32控制步进电机

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用STM32控制步进电机

本次实验使用的是28BYJ-48,详细介绍步进电机介绍

1.在CudeMX设置IO口作ABCD相输入。

2.其他步骤跟点亮LED灯步骤相同:点亮LED

3.连接好单片机引脚跟驱动板引脚。

4.写入驱动程序。

#define MOTOR_A_H  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET)
#define MOTOR_A_L  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET)
	
#define MOTOR_B_H  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET)
#define MOTOR_B_L  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET)
	
#define MOTOR_C_H  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET)
#define MOTOR_C_L  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET)
	
#define MOTOR_D_H  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET)
#define MOTOR_D_L  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET)
void MOTOR_CONTROL(uint8_t direction)
{
	static uint8_t step = 0;
	if(1 == direction){//反转
		if(0 == step) step = 8;
		step--;
	}
	if(0 == step){//步序1
		MOTOR_A_H;
		MOTOR_B_L;
		MOTOR_C_L;
		MOTOR_D_L;
	}else if(1 == step){//步序2
		MOTOR_A_H;
		MOTOR_B_H;
		MOTOR_C_L;
		MOTOR_D_L;
	}else if(2 == step){//步序3
		MOTOR_A_L;
		MOTOR_B_H;
		MOTOR_C_L;
		MOTOR_D_L;
	}else if(3 == step){//步序4
		MOTOR_A_L;
		MOTOR_B_H;
		MOTOR_C_H;
		MOTOR_D_L;
	}else if(4 == step){//步序5
		MOTOR_A_L;
		MOTOR_B_L;
		MOTOR_C_H;
		MOTOR_D_L;
	}else if(5 == step){//步序6
		MOTOR_A_L;
		MOTOR_B_L;
		MOTOR_C_H;
		MOTOR_D_H;
	}else if(6 == step){//步序7
		MOTOR_A_L;
		MOTOR_B_L;
		MOTOR_C_L;
		MOTOR_D_H;
	}else if(7 == step){//步序8
		MOTOR_A_H;
		MOTOR_B_L;
		MOTOR_C_L;
		MOTOR_D_H;
	}
	if(0 == direction){//正转
		step++; if(8 == step) step = 0;
	}
}

void Step_Motor_Start(uint16_t angle, uint8_t direction)//驱动函数angle:角度,direction正反
{
	int i = 0;
	int pulse = (int)((double)(angle/5.625)*64);
	for(i=0; i 

 5.观察到电机按设定的角度旋转。

04114b5a83dc116dffd3687237ef7f16

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号