电子说
#include
#include
//MCU: stc8f2k08s2
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 FOSC 24000000UL //使用内部24M晶振
#define BRT (65536 - FOSC / 115200 / 4) //定义115200波特率
sbit b2 = P1^1;
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] = {0};
unsigned char b1_state=0;
long sxnum;
void initial()
{
P1M1 = 0;
P1M0 = 0x2c; // 引脚模拟通信时,MOSI,SCK, CS 设为推挽输出
SPI1_CSHIGH; //CS不使用时设为高
SPI2_CSHIGH;
SPI3_CSHIGH;
SPI_SCKLOW;//SCK空闲状态一定要为低电平。
/////// spi模拟通信时不用
SPCTL = 0xd0; //使能SPI主机模式
SPSTAT = 0xc0; //清中断标志
}
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 {
USART_Txbyte(fdata[i]);
}
};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,单片机作为主机,运动控制芯片为从机。CPHA=0,CPOL=0,高位在前,SPI数据宽度为8位。
空闲状态下单片机SCK引脚必须为低电平。每一条指令开始发送前将CS引脚置低,整条指令发送完成后必须将CS置高。
每条指令间需有时间间隔,推荐延时1MS以上。
*/
unsigned char aSPI_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;
}
delay_nus(1); //发一个字节延时下
return RecevieData;
}
unsigned char SPI_SendData(unsigned char outdata)
{
unsigned char RecevieData=0,i;
SPDAT = outdata; //发送数据
while (!(SPSTAT & 0x80)); //查询完成标志
SPSTAT = 0xc0; //清中断标志
return SPDAT;
}
/*
函数名: enabled_cs
功能: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
功能: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
功能:设置轴速度
参数:
cardno 卡号
axis 轴号(1,2,3,4)
acc 加速度: 值(Hz/s2)
speed 运行频率为:值(Hz)
dec 减速度: 值(Hz/s2)
*/
void set_speed(unsigned char cardno ,unsigned char axis ,unsigned long acc ,unsigned long speed,unsigned long dec )
{
unsigned char OutByte[25];
OutByte[0] = 1; // 1
OutByte[1] = 0;
OutByte[2] = axis;
OutByte[3] = acc >>24;
OutByte[4] = acc >>16;
OutByte[5] = acc >>8;
OutByte[6] = acc ;
OutByte[7] = speed >>24;
OutByte[8] = speed >>16;
OutByte[9] = speed >>8;
OutByte[10] = speed ;
OutByte[11] = dec >>24;
OutByte[12] = dec >>16;
OutByte[13] = dec >>8;
OutByte[14] = dec ;
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]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: change_speed
功能: 改变当前正在运行的轴的速度
参数:
cardno 卡号
axis 轴号(1,2,3,4)
speed 运行频率为:值(Hz)
*/
void change_speed(unsigned char cardno ,unsigned char axis, long speed )
{
unsigned char OutByte[25];
OutByte[0] = 0x11 ;
OutByte[1] = 0 ;
OutByte[2] = axis ;
OutByte[3] = speed >>24;
OutByte[4] = speed >>16;
OutByte[5] = speed >>8;
OutByte[6] = speed ;
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
功能: 设置轴逻辑位置
参数:
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] = 0 ;
OutByte[2] = axis ;
OutByte[3] = value >>24;
OutByte[4] = value >>16;
OutByte[5] = value >>8;
OutByte[6] = 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]);
SPI_SendData(OutByte[6]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: sudden_stop
功能: 轴立即停止
参数:
cardno 卡号
axis 停止的轴号(1,2,3,4)
mode 0:急停并清空后面缓存的指令 2:急停不清后面缓存的指令
*/
void sudden_stop(unsigned char cardno ,unsigned char axis ,unsigned char mode)
{
unsigned char OutByte[25];
OutByte[0] = 0x17 ;
OutByte[1] = axis ;
OutByte[2] = mode;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: set_special
功能:设置特别功能
参数:
cardno 卡号
value
0xfc 缓存插补运动暂停
0xfd 取消缓存插补暂停
*/
void set_special(unsigned char cardno,unsigned char value)
{
unsigned char OutByte[25];
OutByte[0] = 0xFA ;
OutByte[1] = 0;
OutByte[2] = value;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
SPI_SendData(OutByte[2]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: get_inp_state
功能: 获取轴状态,缓存剩余量,各轴逻辑位置。
参数:
cardno 卡号
amount 获取字节数量。 设为22将取全部数据。
inbuf[] 读取的数据存放的数组
*/
void get_inp_state( unsigned char cardno, unsigned char amount,unsigned char tinbuf[50])
{
unsigned char OutByte[50];
char i;
enabled_cs(cardno);
tinbuf[0]=SPI_SendData(0x04);
for(i=1;i{
tinbuf[i]=SPI_SendData(0);;i++)
}
disabled_cs(cardno);
delay_nms(1);
}
/*
long get_state( unsigned char cardno,unsigned char axis )
{
unsigned char OutByte[50];
unsigned char inbuf[50];
char i;
long tmp=0;
enabled_cs(cardno);
inbuf[0]=SPI_SendData(0x04);
for(i=1;i<20;i++)
{
inbuf[i]=SPI_SendData(0);
}
disabled_cs(cardno);
tmp= (long)inbuf[0+axis*4]<<24;
tmp+= (long)inbuf[1+axis*4]<<16;
tmp+= (long)inbuf[2+axis*4]<<8;
tmp+= (long)inbuf[3+axis*4];
return tmp;
delay_nms(1);
}
*/
/*
函数名: go_home
功能:回原点,回到原点开关会自动减速停止,随后离开原点开关自动急停
参数:
cardno 卡号
no 轴号
speed1 进入原点速度,运行频率为:值(Hz)
speed2 离开原点速度,运行频率为:值(Hz)
*/
void go_home(unsigned char cardno,unsigned char no , long speed1 ,long speed2 )
{
unsigned char OutByte[25];
OutByte[0] = 0x1a;
OutByte[1] = 0;
OutByte[2] = no;
OutByte[3] = speed1>>24;
OutByte[4] = speed1 >>16;
OutByte[5] = speed1>> 8;
OutByte[6] = speed1;
OutByte[7] = speed2 >>24;
OutByte[8] = speed2 >>16;
OutByte[9] = speed2 >>8;
OutByte[10] = speed2 ;
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]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: inp_move4
功能:四轴直线插补
参数:
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] = 0 ;
OutByte[22] = 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]);
SPI_SendData(OutByte[22]);
disabled_cs(cardno);
delay_nms(100);
}
/*
函数名: inp_arc
功能:二轴圆弧插补
参数:
cardno 卡号
no1 参与插补X轴的轴号
no2 参与插补Y轴的轴号
x,y 圆弧插补的终点位置(相对于起点),范围(-8388608~+8388607)
i,j 圆弧插补的圆心点位置(相对于起点),范围(-8388608~+8388607)
mode1 0:圆心法逆时针插补 1:圆心法顺时针插补 2:三点圆弧 3:半径法逆时针插补 4:半径法顺时针插补
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(100);
}
/*
函数名: inp_helical
功能:圆弧螺旋插补
参数:
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);
}
*/
/*
函数名: write_bit
功能:写输出口状态
参数:
cardno 卡号
number 端口号(0-6) Y0-Y6
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
功能:等待延时数
参数:
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);
}
/*
函数名: pmove
功能: e版本单轴运行
参数:
cardno 卡号
axis 轴号
mode 0:绝对位移 1:相对位移
pulse1 轴移动的距离,范围(-8388608~+8388607)
*/
void pmove(unsigned char cardno,unsigned char axis,unsigned char mode, long pulse1 )
{
unsigned char OutByte[25];
OutByte[0] = 0x2;
OutByte[1] = axis ;
OutByte[2] = mode;
OutByte[3] = pulse1>>24;
OutByte[4] = pulse1 >>16;
OutByte[5] = pulse1>>8;
OutByte[6] = pulse1;
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);
}
/*
函数名: wait_stop
功能: e版本等待轴停止
参数:
cardno 卡号
axis 停止的轴号(1,2,3,4)
mode 0:急停并清空后面缓存的指令 2:急停不清后面缓存的指令
*/
void wait_stop(unsigned char cardno ,unsigned char axis)
{
unsigned char OutByte[25];
OutByte[0] = 0xf ;
OutByte[1] = axis ;
enabled_cs(cardno);
SPI_SendData(OutByte[0]);
SPI_SendData(OutByte[1]);
disabled_cs(cardno);
delay_nms(1);
}
/*
函数名: set_cam
功能: 设置电子凸轮
参数:
cardno 卡号
num 凸轮表数据段行号
xa 主轴编码器脉冲数
xb 从轴脉冲数
style 从轴运动方式
*/
void set_cam(unsigned char cardno ,unsigned int num ,unsigned long xa ,unsigned long xb,unsigned char style )
{
unsigned char OutByte[25];
OutByte[0] = 0x21;
OutByte[1] = num >>8;
OutByte[2] = num;
OutByte[3] = xa >>24;
OutByte[4] = xa >>16;
OutByte[5] = xa >>8;
OutByte[6] = xa ;
OutByte[7] = xb >>24;
OutByte[8] = xb >>16;
OutByte[9] = xb >>8;
OutByte[10] = xb ;
OutByte[11] = style ;
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);
}
/*
函数名: run_cam
功能: 运行电子凸轮
参数:
cardno 卡号
axis 从轴轴号
mode 从轴运动方式
snum 数据段起始行号
dnum 数据段结束行号
*/
void run_cam(unsigned char cardno,unsigned char axis,unsigned char mode ,unsigned int snum ,unsigned int dnum )
{
unsigned char OutByte[25];
OutByte[0] = 0x22;
OutByte[1] = axis;
OutByte[2] = mode;
OutByte[3] = snum >>8;
OutByte[4] = snum ;
OutByte[5] = dnum >>8;
OutByte[6] = dnum ;
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);
}
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(100) ;
/*下面指令为e型无缓存1轴2轴手动方式回原点,xc1006适用
set_speed(1 ,1,80000,10000,80000); // 设1轴速度
set_speed(1 ,2,80000,10000,80000); // 设2轴速度
pmove(1,1,1, -320000000); // 1轴多脉冲负方向运动 ,原点开关有效会停下
pmove(1,2,1, -320000000); // 2轴多脉冲负方向运动,原点开关有效会停下
do
{
get_inp_state( 1, 4,inbuf); //只需读出4个字节来判断轴状态
}
while(inbuf[1]); //通过返回的第二个字节里面的4个位获取各轴状态,不全为0时表示轴还没停止 ,一直等到全为0
set_command_pos(1,1,0); // 设1轴此时坐标为0
set_command_pos(1,2,0); // 设2轴此时坐标为0
*/
/*下面的指令为f型1,2轴使用回原点指令自动二次回原点
go_home(1,1,30000,5000 ) ; // 1轴回原点
go_home(1,2,30000,5000 ) ; // 2轴回原点
do
{
get_inp_state( 1, 4,inbuf); //只需读出4个字节来判断轴状态
}
while(inbuf[3]); // 等待缓存数量为0 ,如果多条运动指令在缓存里 ,可以读取缓存数量来判断指令有没执行完成。
//while(inbuf[1]); // 等待轴停止 ,如果只有一条除圆弧外的运动指令,可以读取轴状态来判断有没执行完。
set_command_pos(1 ,1,0); //设1轴坐标
set_command_pos(1 ,2,0); //设2轴坐标
*/
//set_speed(1 ,1,80000,20000,80000); //设置1轴运行速度20K,加速度80k
//set_speed(1 ,2,80000,20000,80000); // 设置2轴运行速度20K,加速度80k
//set_speed(1 ,3,80000,20000,80000); // 设置3轴运行速度20K,加速度80k
//set_speed(1 ,4,80000,20000,80000); //设置4轴运行速度20K,加速度80k
sudden_stop(1,1,3);
set_speed(1 ,1,1600,1600,1600);
set_command_pos(1,1, 0x00f8 ) ;
set_special(1,0xea);
pmove(1,1,1, 3200);
while(1)
{
// write_bit(1 , 1, 0) ;
// pmove(1,1,1, 3200);
if(!b1) //按下按键
{
delay_nms(10);
if(!b1)
{
pmove(1,1,1, 3200);
/*下面指令为e型无缓存运行 ,xc1006适用
pmove(1,1,1, 3200); // 1轴正方向运动
pmove(1,2,1, 6400); // 2轴正方向运动
do
{
get_inp_state( 1, 4,inbuf); //只需读出4个字节来判断轴状态
}
while(inbuf[1]&(1<<0)); //通过返回的第二个字节里面的0位获取1轴状态,直到1轴停止
pmove(1,1,1, -3200); // 1轴负方向运动
*/
//set_speed(1 ,1,80000,20000,80000); //设置1轴运行速度20K,加速度80k
//set_speed(1 ,2,80000,20000,80000); // 设置2轴运行速度20K,加速度80k
//set_speed(1 ,3,80000,20000,80000); // 设置3轴运行速度20K,加速度80k
// set_speed(1 ,4,80000,20000,80000); //设置4轴运行速度20K,加速度80k
// pmove(1,1,1, 2);
// pmove(1,1,0, 0);
// pmove(1,1,1, 20000);
// pmove(1,3,1, 2);
// pmove(1,4,1, 2);
// inp_move4(1,1,2,0,0,20000 ,20000,0 ,0 ,1); // 4轴直线插补
// inp_move4(1,1,2,0,0,20000 ,20000,0 ,0 ,1); // 4轴直线插补
/*e型带缓存测试指令*/
// set_speed(1 ,1,200000,50000,80000); //设置1轴运行速度50K,加速度200k
// set_speed(1 ,2,200000,50000,80000); //设置1轴运行速度50K,加速度200k
// set_speed(1 ,3,40000,10000,80000); //设置1轴运行速度10K,加速度40k
//write_bit(1 , 6, 0); // Y6输出低
// pmove(1,1,1, 1000); //1轴相对运行速1000个脉冲
// pmove(1,2,1, 1000); // 2轴相对运行速1000个脉冲
//wait_stop(1 ,1); //等待1轴停止
//wait_stop(1 ,2); //等待2轴停止
// wait_delay(1 ,500); // 延时500MS
// pmove(1,3,1, 1000); //3轴相对运行速1000个脉冲
//write_bit(1 , 6, 1); // Y6输出高
/*f型测试指令*/
// write_bit(1 , 6, 0);
// set_speed(1 ,1,200000,50000,80000); //设置运行速度50K,加速度200h
// wait_delay(1 ,500); // 延时500MS
// write_bit(1 , 2, 0);
// inp_move4(1,1,0,0,0,80000 ,0,0 ,0 ,1); // 1,2轴插补
// set_speed(1,1 ,800000,25000,80000); //设置运行速度25K,加速度800K
//inp_move4(1,1,2,0,0,20000 ,10000,0 ,0 ,1); // 1,2轴插补
// inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ; // 1,2轴圆弧插补
// inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ; // 1,2轴圆弧插补
// inp_arc(1 ,1,2, 3200, 3200, 3200, 0,0,1) ; // 1,2轴圆弧插补
// inp_arc(1 ,1,2, 20000, 20000, 20000, 0,0,1) ; // 1,2轴圆弧插补
// inp_arc(1 ,1,2, -40000, 0, -20000, 20000,2,1) ; // 1,2轴3点圆弧插补
//set_speed(1 ,1,800000,25000,80000); //设置运行速度250K,加速度800K
// inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ;
// wait_delay(1 ,500);
// write_bit(1 , 6, 1);
// wait_delay(1 ,500);
// write_bit(1 , 6, 0);
/*下面的指令会直接发到缓存区自动排队运行*/
// write_bit(1 , 6, 0); // Y6输出低
// set_speed(1 ,1,40000,25000,80000);
// inp_move4(1,1,2,3,4,320000 ,32000,32000 ,32000 ,1); // 4轴直线插补
//wait_delay(1 ,2000); //模块内部指令间延时3S
//inp_move4(1,1,2,0,0,32000 ,32000,0 ,0 ,1); // 1,2轴直线插补
// wait_delay(1 ,2000);
// inp_move4(1,2,0,0,0,32000 ,0,0 ,0 ,1); // 2轴单独运行
//inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ; // 2轴圆弧插补,终点相对起点坐标(-20000,20000),圆心相对起点坐标(-20000,0),逆时针方向,画出1/4圆弧。
// write_bit(1 , 6, 1); // Y6输出高 ,判断指令段有没执行完成也可以在指令段后面加一条端口输出指令,然后用单片机来读引脚来判断。
/*下面指令为D型电子凸轮追剪运行测试 */
// set_cam(1,0 ,0 ,0 ,0) ; // 初始位置
// set_cam(1,1 ,400 ,800,2 ) ; // 轴加速
//set_cam(1,2 ,800 ,2800,1 ) ; // 轴同步
//set_cam(1,3 ,1200 ,3200 ,3) ; // 轴减速
//set_cam(1,4 ,1600,400 ,2) ; // 轴反方向加速
// set_cam(1,5 ,2000,0 ,3) ; // 轴反方向减速到初始位置
//set_cam(1,6 ,3000,0 ,0) ; // 轴在初始位等待
// run_cam(1,1,1 ,0 ,6 ); // 单次运行0-6条
/*下面指令为D型电子凸轮飞剪运行测试 */
//set_cam(1,0 ,0 ,0 ,0) ; // 初始位置
//set_cam(1,1 ,400 ,3200,2 ) ; // 轴加速
//set_cam(1,2 ,800 ,9600,1 ) ; // 轴同步
//set_cam(1,3 ,1200 ,12800,3) ; // 轴减速到初始位
//set_cam(1,4 ,3000,0 ,0) ; // 轴在初始位等待
// run_cam(1,1,1 ,0 ,4 ); // 单次运行0-4条
while(!b1);
}
}
if(!b2) //按下按键
{
delay_nms(10) ;
if(!b2)
{
// run_cam(1,1,1 ,0 ,5 ); // 凸轮表单次运行0-5条
pmove(1,1,1, 3200);
// pmove(1,2,1, 10000);
// sudden_stop(1,1,0);
// pmove(1,1,0, 0);
// sudden_stop(1,1,0); // f型立即停止所有插补轴 ,并清缓存 。 e型立即停止1轴 ,并清缓存 。
// sudden_stop(1,2,0);
// sudden_stop(1,3,0); // f型立即停止所有插补轴 ,并清缓存 。 e型立即停止1轴 ,并清缓存 。
//sudden_stop(1,4,0);
//get_inp_state( 1, 28,inbuf); //读出数据放入数组
//USRAT_transmit(inbuf,28); // 串口将数组数据发送出去查看
// USART_Txbyte('r');
// USART_Txbyte('n');
//USART_Txbyte(0x0d);
// USART_Txbyte(0x0a);
while(!b2);
}
}
// delay_nms(10) ;
get_inp_state( 1, 22,inbuf); //读出数据放入数组
USRAT_transmit(inbuf,22); // 串口将数组数据发送出去查看
//USART_Txbyte(inbuf[1]);
// get_inp_state( 1, 20,inbuf); //读出数据放入数组
// USRAT_transmit(inbuf,20); // 串口将数组数据发送出去查看
// USART_Txbyte(inbuf[3]);
if(inbuf[1]==0) //inbuf[1]数据为0表示所有轴都停
led=1;
else
led=0; // 指示LED点亮
}
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !