爱

手游网游通

试管婴儿全攻略服务平台

单片机控制多轴步进电机

本站编辑 2025-07-20 10:51 99 人阅读

采用单片机stc89C2051和mpc006运动控制芯片模块作为控制系统的核心,控制三路步进电机做运动实验。单片机发送指令给mpc006微型运动控制模块,模块信号输出给步进驱动器作高速度运动。可以定点运动,直线插补和圆弧插补。

1.引言

运动控制的应用在国内已有十几年的历史,技术也相当成熟。通常运动控制都需要用到运动控制卡,

运动控制器等产品,但这些产品价格高昂,使用复杂,也不适合由单片机构成的控制系统。而如果直接采

用单片机来做运动控制,由于运动控制对系统性能要求非常高,单片机速度资源有限,难以设计出性能优

良的运动控制模型。因此,本文采用单片机和专业的mpc006运动控制芯片模块构成运动控制系统。

MPC006运动控制芯片模块采用新型FPGA设计,集成实用运动控制功能,可与普通单片机通过串口通讯

对步进电机和伺服电机控制。具有如下特点:

◆串口通讯,仅需使用几条指令,简单可靠。

◆单模块最高六轴输出,多个模块组网工作可达120轴。

◆最大脉冲输出频率为2MHz,脉冲输出使用脉冲+方向方式。

◆最高六轴独立运动控制,任意两轴直线插补,任意两轴圆弧插补。

◆每轴一路硬件回原点。

◆模块带1000条指令缓存深度,指令先进先出,无需高速通讯。

◆模块体积小巧,仅3.5*2.5*1.5cm,双排直插30脚封装。

硬件系统由四部分构成:

(1)单片机部分

单片机与模块只需三根线连接,用作串口通讯的RXD和TXD,用作模块缓存满输出的BUSY信号。引出一按键作为测试使用。

2)mpc006运动控制芯片模块部分

mpc006运动控制芯片模块采用5V电源供电,RXD,TXD,BUSY与单片机连接。X0,X1,X2可作为三路电机的原点信号,P1,D1为1轴的脉冲和方向信号。P2,D2为2轴的脉冲和方向信号。P3,D3为3轴的脉冲和方向信号。

3)原点信号输入部分

原点采用光藕隔离输入,输入端可接NPN型光电开关来作为原点信号。

4)信号输出部分

输出采用NPN晶体管及电极开路输出,分别接到电机驱动器脉冲和方向信号输入端。

MPC006运动控制芯片模块与单片机串口通讯速率为115200bps,数据位为8位,停止位1位,无校验。单片机与mpc006运动控制模块采用串口应答式通讯,单片机作主机,单片机每发送一条指令给mpc006运动控制芯片模块,mpc006运动控制芯片模块返回以0x68开始的固定长度为10个字节的数据串。单片即可以取出需要的数据。一般情况需接收到mpc006运动控制模块返回的数据后单片机才能发送下一条指令。如果程序中不接收模块返回的数据,需间隔5MS以上才能发送下一条指令。另外需注意,当发送指令时如果字节间时间间隔大于1MS,模块会认为整条指令发送结束,所以在发送一条指令给模块时不要被程序里的中断长时间打断。当接收模块的返回的数据时,由于波特率很高,如果有长时间中断打断接收过程,可能会导致接收字节丢失。所以,通讯时最好能暂时关闭其它太占时长中断。mpc运动控制模块带1000条指令缓存空间,并自带基本逻辑判断能力,单片机不用等待一条执行完成后才发送下一条,完全可以一起发送给模块,模块会按顺序自动逐条执行。

单片机发送和接收指令的数据格式如下:

起始码:为一条指令的起始字节内容,固定为0x68。

数据个数:为从数据个数开始到校验和的数据长度。

模块地址:为控制器的通讯地址。地址0对所有控制器都有效。

功能码:表示指令的功能,每条指令的功能码都是唯一的。

参数:表示指令的参数,每条指令的参数字节数并不都是相同的。

