×
单片机 > 单片机程序设计 > 详情

STM32之DRV8834电机驱动(IO方式)

发布时间:2020-12-25 发布时间:
|

一、简介


本文介绍如何使用STM32通过DRV8834控制步进电机,使用IO方式控制转动。


二、实验平台


库版本:STM32F10x_StdPeriph_Lib_V3.5.0


编译软件:MDK4.53


硬件平台:STM32开发板(主芯片stm32f103c8t6)


仿真器:JLINK


三、基础知识

1、DRV8834是什么?


答:DRV8834是一个电机驱动芯片,它能控制1个步进电机或2个直流电机的方向、步长、停转。


六、实验步骤


1、编写并添加DRV8834电机驱动


1)编写驱动DRV8834.c(存放在“……HARDWARE”)


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

//name:             DRV8834.c           

//introduce:        DRV8834驱动    

//author:           甜甜的大香瓜                   

//email:            897503845@qq.com       

//QQ group          香瓜单片机之STM8/STM32(164311667)                    

//changetime:       2016.10.18     

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

#include "stm32f10x.h"

#include "DRV8834.h"

#include "GUA_SysTick.h"

 

/*********************宏定义************************/   

#define DRV8834_STEP_PER_CIRCLE                  (int32)(15331)   //电机转动一圈的步数

 

#ifndef U8  

typedef unsigned char U8;  

#endif  

  

#ifndef U16  

typedef unsigned short U16;  

#endif  

 

#ifndef U32  

typedef unsigned long U32;  

#endif  

 

#ifndef int32  

typedef signed long int32;  

#endif 

 

//引脚宏定义

#define GUA_DRV8834_SLEEP GPIOA

#define GUA_DRV8834_SLEEP_PIN GPIO_Pin_7

 

#define GUA_DRV8834_M1 GPIOB

#define GUA_DRV8834_M1_PIN GPIO_Pin_10

 

#define GUA_DRV8834_M0 GPIOB

#define GUA_DRV8834_M0_PIN GPIO_Pin_11

 

#define GUA_DRV8834_DIR GPIOB

#define GUA_DRV8834_DIR_PIN GPIO_Pin_1

 

#define GUA_DRV8834_STEP GPIOB

#define GUA_DRV8834_STEP_PIN GPIO_Pin_0

 

/*********************内部变量************************/  

//MOTOR_CONFIG stMotor_Config = {0x00000000,1500, 0x00, 0x00000000, MOTOR_CONFIG_STATUS_IDLE, 0x00000000}; //初始化电机参数为0步,1500个脉冲每秒的速度,整步,本次电机转动时长,空闲,当前为0位置

MOTOR_CONFIG stMotor_Config = {6000, 500, 0x00, 0x00000000, MOTOR_CONFIG_STATUS_IDLE, 0x00000000}; //初始化电机参数为0步,1500个脉冲每秒的速度,整步,本次电机转动时长,空闲,当前为0位置

 

/*********************内部函数************************/ 

static void DRV8834_IO_Init(void);

static void DRV8834_Set_Direction(int nDirection);

static void DRV8834_Set_Size(U8 nSize);

 

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

//name:             DRV8834_IO_Init           

//introduce:        电机驱动的IO初始化         

//parameter:        none                 

//return:           none         

//author:           甜甜的大香瓜                 

//email:            897503845@qq.com     

//QQ group          香瓜单片机之STM8/STM32(164311667)                  

//changetime:       2016.10.18                     

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

static void DRV8834_IO_Init(void)

{

//IO结构体

GPIO_InitTypeDef GPIO_InitStructure;

//时钟使能

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

//SLEEP配置

GPIO_InitStructure.GPIO_Pin = GUA_DRV8834_SLEEP_PIN; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GUA_DRV8834_SLEEP, &GPIO_InitStructure);

//M1配置

GPIO_InitStructure.GPIO_Pin = GUA_DRV8834_M1_PIN; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GUA_DRV8834_M1, &GPIO_InitStructure);

