通过51单片机控制mpc014运动控制模块的程序,完整电路图和程序工程可到官网http://www.mpc000.com下载。
#include <intrins.h>
#include <reg52.h>
//MCU: stc8f2k08s2 运动模块:http://www.mpc000.com
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
sfr P5 = 0xC8;
sfr SPSTAT = 0xcd;
sfr SPCTL = 0xce;
sfr SPDAT = 0xcf;
sfr IE2 = 0xaf;
sfr AUXR = 0x8e;
sfr T2H = 0xd6;
sfr T2L = 0xd7;
sfr P_SW2 = 0xba;
#define CKSEL (*(unsigned char volatile xdata *)0xfe00)
#define CKDIV (*(unsigned char volatile xdata *)0xfe01)
#define IRC24MCR (*(unsigned char volatile xdata *)0xfe02)
#define XOSCCR (*(unsigned char volatile xdata *)0xfe03)
#define IRC32KCR (*(unsigned char volatile xdata *)0xfe04)
#define FOSC 16000000UL //使用外部16M晶振
#define BRT (65536 - FOSC / 115200 / 4) //定义115200波特率
sbit b1 = P5^5;
sbit led = P3^5;
sbit cs3 = P3^3;
sbit cs2 = P3^2;
sbit cs1 = P1^2;
sbit sck = P1^5;
sbit in = P1^4;
sbit out = P1^3;
#define SPI3_CSHIGH cs3=1 // CS3
#define SPI3_CSLOW cs3=0
#define SPI2_CSHIGH cs2=1 // CS2
#define SPI2_CSLOW cs2=0
#define SPI1_CSHIGH cs1=1 // CS1
#define SPI1_CSLOW cs1=0
#define SPI_SCKHIGH sck=1 //SCK
#define SPI_SCKLOW sck=0
#define SPI_OUTHIGH out=1
#define SPI_OUTLOW out=0//MOSI
#define SPI_IN in//MISO
unsigned char inbuf[50];
unsigned char b1_state=0;
void initial()
{
P1M1 = 0;
P1M0 = 0x2c; // 引脚模拟通信时,MOSI,SCK, CS 设为推挽输出
SPI1_CSHIGH; //CS不使用时设为高
SPI2_CSHIGH;
SPI3_CSHIGH;
SPI_SCKLOW;//SCK空闲状态一定要为低电平。
}
void init_uart()
{
SCON = 0x50;
T2L = BRT;
T2H = BRT >> 8;
AUXR = 0x15;
}
/*
串口发送一个字节。
*/
void USART_Txbyte(unsigned char i)
{
SBUF = i;
while(TI ==0);
TI = 0;
}
/*
串口发送一串数据。
*/
void USRAT_transmit(unsigned char *fdata,unsigned char len)
{
unsigned char i;
for(i=0;i<len;i++)
{
USART_Txbyte(fdata[i]);
}
}
void delay_nus(unsigned long n)
{
unsigned long j;
while(n--)
{
j=1;
while(j--);
}
}
//延时n ms
void delay_nms(unsigned long n)
{
while(n--)
delay_nus(1000);
}
/*
函数名: SPI_SendData
功能:软件模拟SPI通讯发送并接收一个8位字节数据。
如需使用硬件SPI,单片机作为主机,mpc014为从机。CPHA=0,CPOL=0,高位在前,SPI数据宽度为8位。
空闲状态下单片机SCK引脚必须为低电平。每一条指令开始发送前将CS引脚置低,整条指令发送完成后必须将CS置高。
每条指令间需有时间间隔,推荐延时1MS以上。
*/
unsigned char SPI_SendData(unsigned char outdata)
{
unsigned char RecevieData=0,i;
SPI_SCKLOW;
// _nop_(); _nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
for(i=0;i<8;i++)
{
SPI_SCKLOW;
_nop_(); _nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
if(outdata&0x80)
{
SPI_OUTHIGH;
}
else
{
SPI_OUTLOW;
}
outdata<<=1;
_nop_(); _nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
SPI_SCKHIGH; //
RecevieData <<= 1;
if(SPI_IN)
{
RecevieData |= 1;
}
_nop_(); _nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();
SPI_SCKLOW;
}
return RecevieData;
}
/*
函数名: enabled_cs
功能:mpc013,mpc014串口SPI运动控制模块使能对应芯片模块的CS脚
参数:
cardno 卡号
用单片机不同引脚去控制不同芯片的CS脚,以便多个芯片模块关联使用。
*/
void enabled_cs(unsigned char cardno)
{
if(cardno==1)
{
SPI1_CSLOW;
}
if(cardno==2)
{
SPI2_CSLOW;
}
if(cardno==3)
{
SPI3_CSLOW;
}
}
/*
函数名: disabled_cs
功能:mpc013,mpc014串口SPI运动控制模块禁止对应芯片模块的CS脚
参数:
cardno 卡号
用单片机不同引脚去控制不同芯片的CS脚,以便多个芯片关联使用。
*/
void disabled_cs(unsigned char cardno)
{
if(cardno==1)
{
SPI1_CSHIGH;
}
if(cardno==2)
{
SPI2_CSHIGH;
}
if(cardno==3)
{
SPI3_CSHIGH;
}
}
/*
函数名: set_speed
功能:mpc013,mpc014串口SPI运动控制模块设置轴速度
参数:
cardno 卡号
acc 加减速:值*倍率(Hz/s2)
speed 运行频率为:值*倍率(Hz)
range 倍率(1-100)
*/
void set_inp_speed(unsigned char cardno ,unsigned int acc ,unsigned int speed ,unsigned char range)
{
unsigned char OutByte[25];
OutByte[0] = 1;
OutByte[1] = acc >>8;
OutByte[2] = acc ;
OutByte[3] = speed >>8;
OutByte[4] = speed ;
OutByte[5] = range;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: pmove
功能:mpc013模块单轴运行 ,仅独立轴可用。
参数:
cardno 卡号
axis 轴号(1,2,3)
pulse 输出的脉冲数 >0:正方向移动 <0:负方向移动 范围(-268435455~+268435455)
mode 0:绝对位移 1:相对位移
*/
void pmove(unsigned char cardno ,unsigned char axis,long pulse , unsigned char mode)
{
unsigned char OutByte[25];
OutByte[0] = 2 ;
OutByte[1] = axis;
OutByte[2] = pulse >>24;
OutByte[3] = pulse >>16;
OutByte[4] = pulse >>8;
OutByte[5] = pulse ;
OutByte[6] = mode ;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
disabled_cs(cardno);
delay_nms(1) ;
}
/*
函数名: set_command_pos
功能: mpc013,mpc014串口SPI运动控制模块设置轴逻辑位置
参数:
cardno 卡号
axis 轴号(1,2,3,4)
pulse 位置脉冲数,范围(-268435455~+268435455)
*/
void set_command_pos(unsigned char cardno ,unsigned char axis, long value )
{
unsigned char OutByte[25];
OutByte[0] = 0x12 ;
OutByte[1] = axis ;
OutByte[2] = value >>24;
OutByte[3] = value >>16;
OutByte[4] = value >>8;
OutByte[5] = value ;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: sudden_stop
功能: mpc013,mpc014串口SPI运动控制模块轴立即停止
参数:
cardno 卡号
axis 停止的轴号(1,2,3,6) 1,2,3:独立轴1-3轴停 6,插补模块所有轴停止。
*/
void sudden_stop(unsigned char cardno ,unsigned char axis)
{
unsigned char OutByte[25];
OutByte[0] = 0x17 ;
OutByte[1] = axis ;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: set_special
功能:mpc014串口SPI运动控制模块设置特别功能
参数:
cardno 卡号
value
0xfb 清除缓存
0xfc 缓存插补运动暂停
0xfd 取消缓存插补暂停
*/
void set_special(unsigned char cardno,unsigned char value)
{
unsigned char OutByte[25];
OutByte[0] = 0xFA ;
OutByte[1] = value;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: get_inp_state
功能: mpc013,mpc014串口SPI运动控制模块获取轴状态,限位状态,缓存剩余量,各轴逻辑位置。
参数:
cardno 卡号
amount 获取字节数量。 设为20将取全部数据。
inbuf[] 读取的数据存放的数组
*/
void get_inp_state( unsigned char cardno, unsigned char amount,unsigned char inbuf[])
{
unsigned char OutByte[25];
char i;
enabled_cs(cardno);
for(i=0;i<amount;i++)
{
inbuf[i]=SPI_SendData(0);
}
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_move1
功能:mpc014串口SPI运动控制模块一轴直线插补
参数:
cardno 卡号
no1 X轴轴号
pulse1 X轴移动的距离,范围(-8388608~+8388607)
mode 0:绝对位移 1:相对位移
*/
void inp_move1(unsigned char cardno,unsigned char no1 , long pulse1 ,unsigned char mode )
{
unsigned char OutByte[25];
OutByte[0] = 0x7;
OutByte[1] = no1;
OutByte[2] = pulse1>>24;
OutByte[3] = pulse1 >>16;
OutByte[4] = pulse1>> 8;
OutByte[5] = pulse1;
OutByte[6] = mode;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_move2
功能:mpc014串口SPI运动控制模块二轴直线插补
参数:
cardno 卡号
no1 X轴轴号
no2 Y轴轴号
pulse1,pulse2 X-Y轴移动的距离,范围(-8388608~+8388607)
mode 0:绝对位移 1:相对位移
*/
void inp_move2(unsigned char cardno,unsigned char no1 ,unsigned char no2 , long pulse1 ,long pulse2 ,unsigned char mode )
{
unsigned char OutByte[25];
OutByte[0] = 0x8;
OutByte[1] = no1;
OutByte[2] = no2;
OutByte[3] = pulse1>>24;
OutByte[4] = pulse1 >>16;
OutByte[5] = pulse1>> 8;
OutByte[6] = pulse1;
OutByte[7] = pulse2 >>24;
OutByte[8] = pulse2 >>16;
OutByte[9] = pulse2 >>8;
OutByte[10] = pulse2 ;
OutByte[11] = mode;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
SPI_SendData(OutByte[7]);
SPI_SendData(OutByte[8]);
SPI_SendData(OutByte[9]);
SPI_SendData(OutByte[10]);
SPI_SendData(OutByte[11]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_move3
功能:mpc014串口SPI运动控制模块三轴直线插补
参数:
cardno 卡号
no1 X轴轴号
no2 Y轴轴号
no3 Z轴轴号
pulse1,pulse2,pulse3 X-Y-Z轴移动的距离,范围(-8388608~+8388607)
mode 0:绝对位移 1:相对位移
*/
void 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];
OutByte[0] = 0x9;
OutByte[1] = no1;
OutByte[2] = no2;
OutByte[3] = no3;
OutByte[4] = pulse1>>24;
OutByte[5] = pulse1 >>16;
OutByte[6] = pulse1>> 8;
OutByte[7] = pulse1;
OutByte[8] = pulse2 >>24;
OutByte[9] = pulse2 >>16;
OutByte[10] = pulse2 >>8;
OutByte[11] = pulse2 ;
OutByte[12] = pulse3 >>24;
OutByte[13] = pulse3 >>16;
OutByte[14] = pulse3 >>8;
OutByte[15] = pulse3 ;
OutByte[16] = mode;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
SPI_SendData(OutByte[7]);
SPI_SendData(OutByte[8]);
SPI_SendData(OutByte[9]);
SPI_SendData(OutByte[10]);
SPI_SendData(OutByte[11]);
SPI_SendData(OutByte[12]);
SPI_SendData(OutByte[13]);
SPI_SendData(OutByte[14]);
SPI_SendData(OutByte[15]);
SPI_SendData(OutByte[16]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_move4
功能:mpc014串口SPI运动控制模块四轴直线插补
参数:
cardno 卡号
no1 X轴轴号
no2 Y轴轴号
no3 Z轴轴号
no4 E轴轴号
pulse1,pulse2,pulse3,pulse4 X-Y-Z-E轴移动的距离,范围(-8388608~+8388607)
mode 0:绝对位移 1:相对位移
*/
void inp_move4(unsigned char cardno,unsigned char no1 ,unsigned char no2 ,unsigned char no3 ,unsigned char no4, long pulse1 ,long pulse2 ,long pulse3 ,long pulse4 ,unsigned char mode )
{
unsigned char OutByte[25];
OutByte[0] = 0xa;
OutByte[1] = no1;
OutByte[2] = no2;
OutByte[3] = no3;
OutByte[4] = no4;
OutByte[5] = pulse1>>24;
OutByte[6] = pulse1 >>16;
OutByte[7] = pulse1>> 8;
OutByte[8] = pulse1;
OutByte[9] = pulse2 >>24;
OutByte[10] = pulse2 >>16;
OutByte[11] = pulse2 >>8;
OutByte[12] = pulse2 ;
OutByte[13] = pulse3 >>24;
OutByte[14] = pulse3 >>16;
OutByte[15] = pulse3 >>8;
OutByte[16] = pulse3 ;
OutByte[17] = pulse4 >>24;
OutByte[18] = pulse4 >>16;
OutByte[19] = pulse4 >>8;
OutByte[20] = pulse4 ;
OutByte[21] = mode;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
SPI_SendData(OutByte[7]);
SPI_SendData(OutByte[8]);
SPI_SendData(OutByte[9]);
SPI_SendData(OutByte[10]);
SPI_SendData(OutByte[11]);
SPI_SendData(OutByte[12]);
SPI_SendData(OutByte[13]);
SPI_SendData(OutByte[14]);
SPI_SendData(OutByte[15]);
SPI_SendData(OutByte[16]);
SPI_SendData(OutByte[17]);
SPI_SendData(OutByte[18]);
SPI_SendData(OutByte[19]);
SPI_SendData(OutByte[20]);
SPI_SendData(OutByte[21]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_arc
功能:mpc014串口SPI运动控制模块二轴圆弧插补
参数:
cardno 卡号
no1 参与插补X轴的轴号
no2 参与插补Y轴的轴号
x,y 圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607)
i,j 圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607)
mode1 0:逆时针插补 1:顺时针插补
mode2 0:绝对位移 1:相对位移
*/
void inp_arc(unsigned char cardno ,unsigned char no1,unsigned char no2, long x , long y, long i, long j,unsigned char mode1,unsigned char mode2 )
{
unsigned char OutByte[25];
OutByte[0] = 0xc;
OutByte[1] = no1;
OutByte[2] = no2;
OutByte[3] = x >>24;
OutByte[4] = x >>16;
OutByte[5] = x >>8;
OutByte[6] = x ;
OutByte[7] = y >>24;
OutByte[8] = y >>16;
OutByte[9] = y >>8;
OutByte[10] = y ;
OutByte[11] = i >>24;
OutByte[12] = i >>16;
OutByte[13] = i >>8;
OutByte[14] = i ;
OutByte[15] = j >>24;
OutByte[16] = j >>16;
OutByte[17] = j >>8;
OutByte[18] = j ;
OutByte[19] = mode1;
OutByte[20] = mode2;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
SPI_SendData(OutByte[7]);
SPI_SendData(OutByte[8]);
SPI_SendData(OutByte[9]);
SPI_SendData(OutByte[10]);
SPI_SendData(OutByte[11]);
SPI_SendData(OutByte[12]);
SPI_SendData(OutByte[13]);
SPI_SendData(OutByte[14]);
SPI_SendData(OutByte[15]);
SPI_SendData(OutByte[16]);
SPI_SendData(OutByte[17]);
SPI_SendData(OutByte[18]);
SPI_SendData(OutByte[19]);
SPI_SendData(OutByte[20]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_helical
功能:mpc014串口SPI运动控制模块圆弧螺旋插补
参数:
cardno 卡号
no1 参与插补X轴的轴号
no2 参与插补Y轴的轴号
no3 参与插补螺旋轴的轴号
x,y 圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607)
z 参与插补螺旋轴的位置(相对于起点)
i,j 圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607)
mode1 0:逆时针插补 1:顺时针插补
mode2 0:绝对位移 1:相对位移
*/
void inp_helical(unsigned char cardno ,unsigned char no1,unsigned char no2,unsigned char no3,long x , long y,long z, long i, long j,unsigned char mode1,unsigned char mode2 )
{
unsigned char OutByte[30];
OutByte[0] = 0xd;
OutByte[1] = no1;
OutByte[2] = no2;
OutByte[3] = no3;
OutByte[4] = x >>24;
OutByte[5] = x >>16;
OutByte[6] = x >>8;
OutByte[7] = x ;
OutByte[8] = y >>24;
OutByte[9] = y >>16;
OutByte[10] = y >>8;
OutByte[11] = y ;
OutByte[12] = z >>24;
OutByte[13] = z >>16;
OutByte[14] = z >>8;
OutByte[15] = z ;
OutByte[16] = i >>24;
OutByte[17] = i >>16;
OutByte[18] = i >>8;
OutByte[19] = i ;
OutByte[20] = j >>24;
OutByte[21] = j >>16;
OutByte[22] = j >>8;
OutByte[23] = j ;
OutByte[24] = mode1;
OutByte[25] = mode2;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
SPI_SendData(OutByte[3]);
SPI_SendData(OutByte[4]);
SPI_SendData(OutByte[5]);
SPI_SendData(OutByte[6]);
SPI_SendData(OutByte[7]);
SPI_SendData(OutByte[8]);
SPI_SendData(OutByte[9]);
SPI_SendData(OutByte[10]);
SPI_SendData(OutByte[11]);
SPI_SendData(OutByte[12]);
SPI_SendData(OutByte[13]);
SPI_SendData(OutByte[14]);
SPI_SendData(OutByte[15]);
SPI_SendData(OutByte[16]);
SPI_SendData(OutByte[17]);
SPI_SendData(OutByte[18]);
SPI_SendData(OutByte[19]);
SPI_SendData(OutByte[20]);
SPI_SendData(OutByte[21]);
SPI_SendData(OutByte[22]);
SPI_SendData(OutByte[23]);
SPI_SendData(OutByte[24]);
SPI_SendData(OutByte[25]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: wait_stop
功能:mpc013模块等待轴停止
参数:
cardno 卡号
axis 需要等待停止的轴号(1,2,3,6) 1,2,3:独立轴1-3轴 6,插补模块所有轴
*/
void wait_stop(unsigned char cardno ,unsigned char axis)
{
unsigned char OutByte[25];
OutByte[0] = 0x0f ;
OutByte[1] = axis ;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: write_bit
功能:mpc013,mpc014串口SPI运动控制模块写输出口状态
参数:
cardno 卡号
number 端口号(0-2) Y0-Y2
value 状态(0,1) 0 输出低电平 1 输出高电平
*/
void write_bit(unsigned char cardno , unsigned char number, unsigned char value)
{
unsigned char OutByte[25];
OutByte[0] = 0x03 ;
OutByte[1] = number;
OutByte[2] = value;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: wait_delay
功能:mpc013,mpc014串口SPI运动控制模块等待延时数
参数:
cardno 卡号
value 延时量(1-10000)MS
*/
void wait_delay(unsigned char cardno ,unsigned int value)
{
unsigned char OutByte[25];
OutByte[0] = 0x0e ;
OutByte[1] = value>>8;
OutByte[2] = value;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
disabled_cs(cardno);
delay_nms(1);
}
void main(void)
{
initial();
init_uart();
// ES = 1;
// EA = 1;
P_SW2 = 0x80;
XOSCCR = 0xc0; //启动外部晶振
while (!(XOSCCR & 1)); //等待时钟稳定
CKDIV = 0x00; //时钟不分频
CKSEL = 0x01; //选择外部晶振
P_SW2 = 0x00;
led=0;
delay_nms(10) ;
/*独立运行轴1,2,3轴回原点,更多方式回原点可自由组合。mpc013运动控制模块专用
set_inp_speed(1 ,200,300,100); //设加速度50000Hz/s2, 运行速度30000Hz
pmove(1,1,-1000000,1); // 1轴多脉冲负方向运动,碰到原点限位自动停止
pmove(1,2,-1000000,1); // 2轴多脉冲负方向运动 ,碰到原点限位自动停止
pmove(1,3,-1000000,1); // 2轴多脉冲负方向运动,碰到原点限位自动停止
do
{
get_inp_state( 1, 2,inbuf); //只需读出2个字节来判断轴状态
}
while(inbuf[1]); //inbuf[1]数据为0表示所有轴都停
set_command_pos(1 ,1,0); //设1轴坐标
set_command_pos(1 ,2,0); //设2轴坐标
set_command_pos(1 ,3,0); //设3轴坐标
*/
/*插补运行轴1,2,3,4轴回原点,更多方式回原点可自由组合。mpc014串口SPI运动控制模块专用
set_inp_speed(1 ,200,300,100); //设加速度50000Hz/s2, 运行速度30000Hz
inp_move4(1,1,2,3,4 ,-1000000,-1000000,-1000000,-1000000 ,1); // 4轴多脉冲插补负方向运动,碰到原点限位自动停止
do
{
get_inp_state( 1, 2,inbuf); //只需读出2个字节来判断轴状态
}
while(inbuf[1]); //inbuf[1]数据为0表示所有轴都停
set_command_pos(1 ,1,0); //设1轴坐标
set_command_pos(1 ,2,0); //设2轴坐标
set_command_pos(1 ,3,0); //设3轴坐标
set_command_pos(1 ,4,0); //设3轴坐标
*/
while(1)
{
if((!b1)&&(!b1_state))
{
delay_nms(10);
if((!b1)&&(!b1_state))
{
led=0;
b1_state=1;
set_inp_speed(1 ,300,300,100);
/*下面的指令会直接发到缓存区自动排队运行。mpc014串口SPI运动控制模块专用*/
inp_move2(1,2 ,1 ,6400 ,32000 ,1);
wait_delay(1 ,3000); //模块内部指令间延时3S
write_bit(1 , 0, 1);
inp_move2(1,2 ,1 ,6400 ,32000 ,1);
wait_delay(1 ,3000);
inp_move2(1,2 ,1 ,6400 ,32000 ,1); //2轴直线插补
inp_move1(1,3 ,10 ,1) ;
inp_move2(1,2 ,1 ,6400 ,32000 ,1);
inp_move2(1,2 ,1 ,6400 ,32000 ,1);
inp_move3(1,2,3,1 ,3000000,3000000 ,3000000 ,1); // 3轴直线插补
wait_delay(1 ,1000);
inp_move2(1,1 ,2 ,1000 ,1000 ,1);
write_bit(1 , 0, 1); // 0输出口为高
inp_move1(1,1 ,1000 ,1) ; // 1轴运行
inp_arc(1 ,2,3, -200, 200, -200, 0,0,1) ; // 2轴圆弧插补
/*上面的指令会直接发到缓存区自动排队运行。mpc014串口SPI运动控制模块专用*/
// set_inp_speed(1 ,5000,5000,100);
// pmove(1,1,1000, 1); // pmove只有mpc013能用
// wait_stop(1 ,1);
// pmove(1,2,-100, 1);
while(!b1);
}
}
else
led=1;
if((!b1)&&(b1_state))
{
delay_nms(10) ;
if((!b1)&&(b1_state))
{
led=0;
//sudden_stop(1,1); // 立即停止独立轴1轴 ,只适用于mpc013
//sudden_stop(1,6); // 立即停止所有插补轴
get_inp_state( 1, 20,inbuf); //读出20个字节数据放入数组
USRAT_transmit(inbuf,20); // 串口将数组数据发送出去查看
// if(inbuf[1]==0) //inbuf[1]数据为0表示所有轴都停
//set_special(1,0xfd);
// else
// set_special(1,0xfc);
//b1_state=0;
while(!b1);
}
}
}
}
串口G代码控制
Mpc014g模块可通过串口 G代码控制模块,串口通讯速率为115200bps,数据位为8位,停止位1位,无校验。
模块通讯协议完全兼容标准G代码,G代码详细的格式与用法请参考标准文档。上电芯片会主动发送字符“>”,工作时上位机每发送一条指令,会回复字符“ok”,发 M114会回复坐标。上位机只有当收到回复后,才可发送下一条指令。目前支持如下指令:
G0-快速定位 坐标,单位mm。速度F表示mm/s。
如:G0 X10 Y10 F30000
G1 - 直线插补 世界坐标,单位mm。
如:G1 X10 Y10 F30000
G2 - 正圆插补 X,Y为终点坐标,I,J为圆心相对起点坐标。
如:G2 X-20 Y20 I-20 J0 F30000
G3 - 逆圆插补
如:G2 X-20 Y20 I-20 J0 F30000
G90 - 绝对坐标
如:G90
G91 - 相对坐标
如:G91
G92 - 设置坐标
如:G92 X0 Y0
M92 - 脉冲系数 每MM多少脉冲,模块默认系数为1。
如:M92 X200 Y200 Z200 E200
M109 - 等待延时,单位MS
如:M109 S2000
M110 - 端口输出 ,T为端口号,Z为值
如:M110 T6 Z1
M112 -急停 ,停所有轴,并清空缓存
如:M112
M114 -读取当前坐标 ,返回轴坐标
如:M114
M115返回轴状态
如:M115
M116返回剩余缓存数量
如:M116
M204 - 设置加速度S,单位mm/s2。
如:M204 S5000
此内容由EEWORLD论坛网友w75815895原创,如需转载或用于商业用途需征得作者同意并注明出处
|