校验和:为从数据个数开始到校验和前一个字节的校验和。

mpc006运动控制芯片模块默认地址为0,可以接收带任何地址的指令数据。如果单片机串口只连接了一个模块,地址可以不用设置。

mpc006运动控制芯片模块无需任何初始化。上电后只发一条pmove单轴运行指令对应轴都会有脉冲输出。使用函数前先设置好单片机的串口功能,并将需要用到的函数的原型拷贝到当前程序内。本文所使用的函数原型为基础版本,已根据模块使用说明书中通讯协议将各指令通讯过程描述出来。用户可根据所使用的单片机的资源在保证通讯格式正确的情况下作出适当优化。

试验程序如下:

#

//-----STC89C2051-------

sfrIPH=0XB7;

sfrCCON=0XD8;

sfrCMOD=0XD9;

sfrCL=0XE9;

sfrCH=0XF9;

sfrCCAP0L=0XEA;

sfrCCAP0H=0XFA;

sfrCCAPM0=0XDA;

sfrCCAPM1=0XDB;

sfrP3M1=0XB1;

sfrP3M0=0XB2;

sfrP1M1=0X91;

sfrP1M0=0X92;

sfrWAKE_CLKO=0X8f;

sfrBRT=0x9c;

sfrAUXR=0x8E;

sfrAUXR1=0xA2;

sfrWDT_CONTR=0xc1;

sfrT2MOD=0xC9;

//////////////////

sbitbusy=P3^2;

sbits1=P3^7;

voidinitial()

{

P3M1=0x00;

P3M0=0x80;

P1M1=0x00;

P1M0=0xf9;

}

/*voidinit_uart()//串口1使用硬件波率发生器

{

PCON=0x7f;//波特率不倍速

SCON=0x50;//8位数据,可变波特率

BRT=0xFD;//设定独立波特率发生器重装值波特率115200bps

AUXR|=0x04;//独立波特率发生器时钟为Fosc,即1T

AUXR|=0x01;//串口1选择独立波特率发生器为波特率发生器

AUXR|=0x10;//启动独立波特率发生器

}*/

voidinit_uart()//串口1使用定时器1重装值为波率

{

AUXR=0x54;//使能独立波特率发生器,独立波特1个计1次,T1不分频,串口1选择定时器重装值为波率

SCON=0x50;//uart1方式1,允许接收

TMOD|=0x20;//T1,方式2

TL1=0xFD;//115200波率

TH1=0xFD;//115200波率

TR1=1;//T1开启

}

/*

串口发送一个字节,需根据所使用的单片机作适当更改。

*/

voidUSART_Txbyte(unsignedchari)

{

SBUF=i;

while(TI==0);//等待发送完成

TI=0;//清零串口发送完成中断请求标志

}

/*

串口接收模块返回的10个字节数据,需根据所使用的单片机作适当更改。

*/

voidreceive(unsignedchar*buf)

{

unsignedchari;

for(i=0;i10;i++)

{

while(RI==0);

RI=0;

buf[i]=SBUF;

}

}

/*

串口发送一串数据。

*/

voidUSRAT_transmit(unsignedchar*fdata,unsignedcharlen)

{

unsignedchari;

for(i=0;ilen;i++)

{

USART_Txbyte(fdata[i]);

}

}

/*

函数名:inp_move

功能:二轴直线插补

参数:

cardno卡号

no1X轴轴号

no2Y轴轴号

pulse1,pulse2

X-Y轴移动的距离,范围(

-8388608~+8388607)

mode0:相对位移1:绝对位移

返回值:

0失败

1成功

*/

unsignedcharinp_move(unsignedcharcardno,unsignedcharno1,unsignedcharno2,longpulse1,long

pulse2,unsignedcharmode)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x0F;

OutByte[2]=cardno;

OutByte[3]=0x7;

OutByte[4]=no1;

OutByte[5]=no2;

OutByte[6]=pulse124;

