当前位置:网站首页>STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
2022-08-05 10:03:00 【InfoQ】
一、环境介绍
二、硬件介绍




三、驱动代码

3.1 motor.c
#include "motor.h"
//步进电机正反转数组1
u16 PositiveSequence[4] ={0x0200,0x0100,0x0080,0x0040};// D-C-B-A
u16 ReverseOrder[4]={0x0040,0x0080,0x0100,0x0200};// A-B-C-D.
void motor_delay_ms(u32 ms)
{
u32 i,j;
for(i=0;i<ms;i++)
for(j=0;j<112;j++);
}
void motor_stop(void)
{
MOTOR_1=0;
MOTOR_2=0;
MOTOR_3=0;
MOTOR_4=0;
}
void motor_init(void)
{
RCC->APB2ENR|=1<<4;
GPIOC->CRL&=0x00FFFFFF;
GPIOC->CRL|=0x33000000;
GPIOC->CRH&=0xFFFFFF00;
GPIOC->CRH|=0x00000033;
// IN4: PC9 d
// IN3: PC8 c
// IN2: PC7 b
// IN1: PC6 a
}
void GPIO_Write(GPIO_TypeDef* GPIOx, u16 PortVal)
{
GPIOx->ODR = PortVal;
}
void motor_just(int speed) //一个脉冲
{
uint8_t i;
for(i=0;i<4;i++)
{
GPIO_Write(GPIOC,PositiveSequence[i]);
motor_delay_ms(speed);
}
}
void motor_back(int speed)
{
uint8_t i;
for(i=0;i<4;i++)
{
GPIO_Write(GPIOC,ReverseOrder[i]);
motor_delay_ms(speed);
}
}
//由于 *一个脉冲* *输出轴* 转0.08789度(电机实转0.08789*64=5.625度),即步进角为5.625度.则转完A-B-C-D为 *8个脉冲*
//,即0.08789*8=0.70312度.若称A-B-C-D为一个周期,则j为需要的转完angle角度所需的周期数.
//步进电机正转角度函数
void motor_just_angle(int angle,int speed)
{
int i,j;
j=(int)(angle/0.70312);
for(i=0;i<j;i++)
{
motor_just(speed);
}
motor_stop();
}
//步进电机反转角度函数
void motor_back_angle(int angle,int speed)
{
int i,j;
j=(int)(angle/0.70312);
for(i=0;i<j;i++)
{
motor_back(speed);
}
motor_stop();
}
//The stepper motor reverses the circle function
void motor_back_circle(int ring ,int speed)
{
int i;
for(i=0;i<ring;i++)
{
motor_back_angle(360,speed);
}
}
//Stepper motor forward rotation function
void motor_just_circle(int ring,int speed) //步进电机正转角度函数
{
int i;
for(i=0;i<ring;i++)
{
motor_just_angle(360,speed);
}
}
3.2 motor.h
#ifndef _MOTOR_H
#define _MOTOR_H
#include "sys.h"
void motor_delay_ms(u32 x);//延时函数
void motor_init(void); //步进电机初始化
void motor_just(int speed); //Stepper motor forward rotation function
void motor_back(int speed); //Stepper motor reversal function
void motor_just_angle(int angle,int speed); //步进电机正转角度函数
void motor_back_angle(int angle,int speed); //步进电机反转角度函数
void motor_stop(void); //Stepper motor stop function
void motor_just_circle(int ring ,int speed); //Stepper motor forward rotation function
void motor_back_circle(int ring ,int speed);//The stepper motor reverses the circle function
//IN
#define MOTOR_1 PCout(6)
#define MOTOR_2 PCout(7)
#define MOTOR_3 PCout(8)
#define MOTOR_4 PCout(9)
#endif
3.3 main.c
/*
ULN2003控制28BYJ-48步进电机接线:
ULN2003接线:
IN4: PC9 d
IN3: PC8 c
IN2: PC7 b
IN1: PC6 a
+ : 5V
- : GND
*/
int main()
{
u8 time_cnt=0;
u8 key;
LED_Init();
KEY_Init();
USART1_Init(115200);
motor_init(); //步进电机初始化
USART1_Printf("程序初始化.....\r\n");
while(1)
{
//按键可以测试开锁和关锁
key=KEY_Scan(0);
if(key==1)
{
LED1=0; //亮灯--表示开锁
motor_just_circle(1,300); //电机正转1圈
}
else if(key==2)
{
LED1=1; //灭灯--表示关锁
motor_back_circle(1,300); //电机反转1圈
}
DelayMs(10);
time_cnt++;
if(time_cnt>=50)
{
time_cnt=0;
LED2=!LED2;
}
}
}
边栏推荐
- 无题八
- Egg framework usage (1)
- 开源一夏|OpenHarmony如何查询设备类型(eTS)
- 电竞、便捷、高效、安全,盘点OriginOS功能的关键词
- Oracle临时表空间作用
- [强网杯2022]WP-UM
- 2022-08-01 Review the basic binary tree and operations
- 公众号如何运维?公众号运维专业团队
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
- Brief Analysis of WSGI Protocol
猜你喜欢

创建一个 Dapp,为什么要选择波卡?

Dry goods!Generative Model Evaluation and Diagnosis

Custom filters and interceptors implement ThreadLocal thread closure

哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?

Pycharm 常用外部工具

Egg framework usage (2)

入门 Polkadot 平行链开发,看这一篇就够了

Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?

IDEA performs the Test operation, resulting in duplicate data when data is inserted

手把手教你纯c实现异常捕获try-catch组件
随机推荐
uniapp 连接ibeacon
IDEA performs the Test operation, resulting in duplicate data when data is inserted
无题八
NowCoderTOP35-40——持续更新ing
Imitation SBUS fixed with serial data conversion
IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
项目成本控制如何帮助项目成功?
无题五
Dry goods!Generative Model Evaluation and Diagnosis
Keil升级到AC6后,到底有哪些变化?
PAT乙级-B1021 个位数统计(15)
2022/8/4 考试总结
Open Source Summer | How OpenHarmony Query Device Type (eTS)
Seata source code analysis: initialization process of TM RM client
长达四年的减肥记录
Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
基于MindSpore高效完成图像分割,实现Dice!
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
static linking and dynamic linking
歌词整理