当前位置:网站首页>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;
}
}
}
边栏推荐
- Oracle临时表空间作用
- 首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
- NowCoderTOP35-40 - continuous update ing
- SD NAND Flash简介!
- 无题十二
- Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
- 无题十四
- Seata source code analysis: initialization process of TM RM client
- 企业的数字化转型到底是否可以买来?
- PAT Grade B-B1020 Mooncake(25)
猜你喜欢
自定义过滤器和拦截器实现ThreadLocal线程封闭
Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)
Egg framework usage (2)
Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
three.js debugging tool dat.gui use
蚁剑webshell动态加密连接分析与实践
一文道清什么是SPL
egg框架使用(一)
我们的Web3创业项目,黄了
Redis源码解析:Redis Cluster
随机推荐
js hijacks the array push method
leetcode: 529. Minesweeper Game
shell脚本实例
three物体围绕一周呈球形排列
一文道清什么是SPL
手写柯里化 - toString 理解
无题二
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
你最隐秘的性格在哪?
七夕浪漫约会不加班,RPA机器人帮你搞定工作
mysql索引
Keil升级到AC6后,到底有哪些变化?
[Unity] [UGUI] [Display text on the screen]
开源一夏|OpenHarmony如何查询设备类型(eTS)
Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
Handwriting Currying - toString Comprehension