【E814】基于STM32F103的ULN2003步进电机驱动源程序

2021-09-06 09:09:59      索炜达电子      570     

项目编号:E814

文件大小:279K

源码说明:带中文注释

开发环境:C编译器

简要概述:

一、本次使用的硬件

开发板:stm32f103c8t6核心板

电机:ULN2003 步进电机扩展板+普通42电机


二、简单原理及代码

1.原理

四相步进电机有两种运行方式 1.四相四拍;2.四相八拍。


拍数

指电机转过一个齿距角所需脉冲数,通俗的来讲拍数指的是步进电机运行时每转一个齿距所需的脉冲数。以四相电机为例,有四相单四拍运行方式即A-B-C-D-A,有四相四拍运行方式即AB-BC-CD-DA-AB,有四相八拍运行方式即 A-AB-B-BC-C-CD-D-DA-A。


转动

给四个管脚一定频率的脉冲即可使其转动,脉冲频率越快,转动越快。反之转动越慢,脉冲次数越多,转动角度越大。


实现方式

使用最简单的延时控制脉冲,将数据放在数组内通过for循环发给驱动模块。不使用定时器或者复杂的计算,能实现短时间内将电机驱动起来。


2.主要函数

main.c

#include "delay.h"

#include "sys.h"

#include "stdio.h"

#include "motor.h"

//IN4: PB9  d

//IN3: PB8  c

//IN2: PB7  b

//IN1: PB6  a

int main(void)

{        

        Moto_Init();                                   //电机

        delay_init();                     //延时函数初始化                  

while(1)

        {

         Motor_Ctrl_Direction_Angle(1,180);//正转180度

         delay_ms(500);

         Motor_Ctrl_Direction_Angle(0,90);//反转90度

        }

}

简单的执行正转180度,然后反转90度。中间的delay_ms()按需要修改,但是注意如果delay_ms()太短可能出现卡顿现象。因为执行上一个转动角度的任务需要一定的时间。所以要合理修改。


motor.c


#include "motor.h"

#include "delay.h"

unsigned short phasecw[4] ={0x0200,0x0100,0x0080,0x0040};// D-C-B-A   反转

unsigned short phaseccw[4]={0x0040,0x0080,0x0100,0x0200};// A-B-C-D   正转

/***************************************************/

//           电机模块与单片机连接引脚

//IN4: PB9  d

//IN3: PB8  c

//IN2: PB7  b  

//IN1: PB6  a

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<4;i++)  

    {  

        GPIO_Write(GPIOB,phasecw[ i]);  

        delay_ms(3);  

    }  

}

正转函数,修改 delay_ms()即可以改变转动速度,反转函数依然同样操作。


void Motor_Ctrl_Direction_Angle(int direction, int angle)

{

        u16 j;

        if(direction == 1)

        {

                for(j=0;j<64*angle/45;j++)

                {

                        MotoRccw();//正转

                }

                 MotorStop();//停止

  }

        else

        {

                for(j=0;j<64*angle/45;j++)

                {

                        MotoRcw();//反转

                }

                 MotorStop();//停止

        }

}

控制电机转动角度函数

direction为方向变量,输入1为正转,0为反转。

angle为角度变量,可为0-360具有实际意义。

**注意:


j<64*angle/45

这一句是转动一度的数学公式,是根据这个步进电机本身的转子数计算出来的.

目录│文件列表:

 └ 步进电机驱动程序

    │ keilkilll.bat

    │ README.TXT

    ├ CORE

    │  │ core_cm3.c

    │  │ core_cm3.h

    │  │ startup_stm32f10x_hd.s

    │  └ startup_stm32f10x_md.s

    ├ HARDWARE

    │  └ LED

    │     │ led.c

    │     └ led.h

    ├ OBJ

    │  └ LED.hex

    ├ STM32F10x_FWLib

    │  ├ inc

    │  │  │ misc.h

    │  │  │ stm32f10x_adc.h

    │  │  │ stm32f10x_bkp.h

    │  │  │ stm32f10x_can.h

    │  │  │ stm32f10x_cec.h

    │  │  │ stm32f10x_crc.h

    │  │  │ stm32f10x_dac.h

    │  │  │ stm32f10x_dbgmcu.h

    │  │  │ stm32f10x_dma.h

    │  │  │ stm32f10x_exti.h

    │  │  │ stm32f10x_flash.h

    │  │  │ stm32f10x_fsmc.h

    │  │  │ stm32f10x_gpio.h

    │  │  │ stm32f10x_i2c.h

    │  │  │ stm32f10x_iwdg.h

    │  │  │ stm32f10x_pwr.h

    │  │  │ stm32f10x_rcc.h

    │  │  │ stm32f10x_rtc.h

    │  │  │ stm32f10x_sdio.h

    │  │  │ stm32f10x_spi.h

    │  │  │ stm32f10x_tim.h

    │  │  │ stm32f10x_usart.h

    │  │  └ stm32f10x_wwdg.h

    │  └ src

    │     │ misc.c

    │     │ stm32f10x_adc.c

    │     │ stm32f10x_bkp.c

    │     │ stm32f10x_can.c

    │     │ stm32f10x_cec.c

    │     │ stm32f10x_crc.c

    │     │ stm32f10x_dac.c

    │     │ stm32f10x_dbgmcu.c

    │     │ stm32f10x_dma.c

    │     │ stm32f10x_exti.c

    │     │ stm32f10x_flash.c

    │     │ stm32f10x_fsmc.c

    │     │ stm32f10x_gpio.c

    │     │ stm32f10x_i2c.c

    │     │ stm32f10x_iwdg.c

    │     │ stm32f10x_pwr.c

    │     │ stm32f10x_rcc.c

    │     │ stm32f10x_rtc.c

    │     │ stm32f10x_sdio.c

    │     │ stm32f10x_spi.c

    │     │ stm32f10x_tim.c

    │     │ stm32f10x_usart.c

    │     └ stm32f10x_wwdg.c

    ├ SYSTEM

    │  ├ delay

    │  │  │ delay.c

    │  │  └ delay.h

    │  ├ sys

    │  │  │ sys.c

    │  │  └ sys.h

    │  └ usart

    │     │ usart.c

    │     └ usart.h

    └ USER

       │ drv_systick.c

       │ drv_systick.h

       │ EventRecorderStub.scvd

       │ JLinkSettings.ini

       │ LED.uvguix.10238

       │ LED.uvoptx

       │ LED.uvprojx

       │ main.c

       │ motor.c

       │ motor.h

       │ step.c

       │ step.h

       │ stm32f10x.h

       │ stm32f10x_conf.h

       │ stm32f10x_it.c

       │ stm32f10x_it.h

       │ system_stm32f10x.c

       │ system_stm32f10x.h

       └ DebugConfig

          │ LED_STM32F103C8_1.0.0.dbgconf

          │ LED_STM32F103RC_1.0.0.dbgconf

          └ LED_STM32F103VC_1.0.0.dbgconf

TAGULN2003
  • 3 次
  • 1 分