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

1.引言

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

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

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

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

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

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

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

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

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

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

◆ 每轴一路硬件回原点。

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

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

硬件系统由四部分构成:

(1) 单片机部分

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

单片机控制步进电机或伺服电机(单片机控制多轴步进电机)(1)

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

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

单片机控制步进电机或伺服电机(单片机控制多轴步进电机)(2)

3)原点信号输入部分

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

单片机控制步进电机或伺服电机(单片机控制多轴步进电机)(3)

4) 信号输出部分

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

单片机控制步进电机或伺服电机(单片机控制多轴步进电机)(4)

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

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

单片机控制步进电机或伺服电机(单片机控制多轴步进电机)(5)

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

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

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

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

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

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

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

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

试验程序如下:

#include <reg52.h>

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

sfr IPH =0XB7;

sfr CCON =0XD8;

sfr CMOD =0XD9;

sfr CL =0XE9;

sfr CH =0XF9;

sfr CCAP0L =0XEA;

sfr CCAP0H =0XFA;

sfr CCAPM0 =0XDA;

sfr CCAPM1 =0XDB;

sfr P3M1= 0XB1;

sfr P3M0= 0XB2;

sfr P1M1= 0X91;

sfr P1M0= 0X92;

sfr WAKE_CLKO= 0X8f;

sfr BRT =0x9c;

sfr AUXR =0x8E;

sfr AUXR1 = 0xA2;

sfr WDT_CONTR = 0xc1;

sfr T2MOD = 0xC9;

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

sbit busy = P3^2;

sbit s1=P3^7;

void initial()

{

P3M1 = 0x00;

P3M0 = 0x80;

P1M1 = 0x00;

P1M0 = 0xf9;

}

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

{

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

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

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

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

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

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

} */

void init_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 开启

}

/*

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

*/

void USART_Txbyte(unsigned char i)

{

SBUF = i;

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

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

}

/*

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

*/

void receive(unsigned char *buf)

{

unsigned char i;

for(i=0;i<10;i )

{

while(RI==0);

RI=0;

buf[i]=SBUF;

}

}

/*

串口发送一串数据。

*/

void USRAT_transmit(unsigned char *fdata,unsigned char len)

{

unsigned char i;

for(i=0;i<len;i )

{

USART_Txbyte(fdata[i]);

}

}

/*

函数名: inp_move

功能:二轴直线插补

参数:

cardno 卡号

no1 X 轴轴号

no2 Y 轴轴号

pulse1,pulse2

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

-8388608~ 8388607)

mode 0:相对位移 1:绝对位移

返回值:

0 失败

1 成功

*/

unsigned char inp_move(unsigned char cardno,unsigned char no1 ,unsigned char no2 , long pulse1 ,long

pulse2 ,unsigned char mode )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0x0F;

OutByte[2] = cardno;

OutByte[3] = 0x7;

OutByte[4] = no1;

OutByte[5] = no2;

OutByte[6] = pulse1>>24;

OutByte[7] = pulse1 >>16;

OutByte[8] = pulse1>> 8;

OutByte[9] = pulse1;

OutByte[10] = pulse2 >>24;

OutByte[11] = pulse2 >>16;

OutByte[12] = pulse2 >>8;

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);

return 1;

}

/*

函数名: inp_arc

功能:二轴圆弧插补

参数:cardno 卡号

no1参与插补 X 轴的轴号

no2参与插补 Y 轴的轴号

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

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

mode 0:顺时针插补 1:逆时针插补

返回值:

0 失败 1 成功

*/

unsigned char inp_arc(unsigned char cardno ,unsigned char no1,unsigned char no2, long X , long y, long i, long j,unsigned char mode )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0x17;

OutByte[2] = cardno;

OutByte[3] = 0x8;

OutByte[4] = no1;

OutByte[5] = no2;

OutByte[6] = X >>24;

OutByte[7] = X >>16;

OutByte[8] = X >>8;

OutByte[9] = X ;

OutByte[10] = y >>24;

OutByte[11] = y >>16;

OutByte[12] = y >>8;

OutByte[13] = y ;

OutByte[14] = i >>24;

OutByte[15] = i >>16;

OutByte[16] = i >>8;

OutByte[17] = i ;

OutByte[18] = j >>24;

OutByte[19] = j >>16;

OutByte[20] = j >>8;

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);

return 1;

}

/*

函数名: set_speed

功能:设置轴速度

参数:

cardno 卡号

axis 轴号(1-6)

acc 加速时间(ms)

dec 减速时间(ms)

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

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

range 频率倍率(1-100)

返回值:

0 失败 1 成功

*/