OutByte[7]=pulse116;

OutByte[8]=pulse18;

OutByte[9]=pulse1;

OutByte[10]=pulse224;

OutByte[11]=pulse216;

OutByte[12]=pulse28;

OutByte[13]=pulse2;

OutByte[14]=mode;

OutByte[15]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+\

OutByte[12]+OutByte[13]+OutByte[14];

USRAT_transmit(OutByte,16);

receive(inbuf);

return1;

}

/*

函数名:inp_arc

功能:二轴圆弧插补

参数:cardno卡号

no1参与插补X轴的轴号

no2参与插补Y轴的轴号

x,y圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607)

i,j圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607)

mode0:顺时针插补1:逆时针插补

返回值:

0失败1成功

*/

unsignedcharinp_arc(unsignedcharcardno,unsignedcharno1,unsignedcharno2,longX,longy,longi,longj,unsignedcharmode)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x17;

OutByte[2]=cardno;

OutByte[3]=0x8;

OutByte[4]=no1;

OutByte[5]=no2;

OutByte[6]=X24;

OutByte[7]=X16;

OutByte[8]=X8;

OutByte[9]=X;

OutByte[10]=y24;

OutByte[11]=y16;

OutByte[12]=y8;

OutByte[13]=y;

OutByte[14]=i24;

OutByte[15]=i16;

OutByte[16]=i8;

OutByte[17]=i;

OutByte[18]=j24;

OutByte[19]=j16;

OutByte[20]=j8;

OutByte[21]=j;

OutByte[22]=mode;

OutByte[23]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+\

OutByte[12]+OutByte[13]+OutByte[14]+OutByte[15]+OutByte[16]+OutByte[17]+OutByte[18]+OutByte[19]

+OutByte[20]+OutByte[21]+OutByte[22];

USRAT_transmit(OutByte,24);

receive(inbuf);

return1;

}

/*

函数名:set_speed

功能:设置轴速度

参数:

cardno卡号

axis轴号(1-6)

acc加速时间(ms)

dec减速时间(ms)

startv启动频率为:值*频率倍率(Hz)

speed运行频率为:值*频率倍率(Hz)

range频率倍率(1-100)

返回值:

0失败1成功

*/

unsignedcharset_speed(unsignedcharcardno,unsignedcharaxis,unsignedintacc,unsignedint

dec,unsignedintstartv,unsignedintspeed,unsignedcharrange)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0xe;

OutByte[2]=cardno;

OutByte[3]=1;

OutByte[4]=axis;

OutByte[5]=acc8;

OutByte[6]=acc;

OutByte[7]=dec8;

OutByte[8]=dec;

OutByte[9]=startv8;

OutByte[10]=startv;

OutByte[11]=speed8;

OutByte[12]=speed;

OutByte[13]=range;

OutByte[14]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+OutByte[12]+OutByte[13];

USRAT_transmit(OutByte,15);

receive(inbuf);

return1;

}

/*

函数名:set_soft_limit

功能:设置轴软件限位

参数:

cardno卡号

axis

轴号(1-6)

mode0:解除软件限位1:启用软件限位

pulse1负方向限位脉冲值,范围(-268435455~0)

pulse2正方向限位脉冲值,范围(

0~+268435455)

返回值:

0失败1成功

*/

unsignedcharset_soft_limit(unsignedcharcardno,unsignedcharaxis,unsignedcharmode,longpulse1,long

pulse2)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0xE;

OutByte[2]=cardno;

OutByte[3]=0x13;

OutByte[4]=axis;

OutByte[5]=mode;

OutByte[6]=pulse124;

OutByte[7]=pulse116;

OutByte[8]=pulse18;

OutByte[9]=pulse1;

OutByte[10]=pulse224;

OutByte[11]=pulse216;

OutByte[12]=pulse28;

OutByte[13]=pulse2;

OutByte[14]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+

OutByte[12]+OutByte[13];

USRAT_transmit(OutByte,15);

receive(inbuf);

