OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4454|回复: 2

步进电机,两相四线,L298N驱动

[复制链接]

75

主题

458

帖子

4

精华

金牌会员

Rank: 6Rank: 6

积分
1635
金钱
1635
注册时间
2012-8-28
在线时间
71 小时
发表于 2018-5-29 15:28:52 | 显示全部楼层 |阅读模式
//有加减速的,我自己写的。大概原理你们应该能理解,我写的不是太难。

#ifndef __motor_h
#define __motor_h
///////////
#include "stm32f10x.h"
#include "delay.h"
//////////
//作者QQ750273008   祁成 2018.5.29
//两相四线,步进电机线圈节拍:A-AB-B-A'B-A'-A'B'-B'-AB'
static const u8 motor[]={0X00,0x08,0x0A,0X02,0X06,0X04,0X05,0X01,0X09};//步进电机,停止+8种节拍

//typedef enum {FALSE = 0, TRUE = !FALSE} bool;
typedef struct
{
        u8 num;                                //电机轴的编号0~0xFF
  const u8 *R8;        //步进电机节拍:停止+8种节拍
        u8  r8;                                //电机当前节拍位置
        int add;                        //当前绝对位置
        int go;                                //预计移动
        //加减速参数
        u32 dead;     //最小死区时间(最快速度)//迷你滑台小于0x2000 步进电机丢步
  u32 delay;                //电机转一拍停留时间(必须大于死区延时)
        u16  VF;    //加减速曲线,(值越大,加减速越快)
       
}motors;

//电机默认参数表
static motors zhou={0,motor,1,1,0,0xFFF,0xfffff,255};
//static motors zhou={1,(u8*)motor,1,1,300};

//当前可用轴
static motors
zhouX={0xF1,motor,1,1,0,300},
zhouY={0xF2,motor,1,1,0,300},
zhouZ={0xF3,motor,1,1,0,300},
zhouA={0xFA,motor,1,1,0,300},
zhouB={0xFB,motor,1,1,0,300},
zhouC={0xFC,motor,1,1,0,300};
static motors zhou1={0x01,motor,1,1,0,0x1800,0xffff,255};
//static motors zhou1,zhou2,zhou3,zhou4,zhou5,zhou6,zhou7,zhou8,zhou9;
//static motors zhou10,zhou11,zhou12,zhou13,zhou14,zhou15;

//////////////
void motor_init(void);//初始化
u8 motorGo(motors *x,int go);//轴移动
u8 motorGoto(motors *x,int Goto);//轴定位

#endif


       
qq750273008.jpg

motor.zip

1.18 MB, 下载次数: 298

L298N

QQ750273008有好的资料记得发给我哦。。。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

75

主题

458

帖子

4

精华

金牌会员

Rank: 6Rank: 6

积分
1635
金钱
1635
注册时间
2012-8-28
在线时间
71 小时
 楼主| 发表于 2018-5-29 15:30:40 | 显示全部楼层
#include <motor.h>
#include "stm32f10x.h"
#include "delay.h"
//#include <GPIO.C>