unsigned char set_speed(unsigned char cardno ,unsigned char axis ,unsigned int acc ,unsigned int

dec ,unsigned int startv ,unsigned int speed ,unsigned char range)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0xe;

OutByte[2] = cardno;

OutByte[3] = 1;

OutByte[4] = axis;

OutByte[5] = acc >>8;

OutByte[6] = acc ;

OutByte[7] = dec >>8;

OutByte[8] = dec ;

OutByte[9] = startv >>8;

OutByte[10] = startv ;

OutByte[11] = speed >>8;

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);

return 1;

}

/*

函数名: set_soft_limit

功能:设置轴软件限位

参数:

cardno 卡号

axis

轴号(1-6)

mode 0:解除软件限位 1:启用软件限位

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

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

0~ 268435455)

返回值:

0 失败 1 成功

*/

unsigned char set_soft_limit(unsigned char cardno ,unsigned char axis ,unsigned char mode, long pulse1 , long

pulse2 )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0xE;

OutByte[2] = cardno ;

OutByte[3] = 0x13;

OutByte[4] = axis;

OutByte[5] = mode;

OutByte[6] = pulse1 >>24;

OutByte[7] = pulse1 >>16;

OutByte[8] = pulse1 >>8;

OutByte[9] = pulse1 ;

OutByte[10] = pulse2 >>24;

OutByte[11] = pulse2 >>16;

OutByte[12] = pulse2 >>8;

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);

return 1;

}

/*

函数名: set_hard_limit

功能:设置轴硬件限位

参数:

cardno 卡号

axis 轴号(1-6)

mode 0:解除硬件限位 1:启用硬件限位

dir 0:反方向 1:正方向

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

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

返回值:

0 失败 1 成功

*/

unsigned char set_hard_limit(unsigned char cardno ,unsigned char axis ,unsigned char mode, unsigned char dir,

unsigned char number, unsigned char value)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: pmove

功能:单轴运行

参数:

cardno 卡号

axis 轴号(1-6)

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

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

mode 0:相对位移 1:绝对位移 2:连续位移

返回值:

0 失败 1 成功

*/

unsigned char pmove(unsigned char cardno ,unsigned char axis,long pulse , unsigned char mode)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0xA ;

OutByte[2] = cardno;

OutByte[3] = 2

;

OutByte[4] = axis;

OutByte[5] = pulse >>24;

OutByte[6] = pulse >>16;

OutByte[7] = pulse >>8;

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);

return 1;

}

/*

函数名: wait_delay

功能:等待延时数

参数:

cardno 卡号

value 延时量(1-10000)MS

返回值:

0 失败 1 成功

*/

unsigned char wait_delay(unsigned char cardno ,unsigned int value)

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68 ;

OutByte[1] = 0x6 ;

OutByte[2] = cardno ;

OutByte[3] = 0xE ;

OutByte[4] = value >>8;

OutByte[5] = value ;

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

USRAT_transmit(OutByte,7);

receive(inbuf);

return 1;

}

/*

函数名: set_command_pos

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

参数:

cardno 卡号

axis 轴号(1-8)

pulse

位置脉冲数,范围(

-268435455~ 268435455)

返回值:

0 失败 1 成功

*/

unsigned char set_command_pos(unsigned char cardno ,unsigned char axis, long value )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68 ;

OutByte[1] = 0x9 ;

OutByte[2] = cardno ;

OutByte[3] = 0x12 ;

OutByte[4] = axis ;

OutByte[5] = value >>24;

OutByte[6] = value >>16;

OutByte[7] = value >>8;

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);

return 1;

}

/*

函数名: write_bit

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

参数:

cardno 卡号

number 端口号(

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

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

返回值:

0 失败 1 成功

*/

unsigned char write_bit(unsigned char cardno , unsigned char number, unsigned char value)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: read_bit

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

参数:

cardno 卡号

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

返回值:

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

*/

unsigned char read_bit(unsigned char cardno ,unsigned char number)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return inbuf[5];

}

/*

函数名: sudden_stop

功能: 轴停止

参数:

cardno 卡号

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

返回值:

0 失败 1 成功

*/

unsigned char sudden_stop(unsigned char cardno ,unsigned char axis)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: wait_in

功能: 等待输入口状态

参数:

cardno 卡号

number 端口号(

0-9) X0-X9

