当前位置:网站首页>STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
2022-08-05 09:47: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();
}
//步进电机反转圈函数
void motor_back_circle(int ring ,int speed)
{
int i;
for(i=0;i<ring;i++)
{
motor_back_angle(360,speed);
}
}
//步进电机正转圈函数
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); //步进电机正转函数
void motor_back(int speed); //步进电机反转函数
void motor_just_angle(int angle,int speed); //步进电机正转角度函数
void motor_back_angle(int angle,int speed); //步进电机反转角度函数
void motor_stop(void); //步进电机停止函数
void motor_just_circle(int ring ,int speed); //步进电机正转圈函数
void motor_back_circle(int ring ,int speed);//步进电机反转圈函数
//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框架使用(一)
- 皕杰报表的下拉框联动
- 19.服务器端会话技术Session
- Custom filters and interceptors implement ThreadLocal thread closure
- What is the function of the regular expression replaceFirst() method?
- 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
- 微服务 技术栈
- Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
- Egg framework usage (2)
- 如何实现按键的短按、长按检测?
猜你喜欢

Hundred lines of code launch red hearts, why programmers lose their girlfriends!

eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑

周报2022-8-4

express hot-reload

放大器OPA855的噪声计算实例

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g

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

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

There is only one switch, how to realize the nqa of master-slave automatic switching

皕杰报表的下拉框联动
随机推荐
hcip BGP 增强实验
无题六
Example of Noise Calculation for Amplifier OPA855
How to realize the short press and long press detection of the button?
无题十
Concurrent CAS
5.部署web项目到云服务器
tensorflow.keras cannot introduce layers
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
Assembly language (8) x86 inline assembly
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
手写柯里化 - toString 理解
IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
Keil升级到AC6后,到底有哪些变化?
无题九
The Seven Weapons of Programmers
Two-table query average grouping in sql server
express hot-reload
PAT乙级-B1019 数字黑洞(20)