STM32
直播中

黄色的小金橘

12年用户 599经验值
私信 关注
[问答]

如何用串口调试助手发送数据让步进电机运转呢

A4988驱动芯片有何功能?
如何用串口调试助手发送数据让步进电机运转呢?

回帖(1)

洪子云

2021-12-20 10:01:34
       首先简单介绍一下A4988这个驱动芯片。



EN:使能端,低电平有效,就是这个脚输入为0时才能用
MS1.2.3:控制一个脉冲旋转多少度。分别是全步进,1/2步进,1/4步进,1/8步进,1/16步进模式。
步进电机走一步是1.8度,一圈就是200步。例如使用1/16步进,则需要走3200步才等于一圈
SLP:休眠,高电平有效。一般不用
STEP:用来输入脉冲,一个脉冲转一下,脉冲频率太快会丢步甚至抖动,频率太低转得慢;脉冲数量配合MS123位使用。
DIR:方向位,0,1各一个方向
VMOT,GND:驱动电源接口,直流供电8-35V 最大2A 的峰值电流
1A,1B,2A,2B:接步进电机的四条线,我用的是四二步进电机。这四个端反接的话,旋转方向也跟着反,配合DIR位使用
VDD,GND:接单片机的3.3V和GND,应该是给这个芯片一个参考的电压标准并共地,我没有详细的翻它的手册,反正就是不解就乱抖,不能用
     综上,控制MS1、MS2、MS3、STEP、DIR就可以了,甚至只用STEP就可以了。
    下面是下位机的控制代码,比较基本。
首先是配置串口,直接用开发板例程的代码就够了。

#include "bsp_usart.h"

static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* 嵌套向量中断控制器组选择 */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  
  /* 配置USART为中断源 */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  /* 抢断优先级*/
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  /* 子优先级 */
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  /* 使能中断 */
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  /* 初始化配置NVIC */
  NVIC_Init(&NVIC_InitStructure);
}


void USART_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;

        // 打开串口GPIO的时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
       
        // 打开串口外设的时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

        // 将USART Tx的GPIO配置为推挽复用模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA , &GPIO_InitStructure);

        // 将USART Rx的GPIO配置为浮空输入模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        // 配置串口的工作参数
        // 配置波特率
        USART_InitStructure.USART_BaudRate = 115200;
        // 配置 针数据字长
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        // 配置停止位
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        // 配置校验位
        USART_InitStructure.USART_Parity = USART_Parity_No ;
        // 配置硬件流控制
        USART_InitStructure.USART_HardwareFlowControl =
        USART_HardwareFlowControl_None;
        // 配置工作模式,收发一起
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        // 完成串口的初始化配置
        USART_Init(USART1, &USART_InitStructure);
        // 串口中断优先级配置
        NVIC_Configuration();
        // 使能串口接收中断
        USART_ITConfig( USART1, USART_IT_RXNE, ENABLE);       
        // 使能串口
        USART_Cmd( USART1, ENABLE);            
}
  
然后是串口中断接收,存到RecvBuffer[]里

// 串口中断服务函数 USART1_IRQHandler
void USART1_IRQHandler(void)
{
        if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
        {               
                RecvBuffer[RecvPointer] = USART_ReceiveData(USART1);
                RecvPointer++;
        }
}


然后就是主函数写一个switch函数对接收到的数据进行遍历,对应规则就是自己想怎么写就怎么写。

while(1)
{
    if(a< RecvPointer)
    {/*初始化IO口用了GPIOB和BPIOC;电机F:B89; B:B1213; L:B1415; R:C67; D:C89;U:C1011*/
        switch(RecvBuffer[a])
        {
            case 0 : motor(GPIOC, GPIO_Pin_11,GPIO_Pin_10,1,1);        a++; break;                        //U1
            case 1 : motor(GPIOC, GPIO_Pin_11,GPIO_Pin_10,1,2);        a++; break;                        //U2
            case 2 : motor(GPIOC, GPIO_Pin_11,GPIO_Pin_10,0,1);        a++; break;                        //U3
                               
            case 3 : motor(GPIOC, GPIO_Pin_9,GPIO_Pin_8,1,1);        a++; break;                                //D1
            case 4 : motor(GPIOC, GPIO_Pin_9,GPIO_Pin_8,1,2);        a++; break;                                //D2
            case 5 : motor(GPIOC, GPIO_Pin_9,GPIO_Pin_8,0,1);        a++; break;                                //D3
                               
            case 6 : motor(GPIOB, GPIO_Pin_9,GPIO_Pin_8,1,1);        a++; break;                          //F1
            case 7 : motor(GPIOB, GPIO_Pin_9,GPIO_Pin_8,1,2);        a++; break;                          //F2
            case 8 : motor(GPIOB, GPIO_Pin_9,GPIO_Pin_8,0,1);        a++; break;                          //F3
                               
            case 9 : motor(GPIOB, GPIO_Pin_13,GPIO_Pin_12,1,1); a++; break;                        //B1
            case 10 : motor(GPIOB, GPIO_Pin_13,GPIO_Pin_12,1,2); a++; break;                //B2
            case 11 : motor(GPIOB, GPIO_Pin_13,GPIO_Pin_12,0,1); a++; break;                //B3                               
                               
            case 12 : motor(GPIOB, GPIO_Pin_15,GPIO_Pin_14,1,1);        a++; break;                //L1
            case 13 : motor(GPIOB, GPIO_Pin_15,GPIO_Pin_14,1,2);        a++; break;                //L2
            case 14 : motor(GPIOB, GPIO_Pin_15,GPIO_Pin_14,0,1);        a++; break;                //L3

            case 15 : motor(GPIOC, GPIO_Pin_7,GPIO_Pin_6,1,1);        a++; break;                        //R1
            case 16 : motor(GPIOC, GPIO_Pin_7,GPIO_Pin_6,1,2);        a++; break;                        //R2
            case 17 : motor(GPIOC, GPIO_Pin_7,GPIO_Pin_6,0,1);        a++; break;                        //R3       

            default : break;
        }
}
}
里面的moter()函数是自己写的一个子函数,输入参数有两个IO口,旋转方向和旋转几个90°

/*GPIO_motornum和GPIOx用于选择电机,GPIO_direction用于选择电机方向,dir:0为逆1为正,k为90°的倍数*/
void motor(GPIO_TypeDef* GPIOx, uint16_t GPIO_motornum, uint16_t GPIO_direction, unsigned int dir, unsigned int k)
{
    unsigned int i, steps;
    steps = 50*k;
    switch(dir)
    {
        case 0 : GPIO_SetBits(GPIOx,GPIO_direction); break;
        case 1 : GPIO_ResetBits(GPIOx,GPIO_direction); break;
        default : break;
    }
    for(i = 0;i < steps; i++)
    {
        GPIO_SetBits(GPIOx,GPIO_motornum);
        Delay(200);                                                                        //周期1.3ms
        GPIO_ResetBits(GPIOx,GPIO_motornum);
        Delay(200);
    }
    Delay(9000);                                        //延时一会
}
有了这些就可以用串口调试助手发数据让步进电机快乐的旋转起来了。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分