u8 motorGo(motors *x,int go)//轴移动
{
        u8  r,val;//获取旋转编码号
        u32 t;//时间
  int        i;
        if(go>0)//正转
                {
                        x->go=go;//距离脉冲
                       
                        for(i=0;i < x->go;i++)
                                {
                                        r=(x->r8+1)%9;//电机8拍位置加数组0//获取角度编码
                                        if(r==0)r=1;//绕过电机停止的编码值0x00
                                        val=x->R8[r];//输出电机8拍编码
                                        //////////////////////////////
                                        //输出:
                                               GPIOB->ODR&=0XFFFF0FFF;
                                               GPIOB->BSRR=val<<12;
                                                     x->r8=r;        //记录电机最后一次节拍编号
                                        //////////////////////////////
                                        //停留时间:
//                                        delay_ms(x.delay);
                                        for(t=0;t < x->delay;t++);//电机转一拍停留时间
                                       
                                }
                                //记录当前地址
                                x->add=x->add+go;
                        return 0;
                }
        else//反转
                {
                        x->go=go;//距离脉冲
                        for(i=0;i > x->go;i--)//负数
                                {
                                        if(x->r8 >1)r=(x->r8 -1)%8;//电机8拍位置加数组0//获取角度编码
                                        if(x->r8 ==1)r=8;//绕过电机停止的编码值0x00
                                        if(x->r8 < 1)r=8;
                                        val=x->R8[r];//输出电机8拍编码
                                        //////////////////////////////
                                        //输出:
                                               GPIOB->ODR&=0XFFFF0FFF;
                                               GPIOB->BSRR=val<<12;
                                               x->r8=r;        //记录电机最后一次节拍编号
                                        //////////////////////////////
                                        //停留时间:
//                                        delay_ms(x.speed);
//                                        for(t=0;t<(0XFFFFFFFF/ (x->speed*0xFFF));t++);//延时
                                                for(t=0;t < x->delay;t++);//电机转一拍停留时间
                                }
                                //记录当前地址
                                x->add+=go;
                        return 0;
                }

}

u8 motorGoto(motors *x,int Goto)//轴定位
{
        if(x->add > Goto) //大于地址
                {
                        motorGo(x,-(x->add - Goto));//轴移动//负向移动
                }
        else
                {
                        motorGo(x,(Goto - x->add));//轴移动//正向移动
                }
        return 0;
}


void motor_init(void)
{
        //添加并初始化需要用的轴
//        zhou1=zhou2=zhou3=zhou4=zhou5=zhou6=zhou7=zhou8=zhou9=zhou;
//        zhou10=zhou11=zhou12=zhou;
        zhou1=zhou;
        zhou1.num=0x01;
//        zhou2.num=0x02;
//        zhou3.num=0x03;
//        zhou4.num=0x04;
//        zhou5.num=0x05;
//        zhou6.num=0x06;
//        zhou7.num=0x07;
//        zhou8.num=0x08;
//        zhou9.num=0x09;
//        zhou10.num=0x0A;
//        zhou11.num=0x0B;
//        zhou12.num=0x0C;

}



QQ750273008有好的资料记得发给我哦。。。
回复 支持 反对

使用道具 举报

75

主题

458

帖子

4

精华

金牌会员

Rank: 6Rank: 6

积分
1635
金钱
1635
注册时间
2012-8-28
在线时间
71 小时
 楼主| 发表于 2018-5-29 15:33:11 | 显示全部楼层
#include "stm32f10x.h"
#include <motor.h>
#include <GPIO.C>
#include "delay.h"

int main(void)
{
        int i,t;
       
        //电机参数初始化
//motor_init();
        zhou1.num=0x11;
       
        delay_init();//延时初始化
        gpio_Init();//引脚初始化
       
        delay_ms(1800);//延时
        motorGoto(&zhou1,0);
        while(1)
        {
    t=0xffff;//起始速度
                zhou1.VF=0x3ff;//vf曲线
/////////////////////////////////
                for(i=0;i<222;i++)
                {
                        //定位,加速
                        motorGoto(&zhou1,i);
                  if(t > zhou1.dead )//必须大于死区延时(最快速度)
                        {
                                zhou1.delay = t ;//设置:电机转一拍停留时间
                                t -= zhou1.VF;//加速值
                        }
                }
//////////////////////////////////
                for(i=222;i>0;i--)
                {
                        //定位,减速
                        motorGoto(&zhou1,444-i+222);
                  if(t < 0x3FFF )//速度快于最慢速度
                        {
                                zhou1.delay = t;//设置:电机转一拍停留时间
                                t += zhou1.VF;//减速值
                        }
                }

//                zhou1.delay=0x2000;
//                motorGoto(&zhou1,444);
//                delay_ms(1000);//延时
                delay_ms(1000);//延时
                zhou1.delay=0x1800;
                motorGoto(&zhou1,0);
                delay_ms(1000);//延时
         }
}






       
       
       
       
       
QQ750273008有好的资料记得发给我哦。。。
回复 支持 反对

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-5 12:17

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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