return1;

}

/*

函数名:set_hard_limit

功能:设置轴硬件限位

参数:

cardno卡号

axis轴号(1-6)

mode0:解除硬件限位1:启用硬件限位

dir0:反方向1:正方向

number端口号(0-9)X0-X9

value状态(0,1)0:输入低电平1:输入高电平

返回值:

0失败1成功

*/

unsignedcharset_hard_limit(unsignedcharcardno,unsignedcharaxis,unsignedcharmode,unsignedchardir,

unsignedcharnumber,unsignedcharvalue)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x9;

OutByte[2]=cardno;

OutByte[3]=0x10;

OutByte[4]=axis;

OutByte[5]=mode;

OutByte[6]=dir;

OutByte[7]=number;

OutByte[8]=value;

OutByte[9]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8];

USRAT_transmit(OutByte,10);

receive(inbuf);

return1;

}

/*

函数名:pmove

功能:单轴运行

参数:

cardno卡号

axis轴号(1-6)

pulse输出的脉冲数0:正方向移动

0:负方向移动范围(-268435455~+268435455)

mode0:相对位移1:绝对位移2:连续位移

返回值:

0失败1成功

*/

unsignedcharpmove(unsignedcharcardno,unsignedcharaxis,longpulse,unsignedcharmode)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0xA;

OutByte[2]=cardno;

OutByte[3]=2

;

OutByte[4]=axis;

OutByte[5]=pulse24;

OutByte[6]=pulse16;

OutByte[7]=pulse8;

OutByte[8]=pulse;

OutByte[9]=mode;

OutByte[10]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9];

USRAT_transmit(OutByte,11);

receive(inbuf);

return1;

}

/*

函数名:wait_delay

功能:等待延时数

参数:

cardno卡号

value延时量(1-10000)MS

返回值:

0失败1成功

*/

unsignedcharwait_delay(unsignedcharcardno,unsignedintvalue)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x6;

OutByte[2]=cardno;

OutByte[3]=0xE;

OutByte[4]=value8;

OutByte[5]=value;

OutByte[6]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5];

USRAT_transmit(OutByte,7);

receive(inbuf);

return1;

}

/*

函数名:set_command_pos

功能:设置轴逻辑位置或编码器值

参数:

cardno卡号

axis轴号(1-8)

pulse

位置脉冲数,范围(

-268435455~+268435455)

返回值:

0失败1成功

*/

unsignedcharset_command_pos(unsignedcharcardno,unsignedcharaxis,longvalue)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x9;

OutByte[2]=cardno;

OutByte[3]=0x12;

OutByte[4]=axis;

OutByte[5]=value24;

OutByte[6]=value16;

OutByte[7]=value8;

OutByte[8]=value;

OutByte[9]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8];

USRAT_transmit(OutByte,10);

receive(inbuf);

return1;

}

/*

函数名:write_bit

功能:写输出口状态或寄存器值

参数:

cardno卡号

number端口号(

0-14)Y0-Y14;寄存器号(100-115)D100-D115

value状态(0,1)0输出低电平1输出高电平;寄存器值(0-255)

返回值:

0失败1成功

*/

unsignedcharwrite_bit(unsignedcharcardno,unsignedcharnumber,unsignedcharvalue)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x6;

OutByte[2]=cardno;

OutByte[3]=3

;

OutByte[4]=number;

OutByte[5]=value;

OutByte[6]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5];

USRAT_transmit(OutByte,7);

receive(inbuf);

return1;

}

/*

函数名:read_bit

功能:读输入口状态或寄存器值

参数:

cardno卡号

number端口号(0-6)X0-X6;寄存器号(100-115)D100-D115

返回值:

状态(0,1)0输出低电平1输出高电平;寄存器值(0-255)

*/

unsignedcharread_bit(unsignedcharcardno,unsignedcharnumber)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x5;

OutByte[2]=cardno;

OutByte[3]=4;

OutByte[4]=number;

OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];

