赞
踩
在我们平时做一些小项目的时候,需要用到步进电机驱动某一部件转动某一固定角度或者是走过固定长度。
比如自制一个具有扫风功能的小风扇,由于风扇自身尺寸偏小,使用如图1所示传统的复杂机械结还构去实现扫风功能偏难,反而会影响风扇的运行效果和产生噪音。
还比如用超声波制作一个类似于雷达扫描的装置感知周围环境轮廓,需要使用某种旋转机构驱动超声波探头匀速旋转固定角度。
针对以上需求,有两种解决方案,一种是选择扭力合适的舵机,另一种就是选用如图2所示的4相5线步进电机,其型号为28BYJ-48,驱动电压为5V,需要使用ULN2003驱动板进行驱动,如图3所示。
下面就具体介绍如何使用STM32单片机驱动它转动固定角度吧,亲测有效好用。
一、STM32和ULN2003驱动板接线
STM32F103C8T6 | ULN2003驱动板 |
PB6 | IN1 |
PB7 | IN2 |
PB8 | IN3 |
PB9 | IN4 |
驱动板供电需单独供电,电压范围5-12V,不建议用单片机进行供电,因为单片机是微控制器,不是驱动器。
然后接线的实物图如图4所示。
二、STM32F103C8T6程序编写
1、在工程里面新建step.h文件,在里面定义电机所用端口
- /* 定义步进电机连接的GPIO端口*/
- #define IN1_PORT GPIOB
- #define IN1_CLK RCC_APB2Periph_GPIOB
- #define IN1_PIN GPIO_Pin_6
-
- #define IN2_PORT GPIOB
- #define IN2_CLK RCC_APB2Periph_GPIOB
- #define IN2_PIN GPIO_Pin_7
-
- #define IN3_PORT GPIOB
- #define IN3_CLK RCC_APB2Periph_GPIOB
- #define IN3_PIN GPIO_Pin_8
-
- #define IN4_PORT GPIOB
- #define IN4_CLK RCC_APB2Periph_GPIOB
- #define IN4_PIN GPIO_Pin_9

2、新建step.c文件,编写step引脚初始化函数:
- void STEP_GPIO_Config(void)
- {
- /*定义一个GPIO_InitTypeDef类型的结构体*/
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /*开启STEP相关的GPIO外设时钟*/
- RCC_APB2PeriphClockCmd( IN1_CLK | IN2_CLK | IN3_CLK | IN4_CLK, ENABLE);
- /*选择要控制STEP的GPIO引脚*/
- GPIO_InitStructure.GPIO_Pin = IN1_PIN;
-
- /*设置引脚模式为通用推挽输出*/
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
-
- /*设置引脚速率为50MHz */
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
- /*调用库函数,初始化GPIO*/
- GPIO_Init(IN1_PORT, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = IN2_PIN;
- GPIO_Init(IN2_PORT, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = IN3_PIN;
- GPIO_Init(IN3_PORT, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = IN4_PIN;
- GPIO_Init(IN4_PORT, &GPIO_InitStructure);
-
- /* 初始化电机引脚 */
- GPIO_ResetBits(IN1_PORT, IN1_PIN);
- GPIO_ResetBits(IN2_PORT, IN2_PIN);
- GPIO_ResetBits(IN3_PORT, IN3_PIN);
- GPIO_ResetBits(IN4_PORT, IN4_PIN);
- }

3、编写正反转、停止函数
先定义使电机正反转的编码数组
- unsigned short phasecw[4] ={0x0200,0x0100,0x0080,0x0040};// D-C-B-A 反转
- unsigned short phaseccw[4]={0x0040,0x0080,0x0100,0x0200};// A-B-C-D 正转
然后编写正转、反转、停止函数,里面的SysTick_Delay_Ms(3);函数是在系统时钟配置函数里面编写的进准延时函数,单位是ms,这里使用延时3ms。
- void MotoRccw(void) //正转
- {
- int i;
- for(i=0;i<4;i++)
- {
- GPIO_Write(GPIOB,phaseccw[i]);
- SysTick_Delay_Ms(3);
- }
- }
-
- void MotoRcw(void) //反转
- {
- int i;
- for(i=0;i<4;i++)
- {
- GPIO_Write(GPIOB,phasecw[i]);
- SysTick_Delay_Ms(3);
- }
- }
-
- void MotorStop(void) //停止
- {
- GPIO_Write(GPIOB,0x0000);
- }

4、编写电机正/反转特定角度函数
- //控制电机正转还是反转某个角度
- //direction方向,1为正转,0为反转
- //angle角度,0-360度
- void Motor_Ctrl_Direction_Angle(int direction, int angle)
- {
- u16 j;
- if(direction == 1)
- {
- for(j=0;j<64*angle/45;j++)
- {
- MotoRccw();//正转
- }
- MotorStop();//停止
- }
- else
- {
- for(j=0;j<64*angle/45;j++)
- {
- MotoRcw();//反转
- }
- MotorStop();//停止
- }
- }

最后别忘了将.c文件编写的函数在.h文件里面申明
- void STEP_GPIO_Config(void);
- void MotorStop(void);
- void MotoRccw(void);
- void MotoRcw(void);
- void Motor_Ctrl_Direction_Angle(int direction, int angle);
5、编写主函数程序
通过死循环让电机往复循环转动360度。
- while(1)
- {
- Motor_Ctrl_Direction_Angle(1,360);//正转180度
- SysTick_Delay_Ms(500);
- Motor_Ctrl_Direction_Angle(0,360);//反转90度
- SysTick_Delay_Ms(500);
- }
然后,这样就可以在此基础上修改主程序代码,实现自己想要的效果啦!
完整工程代码连接:https://gitee.com/liangshuzhi/stm32-project-engineering.git
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。