運動控制編程參考
通過51單片機控制運動控制芯片的SPI通信程式示例。
(來自產品官網:http://www.lf-control.com)
#include <intrins.h>
#include <reg52.h>
//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];
unsigned char b1_state=0;
void initial()
{
P1M1 = 0;
P1M0 = 0x2c; // 引腳模擬通信時,MOSI,SCK, CS 設為推挽輸出
SPI1_CSHIGH; //CS不使用時設為高
SPI2_CSHIGH;
SPI3_CSHIGH;
SPI_SCKLOW;//SCK空閑狀態一定要為低電平。
//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<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,單片機作為主機,運動控制芯片為從機。CPHA=0,CPOL=0,高位在前,SPI資料寬度為8位。
空閑狀態下單片機SCK引腳必須為低電平。每一條指令開始發送前將CS引腳置低,整條指令發送完成后必須將CS置高。
每條指令間需有時間間隔,推薦延時1MS以上。
*/
unsigned char SPI_SendData(unsigned char outdata)
{
unsigned char RecevieData=https://bbs.csdn.net/topics/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;
}
/*
unsigned char SPI_SendData(unsigned char outdata)
{
unsigned char RecevieData=https://bbs.csdn.net/topics/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)
*/
void set_speed(unsigned char cardno ,unsigned char axis ,unsigned long acc ,unsigned long speed )
{
unsigned char OutByte[25];
OutByte[0] = 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 ;
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);
}
/*
函式名: 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 獲取位元組數量。 設為20將取全部資料。
inbuf[] 讀取的資料存放的陣列
*/
void get_inp_state( unsigned char cardno, unsigned char amount,unsigned char inbuf[])
{
unsigned char OutByte[25];
char i;
enabled_cs(cardno);
inbuf[0]=SPI_SendData(0x04);
for(i=1;i<amount;i++)
{
inbuf[i]=SPI_SendData(0);
}
disabled_cs(cardno);
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(1);
}
/*
函式名: inp_arc
功能:二軸圓弧插補
引數:
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 )
{
unsi
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/10882.html
標籤:單片機/工控
