2325 views|1 replies

8489

Posts

0

Resources
The OP

Four-quadrant stepper motor control simulation with interpolation calculation and single-chip microcomputer source program [Copy link]

51 MCU uses ULN2003A chip to drive stepper motor program plus Proteus simulation The MCU source program is as follows: #include
#define uint unsigned int
#define uchar unsigned char
sbit KEY1=P1^0;
sbit KEY2=P1^1;
sbit KEY3=P1^2;
sbit KEY4=P1^3;
sbit KEY5=P1^4;
sbit KEY6=P1^5;
sbit KEY7=P1^6;


uchar code motor_1[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09,
                                          0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; //第一象限
uchar code motor_2[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09,
                                          0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; //第二象限
uchar code motor_3[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09,
                                          0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90};        //第三象限
uchar code motor_4[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09,
                                          0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90};
uchar code shuma[]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //第四象限


uchar bushu[50];  //路径存储
int F,Xe=0,Ye=0,J,Xm=0,Ym=0,dat=0,xiangxian=0;
uint key_flag=0,key_change=0,chabu_flag=0,motor_flag=1,start_flag=0; void keyscan(void); //Get the keys void chabu(void); //Interpolation calculation void start(void); //Stepper motor execution and digital tube display void main(void) { EA = 1; /*************************************/ ET0 = 1; [color=#25 2525] TMOD = 0x01; //Timer initialization// TH0= (65536-1000) / 256; TL0= (65536-1000)% 256; /****************************************/ TR0 = 1; while(1) { keyscan(); //Get key count chabu(); //Interpolation calculation, generate step trajectory start(); //The motor runs according to the trajectory } } void keyscan(void) { [color=#2 52525] static uint key_up=1; if(key_up&&(KEY1==0||KEY2==0||KEY3==0||KEY4==0||KEY5==0||KEY6==0||KEY7==0)) [color=#25252 5] { key_up=0; // Double click is not supported. if(key_change==1) //Debounce, wait 10ms { if(KEY1==0) Xe++; [color=#2 52525] if(KEY2==0) Ye++; if(KEY3==0) {key_flag=1;xiangxian=1;} if(KEY4==0) {key_flag=1;xiangxian=2;}
                        if(KEY5==0) {key_flag=1;xiangxian=3;}
                        if(KEY6==0) {key_flag=1;xiangxian=4;}
                        if(KEY7==0) {P0=0X99;}


                }
        }
        else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1&&KEY5==1&&KEY6==1)key_up=1;//按键松开        
}


