28BYJ-48步进电机+ULN2003驱动程序(stm32f103c8t6)

28BYJ-48步进电机+ULN2003驱动程序四相八拍(stm32f103c8t6)

在这里插入图片描述

前面发布的四相四拍,这次的程序是四相八拍:
AD-A-AB-B-CB-C-DC-D 四相八拍正转

  • 对应的引脚:(请务必使用该引脚,因为是寄存器操作,更换引脚将配置的输出对应不上输出)
  • //IN4: PB9 d //IN3: PB8 c //IN2: PB7 b //IN1: PB6 a
  • 四相四拍和四相八拍都在其中,使用那种启用和注释对应的即可实现转换
//四相四拍电流0.35A,四相八拍电流0.7A

//四相四拍运行方式:
unsigned short phasecw[4] ={
    
    0x0200,0x0100,0x0080,0x0040};// D-C-B-A   四相四拍反转
unsigned short phaseccw[4]={
    
    0x0040,0x0080,0x0100,0x0200};// A-B-C-D   四相四拍正转

//四相八拍运行方式:
//unsigned short phasecw[8] ={0x0200,0x300,0x100,0x180,0x0080,0xC0,0x0040,0X60};// D-DC-C-CB-B-BA-A-AD   四相八拍反转
//unsigned short phaseccw[8]={0x60,0x004,0xC0,0X80,0X180,0X100,0X300,0x0200};// AD-A-AB-B-CB-C-DC-D 
  • 如果想自己自定义引脚使用请参照下图自行定义:橙黄粉蓝分别代表28BYJ-48步进电机的线序
  • 在这里插入图片描述
  • 程序step.c

//IN4: PB9  d
//IN3: PB8  c
//IN2: PB7  b
//IN1: PB6  a


#include "stdio.h"
#include "sys.h"
#include "delay.h"
#include "step.h"

//四相四拍电流0.35A,四相八拍电流0.7A

//四相四拍运行方式:
unsigned short phasecw[4] ={
    
    0x0200,0x0100,0x0080,0x0040};// D-C-B-A   四相四拍反转
unsigned short phaseccw[4]={
    
    0x0040,0x0080,0x0100,0x0200};// A-B-C-D   四相四拍正转

//四相八拍运行方式:
//unsigned short phasecw[8] ={0x0200,0x300,0x100,0x180,0x0080,0xC0,0x0040,0X60};// D-DC-C-CB-B-BA-A-AD   四相八拍反转
//unsigned short phaseccw[8]={0x60,0x004,0xC0,0X80,0X180,0X100,0X300,0x0200};// AD-A-AB-B-CB-C-DC-D   四相八拍正转


void Moto_Init(void)
{
    
    //引脚初始化
	 GPIO_InitTypeDef GPIO_InitStructure;
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 ;//引脚按着INT1顺序接就行了
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_Init(GPIOB,&GPIO_InitStructure);
	 GPIO_ResetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 );
}
void MotoRcw(void)  //反转
{
    
      
    int  i;  
//	for(i=0;i<8;i++) //四相八拍 
    for(i=0;i<4;i++)  
    {
    
      
        GPIO_Write(GPIOB,phasecw[i]);  
        delay_ms(3);  
    }  
}

void MotoRccw(void)  //正转
{
    
      
    int i;  
//	for(i=0;i<8;i++) //四相八拍
   for(i=0;i<4;i++)  
    {
    
      
        GPIO_Write(GPIOB,phaseccw[i]);  
        delay_ms(3);  
    }  
}

void MotorStop(void) //停止
{
    
      
    GPIO_Write(GPIOB,0x0000);  
}

//控制电机正转还是反转某个角度
//direction方向,1为正转,0为反转
//angle角度,可为0-360具有实际意义
void Motor_Ctrl_Direction_Angle(int direction, int angle)
{
    
    
	u16 j;
	if(direction == 1)
	{
    
    
		for(j=0;j<64*angle/45;j++) 
		{
    
    
			MotoRccw();//正转
		}
		 MotorStop();//停止
		//delay_ms(3);
  }
	else
	{
    
    
		for(j=0;j<64*angle/45;j++) 
		{
    
    
			MotoRcw();//反转
		}
		 MotorStop();//停止
				//delay_ms(3);
	}
}




  • step.c
#ifndef  __STEP_H
#define __STEP_H



void Moto_Init(void);
void Motor_Ctrl_Direction_Angle(int direction, int angle);
void MotorStop(void);//停止
void MotoRccw(void); //正转
void MotoRcw(void); //反转


#endif


  • main.c
#include "stm32f10x.h"
#include  "led.h"
#include "delay.h"
#include "step.h"


int main(void)
{
    
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
   Moto_Init();
	
	 LED_Init();
	GPIO_ResetBits(GPIOC, GPIO_Pin_13);
	
    delay_init();


    while(1){
    
    


Motor_Ctrl_Direction_Angle(1, 360);	//正转1圈	
	delay_ms(2000);
Motor_Ctrl_Direction_Angle(0, 360);	//反转一圈	
		}
}


猜你喜欢

转载自blog.csdn.net/weixin_42880082/article/details/115428219

相关文章