找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1177|回复: 0
打印 上一主题 下一主题
收起左侧

用中密度stm32f103xx微控制器驱动an2820双极步进电机

[复制链接]
跳转到指定楼层
楼主
ID:628815 发表于 2019-10-23 15:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x.h"
#include "StepperMotor.h"

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
DMA_InitTypeDef DMA_InitStructure;


uint16_t SRC_Buffer_DEC[20] ={15000,15000,20000,20000,25000,25000,30000,30000,35000,35000,
                          40000,40000,45000,45000,50000,50000,55000,55000,60000,60000};

uint16_t SRC_Buffer_INC[20] ={60000,60000,55000,55000,50000,50000,45000,45000,40000,40000,
                          35000,35000,30000,30000,25000,25000,20000,20000,15000,15000};
                          
                                          
/**
   * @brief  Configures the driver control pin
   * @param  None
   * @retval : None
   */

void Stepper_PinControlConfig(void)
{
    /* Configure PC.04, PC.05, PC.06, PC.07, PC.08, PC.09 as output push pull*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7
                                 | GPIO_Pin_8 | GPIO_Pin_9;
                                 
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOC, &GPIO_InitStructure);  
}


/**
   * @brief  Starts or Stops the stepper motor
   * @param NewState: This parameter can be ENABLE or DISABLE.
   * @retval : None
   */
void Stepper_Start(FunctionalState NewState)
{   
   if(NewState == ENABLE)
   {
      /* Set the C.09 pin */
     GPIO_SetBits(GPIOC, GPIO_Pin_9);
   }
   else
   {   
     /* Reset the C.09 pin */
     GPIO_ResetBits(GPIOC, GPIO_Pin_9);
   }
}


/**
   * @brief  Disables the initialization of the driver to its default reset value
   * @param  None
   * @retval : None
   */
void Stepper_ResetDisable(void)
{   
   /* Set the C.05pin */
   GPIO_SetBits(GPIOC, GPIO_Pin_5);
}



/**
   * @brief  Selects the direction of the rotation
   * @param Stepper_RotationDirection: Specifies the rotation direction
   *   This parameter can be one of the following values:
   * @arg Stepper_RotationDirection_CW : sets clockwise direction
   * @arg Stepper_RotationDirection_CCW: sets counterclockwise direction
   *   
   * @retval : None
   */
void Stepper_SetRotationDirection(uint16_t Stepper_RotationDirection)
{
     if(Stepper_RotationDirection == Stepper_RotationDirection_CW )
   {
     /* Set the C.06 pin */
     GPIO_SetBits(GPIOC, GPIO_Pin_6);
   }
   else
   {   
     /* Reset the C.06 pin */
     GPIO_ResetBits(GPIOC, GPIO_Pin_6);
   }
}



/**
   * @brief  Selects the step mode  
   * @param Stepper_Mode: Specifies the Step Mode
   *   This parameter can be one of the following values:
   * @param Stepper_Full : Sets FULL STEP Mode
   * @param Stepper_Half : Sets HALF STEP Mode
   *   
   * @retval : None
   */
void Stepper_SelectMode(uint16_t Stepper_Mode)
{
   if(Stepper_Mode ==  Stepper_Full)
   {
      /* Reset the C.07 pin */
    GPIO_ResetBits(GPIOC, GPIO_Pin_7);
   }
   else
   {   
     /* Set the C.07 pin */
    GPIO_SetBits(GPIOC, GPIO_Pin_7);
   }
}



/**
   * @brief  Selects the decay mode  
   * @param StepperControlMode: Specifies the Decay Mode
   *   This parameter can be one of the following values:
   * @param Stepper_ControlFast : Sets FAST DECAY Mode
   * @param Stepper_ControlSlow : Sets SLOW DECAY Mode
   *   
   * @retval : None
   */
void Stepper_SetControlMode(uint16_t Stepper_ControlMode)
{
    if(Stepper_ControlMode ==  Stepper_ControlFast)
   {
     /* Reset the C.08 pin */
     GPIO_ResetBits(GPIOC, GPIO_Pin_8);
   }
   else
   {   
    /* Set the C.08 pin */
    GPIO_SetBits(GPIOC, GPIO_Pin_8);
   }

}


/**
   * @brief  Activates or Desactivates the driver
   * @param NewState: This parameter can be ENABLE or DISABLE.
   * @retval : None
   */
void Stepper_Cmd(FunctionalState NewState)
{   
     if(NewState == ENABLE)
   {
     /* TIM2 clock enable */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
      /* GPIOA clock enable */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
     /* GPIOC clock enable */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
     /* Enable DMA1 clock */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
     
   }
   else
   {   
     /* TIM2 clock disable */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
      /* GPIOA clock disable */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, DISABLE);
     /* GPIOC clock disable */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, DISABLE);
     /* Disable DMA1 clock */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, DISABLE);
   
   }
}



/**
   * @brief  Configures the peripherals used to control the stepper motor
   * @param  None
   * @retval : None
   */
void Stepper_Init(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
   /* GPIOA Configuration:TIM2 Channel1 in Output */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

   /* ---------------------------------------------------------------
   TIM2 Configuration: Output Compare Toggle Mode:
   --------------------------------------------------------------- */
    /* Time base configuration */
   TIM_TimeBaseStructure.TIM_Period =60000;
   TIM_TimeBaseStructure.TIM_Prescaler = 2;
   TIM_OCInitStructure.TIM_Pulse = 0;  
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   
   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
   /* Output Compare Toggle Mode configuration: Channel1 */
   TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Toggle;  
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
   TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  
   TIM_ARRPreloadConfig(TIM2, ENABLE);
   
   /* TIM enable counter */
   TIM_Cmd(TIM2, ENABLE);
   
   /* -------------------------------------------------------------------
   DMA1 configuration
   ---------------------------------------------------------------------- */
   
   /* DMA1 channel2 configuration ----------------------------------*/
   DMA_DeInit(DMA1_Channel2);
   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM2_ARR_Address;
   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SRC_Buffer_INC;
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
   DMA_InitStructure.DMA_BufferSize = BufferSize;
   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
   DMA_Init(DMA1_Channel2, &DMA_InitStructure);
   
/* Enable DMA1 Channel2 Transfer Complete interrupt */
   DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
   
   /* Enable DMA1 Channel2 */
   DMA_Cmd(DMA1_Channel2, ENABLE);
   
   /* Enable TIM2 DMA update request */
   TIM_DMACmd(TIM2,TIM_DMA_Update, ENABLE);
     
}

/**
   * @}
   */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

stm32f10x_an2820_fw.chm

675.52 KB, 下载次数: 3, 下载积分: 黑币 -5

用中密度stm32f103xx微控制器驱动an2820双极步进电机详解

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表