当前位置:网站首页>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;
}
}
}
边栏推荐
- 电竞、便捷、高效、安全,盘点OriginOS功能的关键词
- hcip BGP 增强实验
- 偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
- 你最隐秘的性格在哪?
- 力扣(LeetCode)216. 组合总和 III(2022.08.04)
- 欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
- 21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
- 【AGC】增长服务1-远程配置示例
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)
- Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
猜你喜欢

【zeno】为zeno增加子模块/新节点的最小化的例子

Complete image segmentation efficiently based on MindSpore and realize Dice!

项目成本控制如何帮助项目成功?

5.部署web项目到云服务器

Tanabata romantic date without overtime, RPA robot helps you get the job done

21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)

leetcode: 529. Minesweeper Game

three物体围绕一周呈球形排列

Keil升级到AC6后,到底有哪些变化?

【MindSpore Easy-Diantong Robot-01】You may have seen many knowledge quiz robots, but this one is a bit different
随机推荐
static linking and dynamic linking
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
力扣(LeetCode)216. 组合总和 III(2022.08.04)
2.4G无线收发模块的应用
歌词整理
NowCoderTOP35-40 - continuous update ing
浅析WSGI协议
【MindSpore Easy-Diantong Robot-01】You may have seen many knowledge quiz robots, but this one is a bit different
PHP 操作mangoDb
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
一文道清什么是SPL
shell脚本实例
手把手教你纯c实现异常捕获try-catch组件
2022-08-01 Review the basic binary tree and operations
【Unity】【UGUI】【在屏幕上显示文本】
Analysis and practice of antjian webshell dynamic encrypted connection
无题六
Keil升级到AC6后,到底有哪些变化?
ffmpeg drawtext add text watermark
CPU的亲缘性affinity