void chabu(void)
{
        int i,a=0,b=0;
        
        if(key_flag==1)
        {
                J=Xe+Ye;
                for(i=0;i" color]="" f="((Ym*Xe)-(Xm*Ye));" if(f="=0||F" size][="" {[="" 插补计算公式[="">0) //If F is greater than or equal to zero, go in the X direction { a++; if(a==8)a=0; bushu=(a-1); Xm++; } else //Otherwise go in the Y direction { b++; if(b==8)b=0; bushu=(b+7); Ym++; } if(i==(J-1)) //Judge whether the end point is reached { chabu_flag=1; key_flag=0; start_flag=1; } } } } void start(void) { if(chabu_flag==1&&motor_flag==1&&start_flag==1&&xiangxian==1)//Determine whether it is the first quadrant { motor_flag=0; P0=motor_1[bushu[dat]]; if(bushu[dat]<8) { P2=shuma[bushu[dat]]; } else { P3=shuma[(bushu[dat]-8)]; }
                if(dat==(J-1))
                {
                        Xe=0;Ye=0;Xm=0;Ym=0;dat=0;              //初始化变量
                        key_flag=0;key_change=0;chabu_flag=0;
                        motor_flag=1;start_flag=0;              //初始化标志量
                }        
        }




        if(chabu_flag==1&&motor_flag==1&&start_flag==1&&xiangxian==2)//判断是否为第二象限
        {           
                motor_flag=0;
                P0=motor_2[bushu[dat]];
                if(bushu[dat]<8)
                {
                        P2=shuma[bushu[dat]];
                }
                else
                {
                        P3=shuma[(bushu[dat]-8)];
                }
                if(dat==(J-1))
                {
                        Xe=0;Ye=0;Xm=0;Ym=0;dat=0;              //初始化变量
                        key_flag=0;key_change=0;chabu_flag=0;
                        motor_flag=1;start_flag=0;              //初始化标志量
                }        
        }


        if(chabu_flag==1&&motor_flag==1&&start_flag==1&&xiangxian==3)//判断是否为第三象限
        {           
                motor_flag=0;
                P0=motor_3[bushu[dat]];
                if(bushu[dat]<8)
                {
                        P2=shuma[bushu[dat]];
                }
                else
{ P3=shuma[(bushu[dat]-8)]; [size =14px] } if(dat==(J-1)) [size =14px] { Xe=0;Ye=0;Xm=0;Ym=0;dat=0; //Initialize variables[/size ] key_flag=0;key_change=0;chabu_flag=0; motor_flag=1;start_flag=0; //Initialize the flag } [/ color] } [size= 14px] if(chabu_flag==1&&motor_flag==1&&start_flag==1&&xiangxian==4) //Judge whether it is the fourth quadrant { motor_flag=0; P0=motor_4[bushu[dat]]; if(bushu[dat]<8)[ /size] { P2=shuma[bushu[dat]];[/ size] } else [color=# 252525] { P3=shuma[(bushu[dat]-8)]; } [size =14px] if(dat==(J-1)) { [size =14px] Xe=0; Ye=0; key_change=0;chabu_flag=0; motor_flag=1;start_flag=0; //Initialize the flag amount } }[ /color] } [size =14px]void Timer0( ) interrupt 1 { static uchar key_jishu=0,motor_jishu=0,motor_jishu_a=0; TR0 = 0; TH0= (65536 -1000) / 256; //Timing 1ms TL0= (65536-1000)%256; [color= #252525] key_jishu++; if(key_jishu==10&&key_change==0) //Key delay[/ color] { key_change=1; //Set the key flag to 1 key_jishu=0; //Reset the count } if(chabu_flag==1) //Count 10ms times, because 500 is too large { motor_jishu++;[/ size] if(motor_jishu==10) { motor_jishu_a++; [size= 14px] motor_jishu=0; } } if(motor_flag==0&&motor_jishu_a==50) //50ms timing[/ size] { motor_flag=1; //Motor flag set to 1 dat++; //Array position, motor step motor_jishu_a=0; //Count value cleared } TR0 = 1; ]if(chabu_flag==1)                                                                   //计数10ms的次数,因为500太大
        {
                motor_jishu++;
                if(motor_jishu==10)
                {
                        motor_jishu_a++;
                        motor_jishu=0;
                }
        }


        if(motor_flag==0&&motor_jishu_a==50)                                 //50ms计时
        {
                motor_flag=1;                                                                        //电机标志量置1
                dat++;                                                                                         //数组位置,电机步数
                motor_jishu_a=0;                                                                 //计数值清零
        }
        TR0 = 1;        
}if(chabu_flag==1)                                                                   //计数10ms的次数,因为500太大
        {
                motor_jishu++;
                if(motor_jishu==10)
                {
                        motor_jishu_a++;
                        motor_jishu=0;
                }
        }


        if(motor_flag==0&&motor_jishu_a==50)                                 //50ms计时
        {
                motor_flag=1;                                                                        //电机标志量置1
                dat++;                                                                                         //数组位置,电机步数
                motor_jishu_a=0;                                                                 //计数值清零
        }
        TR0 = 1;        
}
}
}

This post is from Microcontroller MCU

Latest reply

I learned something, thank you!   Details Published on 2023-6-10 11:16

4

Posts

0

Resources
2

I learned something, thank you!

This post is from Microcontroller MCU

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list