STM32库函数开发之步进电机驱动

步进电机是一种将电脉冲信号转换为角位移的执行机构。当收到一个脉冲信号,转过一定角,这个角度叫做步进角。可以控制脉冲的个数来控制总转过的角度,实现定位。

28BYJ-48是四相八拍电机。

单片机接口信号不够大,需要ULN2003来驱动电机。
下面是用在STM32上编写的步进电机驱动:
试了一下,发现出现中文乱码现象,人比较懒,以后再加代码注释了。

moto.h文件:

#ifndef __MOTO_H
#define __MOTO_H    
#include "sys.h"

void Moto_Init(void); 
void Motorcw(void);   
void MotorStop(void); 
void Motorccw(void); 

#endif

moto.c文件:

#include"delay.h"
#include"moto.h"

uint16_t phasecw[4] ={0x2000,0x0001,0x0004,0x0008};// D-C-B-A {1000 0001 0010 0100}
uint16_t phaseccw[4]={0x0008,0x0004,0x0001,0x2000};// A-B-C-D [0100 0010 0001 1000]

// IN4: PC13
// IN3: PC0
// IN2: PC2
// IN1: PC3
void Motorcw(void) //Õýת
{
uint8_t i;

for(i=0;i<4;i++)
{
GPIO_Write(GPIOC,phasecw[i]);
delay_ms(3);

}
}

void Motorccw(void) //·´×ª
{
uint8_t i;
for(i=0;i<4;i++)
{
GPIO_Write(GPIOC,phaseccw[i]);
delay_ms(3);

}
}

void MotorStop(void) //Í£Ö¹
{
GPIO_Write(GPIOC,0x0000);
}

void Moto_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //GPIO CLOCK ENABLE

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //¿ªÂ©Êä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_0);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
}
打赏作者