//M0配置

GPIO_InitStructure.GPIO_Pin = GUA_DRV8834_M0_PIN; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GUA_DRV8834_M0, &GPIO_InitStructure);

 

//DIR配置

GPIO_InitStructure.GPIO_Pin = GUA_DRV8834_DIR_PIN; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GUA_DRV8834_DIR, &GPIO_InitStructure);

//STEP配置

GPIO_InitStructure.GPIO_Pin = GUA_DRV8834_STEP_PIN; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GUA_DRV8834_STEP, &GPIO_InitStructure);

}

 

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

//name:             DRV8834_Control

//introduce:        DRV8834的配置      

//parameter:        nDRV8834_control:需要配置的参数;nDRV8834_control_vaule:配置的数值         

//return:           none      

//author:           甜甜的大香瓜                 

//email:            897503845@qq.com     

//QQ group          香瓜单片机之STM8/STM32(164311667)                  

//changetime:       2016.10.18                     

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

void DRV8834_Control(U8 nDRV8834_control, U8 nDRV8834_control_vaule)

{

  switch(nDRV8834_control)

  {

    //睡眠模式选择

    case DRV8834_CONTROL_SLEEP: 

    {                  

      //睡眠

      if(nDRV8834_control_vaule == DRV8834_CONTROL_SLEEP_ON)

      {

        GPIO_WriteBit(GUA_DRV8834_SLEEP, GUA_DRV8834_SLEEP_PIN, Bit_RESET); //SLEEP 0       

      }

      

      //禁止睡眠

      if(nDRV8834_control_vaule == DRV8834_CONTROL_SLEEP_OFF)

      {

        GPIO_WriteBit(GUA_DRV8834_SLEEP, GUA_DRV8834_SLEEP_PIN, Bit_SET); //SLEEP 1      

      }

      

      break;          

    }

 

    //步数选择

    case DRV8834_CONTROL_MICROSTEP: 

    {                  

      //满步(M1=0, M0=0)

      if(nDRV8834_control_vaule == DRV8834_CONTROL_MICROSTEP_FULL)

      {

        GPIO_WriteBit(GUA_DRV8834_M1, GUA_DRV8834_M1_PIN, Bit_RESET); //M1  0        

        GPIO_WriteBit(GUA_DRV8834_M0, GUA_DRV8834_M0_PIN, Bit_RESET); //M0  0      

      }

      

      //1/2步(M1=0, M0=1)

      if(nDRV8834_control_vaule == DRV8834_CONTROL_MICROSTEP_1_2)

      {

        GPIO_WriteBit(GUA_DRV8834_M1, GUA_DRV8834_M1_PIN, Bit_RESET); //M1  0        

        GPIO_WriteBit(GUA_DRV8834_M0, GUA_DRV8834_M0_PIN, Bit_SET); //M0  1      

      }

/*      

      //1/4步(M1=0, M0=Z)

      if(nDRV8834_control_vaule == DRV8834_CONTROL_MICROSTEP_1_4)

      {

        GPIO_WriteBit(GUA_DRV8834_M1, GUA_DRV8834_M1_PIN, Bit_RESET); //M1  0        

        GPIO_WriteBit(GUA_DRV8834_M0, GUA_DRV8834_M0_PIN, GPIO_MODE_OUT_OD_HIZ_FAST);        // M0  Z       

      }

*/

      //1/8步(M1=1, M0=0)

      if(nDRV8834_control_vaule == DRV8834_CONTROL_MICROSTEP_1_8)

      {

        GPIO_WriteBit(GUA_DRV8834_M1, GUA_DRV8834_M1_PIN, Bit_SET); //M1  1       

        GPIO_WriteBit(GUA_DRV8834_M0, GUA_DRV8834_M0_PIN, Bit_RESET); //M0  0       


关键字:STM32  DRV8834  电机驱动  IO方式

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈msp430f5529入门(2)----时钟配置.例程分析