USRAT_transmit(OutByte,6);

receive(inbuf);

returninbuf[5];

}

/*

函数名:sudden_stop

功能:轴停止

参数:

cardno卡号

axis停止的轴号(1-8)1-6:1-6轴停7:直线插补轴停8:圆弧插补轴停

返回值:

0失败1成功

*/

unsignedcharsudden_stop(unsignedcharcardno,unsignedcharaxis)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x5;

OutByte[2]=cardno;

OutByte[3]=0x17;

OutByte[4]=axis;

OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];

USRAT_transmit(OutByte,6);

receive(inbuf);

return1;

}

/*

函数名:wait_in

功能:等待输入口状态

参数:

cardno卡号

number端口号(

0-9)X0-X9

value状态(

0,1)0输入低电平1输入高电平

返回值:

0失败1成功

*/

unsignedcharwait_in(unsignedcharcardno,unsignedcharnumber,unsignedcharvalue)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x6;

OutByte[2]=cardno;

OutByte[3]=0xF;

OutByte[4]=number;

OutByte[5]=value;

OutByte[6]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5];

USRAT_transmit(OutByte,7);

receive(inbuf);

return1;

}

/*

函数名:wait_stop

功能:等待轴停止

参数:

cardno卡号

axis需要等待停止的轴号1-6:1-6轴停7:直线插补轴停8:圆弧插补轴停

返回值:

0失败1成功

*/

unsignedcharwait_stop(unsignedcharcardno,unsignedcharaxis)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x5;

OutByte[2]=cardno;

OutByte[3]=9

;

OutByte[4]=axis;

OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];

USRAT_transmit(OutByte,6);

receive(inbuf);

return1;

}

/*

函数名:get_number

功能:获取唯一序列号

参数:

cardno卡号

返回值:32位序列号

*/

unsignedlongget_number(unsignedcharcardno)

{

unsignedlongtmp=0;

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x4;

OutByte[2]=cardno;

OutByte[3]=0xC;

OutByte[4]=OutByte[1]+OutByte[2]+OutByte[3];

USRAT_transmit(OutByte,5);

receive(inbuf);

tmp=(unsignedlong)inbuf[4]24;

tmp+=(unsignedlong)inbuf[5]16;

tmp+=(unsignedlong)inbuf[6]8;

tmp+=(unsignedlong)inbuf[7];

returntmp;

//return(((unsignedlong)inbuf[4]24)+((unsignedlong)inbuf[5]16)+((unsignedlong)inbuf[6]8)+((unsigned

//long)inbuf[7]));

}

/*

函数名:get_status

功能:获取各轴工作状态

参数:

cardno卡号

返回值:8位二进制,1-6位分别代表1-6轴状态,第7位为直线插补状态,第8位为圆弧插补状态。0

表示停止中,1表示运行中。

*/

unsignedcharget_status(unsignedcharcardno)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x4;

OutByte[2]=cardno;

OutByte[3]=5;

OutByte[4]=OutByte[1]+OutByte[2]+OutByte[3];

USRAT_transmit(OutByte,5);

receive(inbuf);

returninbuf[4];

}

/*

函数名:get_command_pos

功能:获取轴逻辑位置或编码器值

参数:

cardno卡号

axis轴号

返回值:

位置脉冲数,范围(-268435455~+268435455)

*/

longget_command_pos(unsignedcharcardno,unsignedcharaxis)

{

longtmp=0;

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x5;

OutByte[2]=cardno;

OutByte[3]=6

;

OutByte[4]=axis;

OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];

USRAT_transmit(OutByte,6);

receive(inbuf);

tmp=(long)inbuf[5]24;

tmp+=(long)inbuf[6]16;

tmp+=(long)inbuf[7]8;

tmp+=(long)inbuf[8];

returntmp;

//return(((unsignedlong)inbuf[5]24)+((unsignedlong)inbuf[6]16)+((unsignedlong)inbuf[7]8)+((unsigned

//long)inbuf[8]));

}