value 状态(

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

返回值:

0 失败 1 成功

*/

unsigned char wait_in( unsigned char cardno, unsigned char number, unsigned char value)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: wait_stop

功能:等待轴停止

参数:

cardno 卡号

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

返回值:

0 失败 1 成功

*/

unsigned char wait_stop(unsigned char cardno ,unsigned char axis)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: get_number

功能:获取唯一序列号

参数:

cardno 卡号

返回值: 32 位序列号

*/

unsigned long get_number(unsigned char cardno )

{

unsigned long tmp=0;

unsigned char OutByte[25];

unsigned char inbuf[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= (unsigned long)inbuf[4]<<24;

tmp = (unsigned long)inbuf[5]<<16;

tmp = (unsigned long)inbuf[6]<<8;

tmp = (unsigned long)inbuf[7];

return tmp;

//return(((unsigned long)inbuf[4]<<24) ((unsigned long)inbuf[5]<<16) ((unsigned long)inbuf[6]<<8) ((unsigned

//long)inbuf[7]));

}

/*

函数名: get_status

功能:获取各轴工作状态

参数:

cardno 卡号

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

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

*/

unsigned char get_status( unsigned char cardno)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return inbuf[4];

}

/*

函数名: get_command_pos

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

参数:

cardno 卡号

axis 轴号

返回值:

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

*/

long get_command_pos( unsigned char cardno, unsigned char axis)

{

long tmp=0;

unsigned char OutByte[25];

unsigned char inbuf[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];

return tmp;

//return(((unsigned long)inbuf[5]<<24) ((unsigned long)inbuf[6]<<16) ((unsigned long)inbuf[7]<<8) ((unsigned

//long)inbuf[8]));

}

/*

函数名: set_cardno

功能:设置卡号

参数:

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

返回值:

0 失败 1 成功

*/

unsigned char set_cardno(unsigned char cardno)

{

unsigned char OutByte[25];

unsigned char inbuf[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);

return 1;

}

/*

函数名: read_fifo_count

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

参数:

cardno 卡号

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

*/

unsigned int read_fifo_count ( unsigned char cardno)

{

unsigned char OutByte[25];

unsigned char inbuf[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 (((unsigned int)inbuf[4]<<8) inbuf[5]);

}

/*

函数名: wait_pulse

功能:等待轴脉冲数

参数:

cardno 卡号

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

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

返回值:

0 失败 1 成功

*/

unsigned char wait_pulse(unsigned char cardno ,unsigned char axis, long value )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0x9 ;

OutByte[2] = cardno ;

OutByte[3] = 0x19;

OutByte[4] = axis ;

OutByte[5] = value >>24;

OutByte[6] = value >>16;

OutByte[7] = value >>8;

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);

return 1;

}

/*

函数名: set_synchro

功能:设置轴同步

参数:

cardno 卡号

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

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

mode 方式 0 :关闭 1: 打开

pulse1 主轴脉冲数

pulse2 随动轴脉冲数

返回值:

0 失败 1 成功

*/

unsigned char set_synchro(unsigned char cardno ,unsigned char no1 ,unsigned char no2,unsigned char mode, int

pulse1 ,int pulse2 )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0xb;

OutByte[2] = cardno;

OutByte[3] = 0x22;

OutByte[4] = no1;

OutByte[5] = no2;

OutByte[6] = mode;

OutByte[7] = pulse1 >>8;

OutByte[8] = pulse1 ;

OutByte[9] = pulse2 >>8;

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);

return 1;

}

/*

函数名: inp_move3

功能:三轴直线插补

参数:

cardno 卡号

no1 X 轴轴号

no2 Y 轴轴号

no3 Z 轴轴号

pulse1,pulse2,pulse3

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

-8388608~ 8388607)

mode 0:相对坐标 1:绝对坐标

返回值:

0 失败 1 成功

*/

unsigned char inp_move3(unsigned char cardno,unsigned char no1 ,unsigned char no2,unsigned char no3, long

pulse1 ,long pulse2,long pulse3 ,unsigned char mode )

{

unsigned char OutByte[25];

unsigned char inbuf[12];

OutByte[0] = 0x68;

OutByte[1] = 0x14;

OutByte[2] = cardno;

OutByte[3] = 0x21;

OutByte[4] = no1;

OutByte[5] = no2;

OutByte[6] = no3;

OutByte[7] = pulse1>>24;

OutByte[8] = pulse1 >>16;

OutByte[9] = pulse1>> 8;

OutByte[10] = pulse1;

OutByte[11] = pulse2 >>24;

OutByte[12] = pulse2 >>16;

OutByte[13] = pulse2 >>8;

OutByte[14] = pulse2 ;

OutByte[15] = pulse3 >>24;

OutByte[16] = pulse3 >>16;

OutByte[17] = pulse3 >>8;

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);

return 1;

}

void main(void)

{

char i;

//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;i<10;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);

}

}

}

,