打印
[STM32]

步进电机驱动小车遇到的问题?

[复制链接]
1002|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
蔬木果|  楼主 | 2019-4-14 00:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近在做步进电机的小车,42步进电机,使用步进电机驱动器驱动,可是程序写好之后,却发现小车一直在原地转,发现一个轮子向前,一个轮子向后。可是根据我的程序,应该是前行的状态。线路也检查了,没有问题。
代码如下,希望大神解惑:
#include "pwm.h"
#include "led.h"
#include "usart.h"

/**************左轮驱动器数据线初始化 ****************/
void Driverleft_Init(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6; //DRIVER_DIR DRIVER_OE
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;      
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;   
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        
        GPIO_Init(GPIOE, &GPIO_InitStructure);            
       
        GPIO_ResetBits(GPIOE,GPIO_Pin_5);
        GPIO_SetBits(GPIOE,GPIO_Pin_6);
}

void TIM14_PWM_Init(u32 arr,u32 psc)
{                                                          
       
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);         
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);        
       
        GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;           //GPIOF9
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;       
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;      
        GPIO_Init(GPIOF,&GPIO_InitStructure);            
          
        TIM_TimeBaseStructure.TIM_Prescaler=psc;  
        TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
        TIM_TimeBaseStructure.TIM_Period=arr;  
        TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
       
        TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);
       
       
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
        TIM_OC1Init(TIM14, &TIM_OCInitStructure);  
  TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM14,ENABLE);
       
        TIM_Cmd(TIM14, ENABLE);
                                                                                  
}

/************** 右轮驱动器数据线初始化 ****************/
void Driverright_Init(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6; //DRIVER_DIR DRIVER_OE
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOF, &GPIO_InitStructure);
       
        GPIO_SetBits(GPIOF,GPIO_Pin_5);
        GPIO_SetBits(GPIOF,GPIO_Pin_6);
}

void TIM13_PWM_Init(u32 arr,u32 psc)
{                                                          
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE);            
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);        
       
        GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM13);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;         
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;       
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        
        GPIO_Init(GPIOF,&GPIO_InitStructure);              
          
        TIM_TimeBaseStructure.TIM_Prescaler=psc;
        TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
        TIM_TimeBaseStructure.TIM_Period=arr;   
        TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
       
        TIM_TimeBaseInit(TIM13,&TIM_TimeBaseStructure);
       
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
        TIM_OC1Init(TIM13, &TIM_OCInitStructure);  
        TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);  
        TIM_ARRPreloadConfig(TIM13,ENABLE);
        TIM_Cmd(TIM13, ENABLE);

}  


使用特权

评论回复

相关帖子

沙发
触觉的爱| | 2019-4-14 17:43 | 只看该作者
是不是电机接线问题?选择任意一个电机,然后交换任意一个绕组的接线。比左转的电机,把A相的A+和A-的接线交换接入,这样初始启动方向就能同向了

使用特权

评论回复
板凳
蔬木果|  楼主 | 2019-4-14 19:05 | 只看该作者
触觉的爱 发表于 2019-4-14 17:43
是不是电机接线问题?选择任意一个电机,然后交换任意一个绕组的接线。比左转的电机,把A相的A+和A- ...

谢谢解惑,我尝试了一下,小车可以正常前进了。多谢。

使用特权

评论回复
地板
触觉的爱| | 2019-4-15 13:59 | 只看该作者
还真是初始启动方向的问题? 看来是很少接触步进电机了。既然要折腾这方面,建议网络搜索一下步进电机的基础资料,了解一下这类电机的特性。这样后面折腾起来也顺手一些

使用特权

评论回复
5
airwill| | 2019-4-15 14:42 | 只看该作者
是的, 电机相线反接,就会出现反转问题。
路过的朋友注意一下

使用特权

评论回复
6
蔬木果|  楼主 | 2019-5-11 20:37 | 只看该作者
触觉的爱 发表于 2019-4-15 13:59
还真是初始启动方向的问题? 看来是很少接触步进电机了。既然要折腾这方面,建议网络搜索一下步进电机的基 ...

嗯嗯,多谢。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

171

帖子

2

粉丝