/*

函数名:set_cardno

功能:设置卡号

参数:

cardno卡号(1-128)模块地址(250)重启

返回值:

0失败1成功

*/

unsignedcharset_cardno(unsignedcharcardno)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=5

;

OutByte[2]=0

;

OutByte[3]=0xFA;

OutByte[4]=cardno;

OutByte[5]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4];

USRAT_transmit(OutByte,6);

receive(inbuf);

return1;

}

/*

函数名:read_fifo_count

功能:获取缓冲空间未执行指令数量

参数:

cardno卡号

返回值:(0-1000)缓冲空间未执行指令数量

*/

unsignedintread_fifo_count(unsignedcharcardno)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x4;

OutByte[2]=cardno;

OutByte[3]=0x0d;

OutByte[4]=OutByte[1]+OutByte[2]+OutByte[3];

USRAT_transmit(OutByte,5);

receive(inbuf);

return(((unsignedint)inbuf[4]8)+inbuf[5]);

}

/*

函数名:wait_pulse

功能:等待轴脉冲数

参数:

cardno卡号

axis轴号(1,2,3,5,7,8)

pulse位置脉冲数,范围(-268435455~+268435455)

返回值:

0失败1成功

*/

unsignedcharwait_pulse(unsignedcharcardno,unsignedcharaxis,longvalue)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x9;

OutByte[2]=cardno;

OutByte[3]=0x19;

OutByte[4]=axis;

OutByte[5]=value24;

OutByte[6]=value16;

OutByte[7]=value8;

OutByte[8]=value;

OutByte[9]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8];

USRAT_transmit(OutByte,10);

receive(inbuf);

return1;

}

/*

函数名:set_synchro

功能:设置轴同步

参数:

cardno卡号

no1主轴号(1,2,3,5,7,8)

no2随动轴号(1,2,3,5,7,8)

mode方式0:关闭1:打开

pulse1主轴脉冲数

pulse2随动轴脉冲数

返回值:

0失败1成功

*/

unsignedcharset_synchro(unsignedcharcardno,unsignedcharno1,unsignedcharno2,unsignedcharmode,int

pulse1,intpulse2)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0xb;

OutByte[2]=cardno;

OutByte[3]=0x22;

OutByte[4]=no1;

OutByte[5]=no2;

OutByte[6]=mode;

OutByte[7]=pulse18;

OutByte[8]=pulse1;

OutByte[9]=pulse28;

OutByte[10]=pulse2;

OutByte[11]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9]+OutByte[10];

USRAT_transmit(OutByte,12);

receive(inbuf);

return1;

}

/*

函数名:inp_move3

功能:三轴直线插补

参数:

cardno卡号

no1X轴轴号

no2Y轴轴号

no3Z轴轴号

pulse1,pulse2,pulse3

X-Y-Z轴移动的距离,范围(

-8388608~+8388607)

mode0:相对坐标1:绝对坐标

返回值:

0失败1成功

*/

unsignedcharinp_move3(unsignedcharcardno,unsignedcharno1,unsignedcharno2,unsignedcharno3,long

pulse1,longpulse2,longpulse3,unsignedcharmode)

{

unsignedcharOutByte[25];

unsignedcharinbuf[12];

OutByte[0]=0x68;

OutByte[1]=0x14;

OutByte[2]=cardno;

OutByte[3]=0x21;

OutByte[4]=no1;

OutByte[5]=no2;

OutByte[6]=no3;

OutByte[7]=pulse124;

OutByte[8]=pulse116;

OutByte[9]=pulse18;

OutByte[10]=pulse1;

OutByte[11]=pulse224;

OutByte[12]=pulse216;

OutByte[13]=pulse28;

OutByte[14]=pulse2;

OutByte[15]=pulse324;

OutByte[16]=pulse316;

OutByte[17]=pulse38;

OutByte[18]=pulse3;

OutByte[19]=mode;

OutByte[20]=OutByte[1]+OutByte[2]+OutByte[3]+OutByte[4]+OutByte[5]+OutByte[6]+OutByte[7]

+OutByte[8]+OutByte[9]+OutByte[10]+OutByte[11]+OutByte[12]+OutByte[13]+OutByte[14]+OutByte[15]

+OutByte[16]+OutByte[1]+OutByte[18]+OutByte[19];

USRAT_transmit(OutByte,21);

receive(inbuf);

return1;

}

