欢迎入群共同学习交流
时间记录:2024/11/2
一、模块解析
1.uln2003
E脚:接GND
COM脚:接VCC外部电源
1-7B:输入引脚
1-7C:输出引脚,输入与输出反向
无法输出高电平,外围电路需要接上拉电路输出高电平,是一种反向达林顿管,用于高电流高电压器件控制
2.28BYJ-48步进电机
四相八步示意图,正为公共端接5V,然后A-D端低电平线圈会通电产生电磁推动转子转动,由于步进电机内部是由多个齿轮组成的,根据手册控制齿轮转1圈360°外部转子齿轮转动5.625°,8拍(步)减速比为64,即1步的步距角为5.625°/64。
步 | 电压A-B-C-D |
---|---|
1 | 0-1-1-1 |
2 | 0-0-1-1 |
3 | 1-0-1-1 |
4 | 1-0-0-1 |
5 | 1-1-0-1 |
6 | 1-1-0-0 |
7 | 1-1-1-0 |
8 | 0-1-1-0 |
转动顺序A->AB->B->BC->C->CD->D->DA
四步转动顺序A->B->C->D或者AB->BC->CD->DA,减速比为32
二、STM32单片机示例程序
头文件
#ifndef __STEPMOTOR_H__
#define __STEPMOTOR_H__#include "stm32f10x.h"typedef enum __STEPMOTOR_ROTATION
{STEPMOTORGO = 0,STEPMOTORBACK
}STEPMOTORDIR;#define STEPMOTORMAXSPEED 0.9
#define STEPMOTORMINSPEED 4.5void stepmotorInit(void);
void stepmotorRotate(float angle, STEPMOTORDIR dir, float speed);#endif
源程序
#include "stepmotor.h"
#include "delay.h"
#include "math.h"#define IN1A(bit) GPIO_WriteBit(GPIOA, GPIO_Pin_0, bit)
#define IN2B(bit) GPIO_WriteBit(GPIOA, GPIO_Pin_1, bit)
#define IN3C(bit) GPIO_WriteBit(GPIOA, GPIO_Pin_2, bit)
#define IN4D(bit) GPIO_WriteBit(GPIOA, GPIO_Pin_3, bit)static void stepmotorSetDuty(u8 step, u8 direction); // A->AB->B->BC->C->CD->D->DAvoid stepmotorInit(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);
}static void stepmotorSetDuty(u8 step, u8 direction)
{u8 tempStep = step; // 1个脉冲对应5.625/64°if(direction == STEPMOTORBACK)tempStep = 7-step;switch(tempStep){case 0:IN1A(Bit_SET);IN2B(Bit_RESET);IN3C(Bit_RESET);IN4D(Bit_RESET);break;case 1:IN1A(Bit_SET);IN2B(Bit_SET);IN3C(Bit_RESET);IN4D(Bit_RESET);break;case 2:IN1A(Bit_RESET);IN2B(Bit_SET);IN3C(Bit_RESET);IN4D(Bit_RESET);break;case 3:IN1A(Bit_RESET);IN2B(Bit_SET);IN3C(Bit_SET);IN4D(Bit_RESET);break;case 4:IN1A(Bit_RESET);IN2B(Bit_RESET);IN3C(Bit_SET);IN4D(Bit_RESET);break;case 5:IN1A(Bit_RESET);IN2B(Bit_RESET);IN3C(Bit_SET);IN4D(Bit_SET);break;case 6:IN1A(Bit_RESET);IN2B(Bit_RESET);IN3C(Bit_RESET);IN4D(Bit_SET);break;case 7:IN1A(Bit_SET);IN2B(Bit_RESET);IN3C(Bit_RESET);IN4D(Bit_SET);break;}
}void stepmotorRotate(float angle, STEPMOTORDIR dir, float speed)
{int stepNum = (int)round(angle*64/5.625);for(int i=0;i<stepNum;i++){stepmotorSetDuty(i%8, dir);delayUs(speed*1000);}
}
主程序
#include "stm32f10x.h"
#include "delay.h"
#include "stepmotor.h"int main()
{delayInit();stepmotorInit();stepmotorRotate(360.0f, STEPMOTORBACK, STEPMOTORMINSPEED);while(1){}
}