#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 //使用內(nèi)部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; // 引腳模擬通信時(shí),MOSI,SCK, CS 設(shè)為推挽輸出
SPI1_CSHIGH; //CS不使用時(shí)設(shè)為高
SPI2_CSHIGH;
SPI3_CSHIGH;
SPI_SCKLOW;//SCK空閑狀態(tài)一定要為低電平。
/////// spi模擬通信時(shí)不用
SPCTL = 0xd0; //使能SPI主機(jī)模式
SPSTAT = 0xc0; //清中斷標(biāo)志
}
void init_uart()
{
SCON = 0x50;
T2L = BRT;
T2H = BRT >> 8;
AUXR = 0x15;
}
/*
串口發(fā)送一個(gè)字節(jié)。
*/
void USART_Txbyte(unsigned char i)
{
SBUF = i;
while(TI ==0);
TI = 0;
}
/*
串口發(fā)送一串?dāng)?shù)據(jù)。
*/
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--);
}
}
//延時(shí)n ms
void delay_nms(unsigned long n)
{
while(n--)
delay_nus(1000);
}
/*
函數(shù)名: SPI_SendData
功能:軟件模擬SPI通訊發(fā)送并接收一個(gè)8位字節(jié)數(shù)據(jù)。
如需使用硬件SPI,單片機(jī)作為主機(jī),運(yùn)動(dòng)控制芯片為從機(jī)。CPHA=0,CPOL=0,高位在前,SPI數(shù)據(jù)寬度為8位。
空閑狀態(tài)下單片機(jī)SCK引腳必須為低電平。每一條指令開(kāi)始發(fā)送前將CS引腳置低,整條指令發(fā)送完成后必須將CS置高。
每條指令間需有時(shí)間間隔,推薦延時(shí)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); //發(fā)一個(gè)字節(jié)延時(shí)下
return RecevieData;
}
unsigned char SPI_SendData(unsigned char outdata)
{
unsigned char RecevieData=0,i;
SPDAT = outdata; //發(fā)送數(shù)據(jù)
while (!(SPSTAT & 0x80)); //查詢完成標(biāo)志
SPSTAT = 0xc0; //清中斷標(biāo)志
return SPDAT;
}
/*
函數(shù)名: enabled_cs
功能:SPI運(yùn)動(dòng)控制模塊使能對(duì)應(yīng)芯片模塊的CS腳
參數(shù):
cardno 卡號(hào)
用單片機(jī)不同引腳去控制不同芯片的CS腳,以便多個(gè)芯片模塊關(guān)聯(lián)使用。
*/
void enabled_cs(unsigned char cardno)
{
if(cardno==1)
{
SPI1_CSLOW;
}
if(cardno==2)
{
SPI2_CSLOW;
}
if(cardno==3)
{
SPI3_CSLOW;
}
}
/*
函數(shù)名: disabled_cs
功能:SPI運(yùn)動(dòng)控制模塊禁止對(duì)應(yīng)芯片模塊的CS腳
參數(shù):
cardno 卡號(hào)
用單片機(jī)不同引腳去控制不同芯片的CS腳,以便多個(gè)芯片關(guān)聯(lián)使用。
*/
void disabled_cs(unsigned char cardno)
{
if(cardno==1)
{
SPI1_CSHIGH;
}
if(cardno==2)
{
SPI2_CSHIGH;
}
if(cardno==3)
{
SPI3_CSHIGH;
}
}
/*
函數(shù)名: set_speed
功能:設(shè)置軸速度
參數(shù):
cardno 卡號(hào)
axis 軸號(hào)(1,2,3,4)
acc 加速度: 值(Hz/s2)
speed 運(yùn)行頻率為:值(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);
}
/*
函數(shù)名: change_speed
功能: 改變當(dāng)前正在運(yùn)行的軸的速度
參數(shù):
cardno 卡號(hào)
axis 軸號(hào)(1,2,3,4)
speed 運(yùn)行頻率為:值(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);
}
/*
函數(shù)名: set_command_pos
功能: 設(shè)置軸邏輯位置
參數(shù):
cardno 卡號(hào)
axis 軸號(hào)(1,2,3,4)
pulse 位置脈沖數(shù),范圍(-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);
}
/*
函數(shù)名: sudden_stop
功能: 軸立即停止
參數(shù):
cardno 卡號(hào)
axis 停止的軸號(hào)(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);
}
/*
函數(shù)名: set_special
功能:設(shè)置特別功能
參數(shù):
cardno 卡號(hào)
value
0xfc 緩存插補(bǔ)運(yùn)動(dòng)暫停
0xfd 取消緩存插補(bǔ)暫停
*/
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);
}
/*
函數(shù)名: get_inp_state
功能: 獲取軸狀態(tài),緩存剩余量,各軸邏輯位置。
參數(shù):
cardno 卡號(hào)
amount 獲取字節(jié)數(shù)量。 設(shè)為22將取全部數(shù)據(jù)。
inbuf[] 讀取的數(shù)據(jù)存放的數(shù)組
*/
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);
}
*/
/*
函數(shù)名: go_home
功能:回原點(diǎn),回到原點(diǎn)開(kāi)關(guān)會(huì)自動(dòng)減速停止,隨后離開(kāi)原點(diǎn)開(kāi)關(guān)自動(dòng)急停
參數(shù):
cardno 卡號(hào)
no 軸號(hào)
speed1 進(jìn)入原點(diǎn)速度,運(yùn)行頻率為:值(Hz)
speed2 離開(kāi)原點(diǎn)速度,運(yùn)行頻率為:值(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);
}
/*
函數(shù)名: inp_move4
功能:四軸直線插補(bǔ)
參數(shù):
cardno 卡號(hào)
no1 X軸軸號(hào)
no2 Y軸軸號(hào)
no3 Z軸軸號(hào)
no4 E軸軸號(hào)
pulse1,pulse2,pulse3,pulse4 X-Y-Z-E軸移動(dòng)的距離,范圍(-8388608~+8388607)
mode 0:絕對(duì)位移 1:相對(duì)位移
*/
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);
}
/*
函數(shù)名: inp_arc
功能:二軸圓弧插補(bǔ)
參數(shù):
cardno 卡號(hào)
no1 參與插補(bǔ)X軸的軸號(hào)
no2 參與插補(bǔ)Y軸的軸號(hào)
x,y 圓弧插補(bǔ)的終點(diǎn)位置(相對(duì)于起點(diǎn)),范圍(-8388608~+8388607)
i,j 圓弧插補(bǔ)的圓心點(diǎn)位置(相對(duì)于起點(diǎn)),范圍(-8388608~+8388607)
mode1 0:圓心法逆時(shí)針插補(bǔ) 1:圓心法順時(shí)針插補(bǔ) 2:三點(diǎn)圓弧 3:半徑法逆時(shí)針插補(bǔ) 4:半徑法順時(shí)針插補(bǔ)
mode2 0:絕對(duì)位移 1:相對(duì)位移
*/
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);
}
/*
函數(shù)名: inp_helical
功能:圓弧螺旋插補(bǔ)
參數(shù):
cardno 卡號(hào)
no1 參與插補(bǔ)X軸的軸號(hào)
no2 參與插補(bǔ)Y軸的軸號(hào)
no3 參與插補(bǔ)螺旋軸的軸號(hào)
x,y 圓弧插補(bǔ)的終點(diǎn)位置(相對(duì)于起點(diǎn)),范圍(-8388608~+8388607)
z 參與插補(bǔ)螺旋軸的位置(相對(duì)于起點(diǎn))
i,j 圓弧插補(bǔ)的圓心點(diǎn)位置(相對(duì)于起點(diǎn)),范圍(-8388608~+8388607)
mode1 0:逆時(shí)針插補(bǔ) 1:順時(shí)針插補(bǔ)
mode2 0:絕對(duì)位移 1:相對(duì)位移
*/
/*
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);
}
*/
/*
函數(shù)名: write_bit
功能:寫(xiě)輸出口狀態(tài)
參數(shù):
cardno 卡號(hào)
number 端口號(hào)(0-6) Y0-Y6
value 狀態(tài)(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);
}
/*
函數(shù)名: wait_delay
功能:等待延時(shí)數(shù)
參數(shù):
cardno 卡號(hào)
value 延時(shí)量(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);
}
/*
函數(shù)名: pmove
功能: e版本單軸運(yùn)行
參數(shù):
cardno 卡號(hào)
axis 軸號(hào)
mode 0:絕對(duì)位移 1:相對(duì)位移
pulse1 軸移動(dòng)的距離,范圍(-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);
}
/*
函數(shù)名: wait_stop
功能: e版本等待軸停止
參數(shù):
cardno 卡號(hào)
axis 停止的軸號(hào)(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);
}
/*
函數(shù)名: set_cam
功能: 設(shè)置電子凸輪
參數(shù):
cardno 卡號(hào)
num 凸輪表數(shù)據(jù)段行號(hào)
xa 主軸編碼器脈沖數(shù)
xb 從軸脈沖數(shù)
style 從軸運(yùn)動(dòng)方式
*/
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);
}
/*
函數(shù)名: run_cam
功能: 運(yùn)行電子凸輪
參數(shù):
cardno 卡號(hào)
axis 從軸軸號(hào)
mode 從軸運(yùn)動(dòng)方式
snum 數(shù)據(jù)段起始行號(hào)
dnum 數(shù)據(jù)段結(jié)束行號(hào)
*/
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; //啟動(dòng)外部晶振
// while (!(XOSCCR & 1)); //等待時(shí)鐘穩(wěn)定
// CKDIV = 0x00; //時(shí)鐘不分頻
// CKSEL = 0x01; //選擇外部晶振
// P_SW2 = 0x00;
led=0;
delay_nms(100) ;
/*下面指令為e型無(wú)緩存1軸2軸手動(dòng)方式回原點(diǎn),xc1006適用
set_speed(1 ,1,80000,10000,80000); // 設(shè)1軸速度
set_speed(1 ,2,80000,10000,80000); // 設(shè)2軸速度
pmove(1,1,1, -320000000); // 1軸多脈沖負(fù)方向運(yùn)動(dòng) ,原點(diǎn)開(kāi)關(guān)有效會(huì)停下
pmove(1,2,1, -320000000); // 2軸多脈沖負(fù)方向運(yùn)動(dòng),原點(diǎn)開(kāi)關(guān)有效會(huì)停下
do
{
get_inp_state( 1, 4,inbuf); //只需讀出4個(gè)字節(jié)來(lái)判斷軸狀態(tài)
}
while(inbuf[1]); //通過(guò)返回的第二個(gè)字節(jié)里面的4個(gè)位獲取各軸狀態(tài),不全為0時(shí)表示軸還沒(méi)停止 ,一直等到全為0
set_command_pos(1,1,0); // 設(shè)1軸此時(shí)坐標(biāo)為0
set_command_pos(1,2,0); // 設(shè)2軸此時(shí)坐標(biāo)為0
*/
/*下面的指令為f型1,2軸使用回原點(diǎn)指令自動(dòng)二次回原點(diǎn)
go_home(1,1,30000,5000 ) ; // 1軸回原點(diǎn)
go_home(1,2,30000,5000 ) ; // 2軸回原點(diǎn)
do
{
get_inp_state( 1, 4,inbuf); //只需讀出4個(gè)字節(jié)來(lái)判斷軸狀態(tài)
}
while(inbuf[3]); // 等待緩存數(shù)量為0 ,如果多條運(yùn)動(dòng)指令在緩存里 ,可以讀取緩存數(shù)量來(lái)判斷指令有沒(méi)執(zhí)行完成。
//while(inbuf[1]); // 等待軸停止 ,如果只有一條除圓弧外的運(yùn)動(dòng)指令,可以讀取軸狀態(tài)來(lái)判斷有沒(méi)執(zhí)行完。
set_command_pos(1 ,1,0); //設(shè)1軸坐標(biāo)
set_command_pos(1 ,2,0); //設(shè)2軸坐標(biāo)
*/
//set_speed(1 ,1,80000,20000,80000); //設(shè)置1軸運(yùn)行速度20K,加速度80k
//set_speed(1 ,2,80000,20000,80000); // 設(shè)置2軸運(yùn)行速度20K,加速度80k
//set_speed(1 ,3,80000,20000,80000); // 設(shè)置3軸運(yùn)行速度20K,加速度80k
//set_speed(1 ,4,80000,20000,80000); //設(shè)置4軸運(yùn)行速度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型無(wú)緩存運(yùn)行 ,xc1006適用
pmove(1,1,1, 3200); // 1軸正方向運(yùn)動(dòng)
pmove(1,2,1, 6400); // 2軸正方向運(yùn)動(dòng)
do
{
get_inp_state( 1, 4,inbuf); //只需讀出4個(gè)字節(jié)來(lái)判斷軸狀態(tài)
}
while(inbuf[1]&(1<<0)); ? ? //通過(guò)返回的第二個(gè)字節(jié)里面的0位獲取1軸狀態(tài),直到1軸停止
pmove(1,1,1, -3200); // 1軸負(fù)方向運(yùn)動(dòng)
*/
//set_speed(1 ,1,80000,20000,80000); //設(shè)置1軸運(yùn)行速度20K,加速度80k
//set_speed(1 ,2,80000,20000,80000); // 設(shè)置2軸運(yùn)行速度20K,加速度80k
//set_speed(1 ,3,80000,20000,80000); // 設(shè)置3軸運(yùn)行速度20K,加速度80k
// set_speed(1 ,4,80000,20000,80000); //設(shè)置4軸運(yùn)行速度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軸直線插補(bǔ)
// inp_move4(1,1,2,0,0,20000 ,20000,0 ,0 ,1); // 4軸直線插補(bǔ)
/*e型帶緩存測(cè)試指令*/
// set_speed(1 ,1,200000,50000,80000); //設(shè)置1軸運(yùn)行速度50K,加速度200k
// set_speed(1 ,2,200000,50000,80000); //設(shè)置1軸運(yùn)行速度50K,加速度200k
// set_speed(1 ,3,40000,10000,80000); //設(shè)置1軸運(yùn)行速度10K,加速度40k
//write_bit(1 , 6, 0); // Y6輸出低
// pmove(1,1,1, 1000); //1軸相對(duì)運(yùn)行速1000個(gè)脈沖
// pmove(1,2,1, 1000); // 2軸相對(duì)運(yùn)行速1000個(gè)脈沖
//wait_stop(1 ,1); //等待1軸停止
//wait_stop(1 ,2); //等待2軸停止
// wait_delay(1 ,500); // 延時(shí)500MS
// pmove(1,3,1, 1000); //3軸相對(duì)運(yùn)行速1000個(gè)脈沖
//write_bit(1 , 6, 1); // Y6輸出高
/*f型測(cè)試指令*/
// write_bit(1 , 6, 0);
// set_speed(1 ,1,200000,50000,80000); //設(shè)置運(yùn)行速度50K,加速度200h
// wait_delay(1 ,500); // 延時(shí)500MS
// write_bit(1 , 2, 0);
// inp_move4(1,1,0,0,0,80000 ,0,0 ,0 ,1); // 1,2軸插補(bǔ)
// set_speed(1,1 ,800000,25000,80000); //設(shè)置運(yùn)行速度25K,加速度800K
//inp_move4(1,1,2,0,0,20000 ,10000,0 ,0 ,1); // 1,2軸插補(bǔ)
// inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ; // 1,2軸圓弧插補(bǔ)
// inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ; // 1,2軸圓弧插補(bǔ)
// inp_arc(1 ,1,2, 3200, 3200, 3200, 0,0,1) ; // 1,2軸圓弧插補(bǔ)
// inp_arc(1 ,1,2, 20000, 20000, 20000, 0,0,1) ; // 1,2軸圓弧插補(bǔ)
// inp_arc(1 ,1,2, -40000, 0, -20000, 20000,2,1) ; // 1,2軸3點(diǎn)圓弧插補(bǔ)
//set_speed(1 ,1,800000,25000,80000); //設(shè)置運(yùn)行速度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);
/*下面的指令會(huì)直接發(fā)到緩存區(qū)自動(dòng)排隊(duì)運(yùn)行*/
// 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軸直線插補(bǔ)
//wait_delay(1 ,2000); //模塊內(nèi)部指令間延時(shí)3S
//inp_move4(1,1,2,0,0,32000 ,32000,0 ,0 ,1); // 1,2軸直線插補(bǔ)
// wait_delay(1 ,2000);
// inp_move4(1,2,0,0,0,32000 ,0,0 ,0 ,1); // 2軸單獨(dú)運(yùn)行
//inp_arc(1 ,1,2, -20000, 20000, -20000, 0,0,1) ; // 2軸圓弧插補(bǔ),終點(diǎn)相對(duì)起點(diǎn)坐標(biāo)(-20000,20000),圓心相對(duì)起點(diǎn)坐標(biāo)(-20000,0),逆時(shí)針?lè)较颍?huà)出1/4圓弧。
// write_bit(1 , 6, 1); // Y6輸出高 ,判斷指令段有沒(méi)執(zhí)行完成也可以在指令段后面加一條端口輸出指令,然后用單片機(jī)來(lái)讀引腳來(lái)判斷。
/*下面指令為D型電子凸輪追剪運(yùn)行測(cè)試 */
// 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 ); // 單次運(yùn)行0-6條
/*下面指令為D型電子凸輪飛剪運(yùn)行測(cè)試 */
//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 ); // 單次運(yùn)行0-4條
while(!b1);
}
}
if(!b2) //按下按鍵
{
delay_nms(10) ;
if(!b2)
{
// run_cam(1,1,1 ,0 ,5 ); // 凸輪表單次運(yùn)行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型立即停止所有插補(bǔ)軸 ,并清緩存 。 e型立即停止1軸 ,并清緩存 。
// sudden_stop(1,2,0);
// sudden_stop(1,3,0); // f型立即停止所有插補(bǔ)軸 ,并清緩存 。 e型立即停止1軸 ,并清緩存 。
//sudden_stop(1,4,0);
//get_inp_state( 1, 28,inbuf); //讀出數(shù)據(jù)放入數(shù)組
//USRAT_transmit(inbuf,28); // 串口將數(shù)組數(shù)據(jù)發(fā)送出去查看
// USART_Txbyte('r');
// USART_Txbyte('n');
//USART_Txbyte(0x0d);
// USART_Txbyte(0x0a);
while(!b2);
}
}
// delay_nms(10) ;
get_inp_state( 1, 22,inbuf); //讀出數(shù)據(jù)放入數(shù)組
USRAT_transmit(inbuf,22); // 串口將數(shù)組數(shù)據(jù)發(fā)送出去查看
//USART_Txbyte(inbuf[1]);
// get_inp_state( 1, 20,inbuf); //讀出數(shù)據(jù)放入數(shù)組
// USRAT_transmit(inbuf,20); // 串口將數(shù)組數(shù)據(jù)發(fā)送出去查看
// USART_Txbyte(inbuf[3]);
if(inbuf[1]==0) //inbuf[1]數(shù)據(jù)為0表示所有軸都停
led=1;
else
led=0; // 指示LED點(diǎn)亮
}
}
審核編輯黃宇
-
芯片
+關(guān)注
關(guān)注
456文章
50950瀏覽量
424741 -
SPI
+關(guān)注
關(guān)注
17文章
1711瀏覽量
91756 -
運(yùn)動(dòng)控制
+關(guān)注
關(guān)注
4文章
584瀏覽量
32902
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論