voidmain(void)

{

chari;

//initial();

init_uart();

//set_cardno(1);

//设卡号为1

set_hard_limit(1,1,1,0,0,0);//设1轴向负方向运动时X0输入口状态为低时限位,用于回原点

set_hard_limit(1,2,1,0,1,0);//设2轴向负方向运动时X1输入口状态为低时限位,用于回原点

set_hard_limit(1,3,1,0,2,0);//设3轴向负方向运动时X2输入口状态为低时限位,用于回原点

while(1)

{

if(!s1)//按键按下

{

set_speed(1,1,1000,1000,10,200,100);//设1轴速度

set_speed(1,2,1000,1000,10,200,100);//设2轴速度

set_speed(1,3,1000,1000,10,200,100);//设3轴速度

/*1,2,3轴同时回原点*/

pmove(1,1,-1000000,0);

//1轴向负方向运动

pmove(1,2,-1000000,0);

//2轴向负方向运动

pmove(1,3,-1000000,0);

//3轴向负方向运动

wait_stop(1,1);

//等待1轴停止

wait_stop(1,2);

//等待2轴停止

wait_stop(1,3);

//等待3轴停止

set_command_pos(1,1,0);

//

设1轴此时坐标为0

set_command_pos(1,2,0);

//

设2轴此时坐标为0

set_command_pos(1,3,0);

//

设3轴此时坐标为0

/*3轴向正方向运动,单片机不断查询轴状态,直到轴都停止了才执行后面的程序。*/

pmove(1,3,3200,0);

//3轴运动

wait_stop(1,3);//模块自己等待3轴停止

write_bit(1,101,1);//3轴停止后自己将模块寄存器D101写为1

while(read_bit(1,101)!=1);//模块寄存器D101不为1一直等,即前面动作还没执行完

//while(get_status(1));//轴没停止一直等

/*1轴向正方向运动,碰到感应开关X5后停止,然后2轴开始正方向运行3200个脉冲,运行

完成后,将Y12输出口置1,等待5秒,再将Y12输出口置0。*/

pmove(1,1,1000000,0);

//1轴运动

wait_in(1,5,0);

//模块内部自己等待,直到X5为低

sudden_stop(1,1);

//1轴停止

pmove(1,2,3200,0);

//2轴运动

wait_stop(1,2);

//模块内部自己等待2轴停止

write_bit(1,12,1);//将Y12输出口置1

wait_delay(1,5000);

//模块内部延时5秒

write_bit(1,12,0);//将Y12输出口置0

/*mpc004S芯片轴同步指令配合等待轴脉冲指令完成绕线的过程示例。编码器输入口接主轴电机编

码器,1轴为排线电机。*/

for(i=0;i10;i++)//绕10层

{

set_synchro(1,7,1,1,8,1);/*编码器1口为主轴,1轴排线与其同步,编码器1圈为1600个

脉冲,1轴移一个线宽为200脉冲,比率为8比1。*/

wait_pulse(1,7,160000);//等待编码器脉冲为160000,即转了100圈。

set_synchro(1,7,1,1,8,-1);//将比率设为8比-1。即让1轴反转排线。

wait_pulse(1,7,320000);//等待编码器脉冲为320000,即又转了100圈。

set_command_pos(1,7,0);//将编码器脉冲清为0。

write_bit(1,100,i);

//每绕一层将层号写到模块,单片机可随时读出已绕到哪层。

}

while(!s1);

}

}

}

分享到:
上一篇

已经是第一篇了

下一篇

已经是最后一篇了