?? crjtsc21c03.c
字號:
/***********************************
纏繞機提升控制程序
**********************************/
#include "REG52.h"
#include <intrins.h>
#include <math.h>
unsigned int code res6[5]={324,312,300,288,276};
unsigned int code res5[5]={48,36,24,12,276};
unsigned int code res4[5]={36,24,12,12,288};
unsigned int code res3[5]={24,12,12,24,300};
unsigned int code res2[5]={12,12,24,36,312};
unsigned int code res1[5]={12,24,36,48,324};
unsigned int code acctime[11][8]={ 75,70,66,62,59,56,53,50, //100/0.44
75,64,56,49,44,40,37,34, //150/0.65
75,60,50,43,38,34,30,28, //200/0.79
75,60,50,43,38,34,30,28, //250/0.79
75,64,56,49,44,40,37,34, //300/0.65
75,59,48,41,35,31,28,25, //350/0.86
75,54,43,35,30,26,23,20, //400/1.07
75,50,38,30,25,22,19,17, //450/1.28
75,47,34,27,22,19,16,14, //500/1.49
75,44,31,24,20,16,14,12, //550/1.7
75,43,30,23,18,15,13,11 //600/1.84
};
/*unsigned int code acctime[11][8]={ 75,70,66,62,59,56,53,50, //100/0.44
75,64,56,49,44,40,37,34, //150/0.65
75,60,50,43,38,34,30,28, //200/0.79
75,60,50,43,38,34,30,28, //250/0.79
74,63,55,48,43,39,36,33, //300/0.65
74,58,47,40,34,30,27,24, //350/0.86
74,53,42,34,29,25,22,19, //400/1.07
74,49,37,29,24,21,18,16, //450/1.28
74,46,33,26,21,18,15,13, //500/1.49
74,43,30,23,19,15,13,11, //550/1.7
74,42,29,22,17,14,12,10 //600/1.84
};*/
//**************************************
int data dataen; //數據選通
int direction; //正反轉
unsigned int head; //頭數0~31
int run; //是否運轉中 0:停車狀態1: 運轉狀態
unsigned int revcmd;
unsigned int speed;
unsigned int samespd;
unsigned int tnum;
unsigned int downpoints;
unsigned int upordown;
unsigned int uppoints;
unsigned int contime;
unsigned int contime1;
unsigned int newpos; //提升或者下降的檔位,不含最高檔位
unsigned int newp; //讀取檔位值
unsigned int nowpos; //
unsigned int workpos; //
bit ok;
//unsigned int keycode;
unsigned int swingcode;
//**************************************
//回送主控信號**********************
sbit riseerr=P1^4; //err=0 針桿高度異常=1到位
sbit rising=P1^3; //state3=0 正在提升 =1提升完畢
sbit sd0=P1^0; //測試狀態
sbit sd1=P1^1;
sbit sd2=P1^2;
//sbit sd3=P0^6;
//功能選通信號
sbit address0=P3^6;
sbit address1=P3^7;
sbit strobe=P3^1;
//提升電機驅動信號**************
sbit step=P0^2;
sbit dir=P0^1;
sbit en=P0^0;
//上位機輸入信號******************
sbit ad0=P1^0; //AD8
sbit ad1=P1^1; //AD9
sbit ad2=P1^2; //AD10
sbit ad3=P1^3; //AD11
sbit ad4=P1^4;
sbit ad5=P1^5; //AD13
sbit ad6=P1^6;
sbit ad7=P1^7;
sbit ad8=P1^0;
sbit ad9=P1^1;
sbit ad10=P1^2;
sbit ad11=P1^3;
sbit ad13=P1^5;
sbit ad12=P3^2; //C1板數據選通信號INT0
//**************************************
sbit lamp=P0^7;
//**************************************
void ex44us(void)
{
int i=2;
while(i--);
}
void delay44us(unsigned int nums)
{
do
{
ex44us();
nums--;
}while(nums!=0);
}
void Ex10us()
{
data unsigned char t=3;
while(--t);
}
void Delay10us(unsigned int nums)
{
do{
Ex10us();
nums--;
}while(nums!=0);
}
/**********************************************
信號選通
address1 address0
0 0 選通AD0-AD7
0 1 選通AD8-AD13、JPOS0、JPOS1
1 0 選通state0~4、err
1 1 選通撥碼開關、IN0、IN1、JPOS2
IN:signal 0~3
***********************************************/
void PitchON(int signal)
{
switch(signal)
{
case 0:
address0=0;
address1=0;
break;
case 1:
address0=1;
address1=0;
break;
case 2:
address0=0;
address1=1;
break;
case 3:
address0=1;
address1=1;
break;
}
}
/**********************************************
寫state0~3、err信號
***********************************************/
void Strobe()
{
strobe=0;
_nop_();
_nop_();
strobe=1;
_nop_();
_nop_();
P1 |= 0xff;
}
/*************************************
區分命令
IN:ad8~ad11
OUT: 0:提升 2:讀提升檔位 13:初始化
*************************************/
unsigned int RevCommand()
{
unsigned int cmd=0;
PitchON(1);
Delay10us(10);
cmd=(unsigned int)ad13;
cmd=(cmd<<1)|(unsigned int)ad11;
cmd=(cmd<<1)|(unsigned int)ad10;
cmd=(cmd<<1)|(unsigned int)ad9;
cmd=(cmd<<1)|(unsigned int)ad8;
return (cmd);
}
/*************************************
IN:ad7~ad0 cmdtype=0:提升 2/3:讀檔位 13:初始化
OUT:檔位/運轉否
*************************************/
unsigned int DealCmdData(int cmdtype)
{
unsigned int cmd=0;
unsigned int cmdsir[8];
int i;
PitchON(0);
Delay10us(10);
if(cmdtype==0) //檔位0~5
{
cmd=ad3;
cmd=(cmd<<1)|(unsigned int)ad2;
cmd=(cmd<<1)|(unsigned int)ad1;
cmd=(cmd<<1)|(unsigned int)ad0;
}
else if(cmdtype==1) //轉速0~60
{
cmd=ad5;
cmd=(cmd<<1)|(unsigned int)ad4;
cmd=(cmd<<1)|(unsigned int)ad3;
cmd=(cmd<<1)|(unsigned int)ad2;
cmd=(cmd<<1)|(unsigned int)ad1;
cmd=(cmd<<1)|(unsigned int)ad0;
}
else if(cmdtype==10) //1:壓腳提升/0:壓腳下降
{
cmd=ad0;
}
else if(cmdtype==11) //壓腳振幅10-50檔,最大50
{
cmd=ad5;
cmd=(cmd<<1)|(unsigned int)ad4;
cmd=(cmd<<1)|(unsigned int)ad3;
cmd=(cmd<<1)|(unsigned int)ad2;
cmd=(cmd<<1)|(unsigned int)ad1;
cmd=(cmd<<1)|(unsigned int)ad0;
}
else if(cmdtype==13)
{
cmdsir[0]=ad0;
cmdsir[1]=ad1;
cmdsir[2]=ad2;
cmdsir[3]=ad3;
cmdsir[4]=ad4;
cmdsir[5]=ad5;
cmdsir[6]=ad6;
cmdsir[7]=ad7;
for(i=0;i<8;i++)
{
if(cmdsir[0]!=cmdsir[i])
return(1); //通訊不過
}
return(0);
}
return (cmd);
}
/*************************************
發送一個脈沖(f=1k),驅動電機
*************************************/
void SendPulse(unsigned int ttime)
{
if(direction==0)
{
step=1;
dir=1;
}
else
{
dir=1;
step=1;
}
ex44us();
if(direction==0)
step=0;
else
dir=0;
delay44us(ttime);
}
/*************************************
提升函數
傳動比
細分:1:4
輸入:要提升的檔位
返回:0:正常 1:錯誤
*************************************/
int HeadUpOrDown(unsigned int npos)
{
int i;
newpos=npos;
PitchON(2);
riseerr=0;
rising=0;
Strobe();
if(newpos>nowpos)
direction=0; //電機正轉(上升)
else
direction=1; //電機反轉(下降)
if(newpos==5)
uppoints=res6[nowpos];
else if(newpos==4)
{
if(nowpos<4)
uppoints=res5[nowpos];
else
uppoints=res5[nowpos-1];
}
else if(newpos==3)
{
if(nowpos<3)
uppoints=res4[nowpos];
else
uppoints=res4[nowpos-1];
}
else if(newpos==2)
{
if(nowpos<2)
uppoints=res3[nowpos];
else
uppoints=res3[nowpos-1];
}
else if(newpos==1)
{
if(nowpos<1)
uppoints=res2[nowpos];
else
uppoints=res2[nowpos-1];
}
else if(newpos==0)
{
uppoints=res1[nowpos-1];
}
en=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
for(i=0;i<uppoints;i++)
SendPulse(60);
en=1;
nowpos=newpos;
PitchON(2);
riseerr=1;
rising=1;
Strobe();
return(0);
}
int ToUp(unsigned int spd)
{
int i;
samespd=spd;
samespd=samespd/5;
samespd--;
samespd--;
if(samespd<0)
samespd=0;
else if(samespd>10)
samespd=10;
PitchON(2);
riseerr=0;
rising=0;
Strobe();
direction=0; //電機正轉(上升)
if(samespd>3)
{
//uppoints=36;
//uppoints=keycode*6;
uppoints = swingcode;
}
else if(samespd==3)
{
uppoints=48;
}
else if(samespd<=2)
{
uppoints=60;
}
/*else if(samespd==1)
{
uppoints=72;
}
else if(samespd==0)
{
uppoints=84;
}*/
en=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(uppoints>18)
{
for(i=0;i<8;i++)
SendPulse(acctime[samespd][i]);
for(i=0;i<uppoints-16;i++)
SendPulse(acctime[samespd][7]);
for(i=0;i<8;i++)
SendPulse(acctime[samespd][7-i]);
}
else
{
for(i=0;i<uppoints;i++)
SendPulse(40);
}
en=1;
PitchON(2);
riseerr=1;
rising=1;
Strobe();
return(0);
}
int ToDown()
{
int i;
PitchON(2);
riseerr=0;
rising=0;
Strobe();
direction=1; //電機反轉(下降)
if(samespd>3)
{
//downpoints=36;
//downpoints=keycode*6;
downpoints = swingcode;
}
else if(samespd==3)
{
downpoints=48;
}
else if(samespd<=2)
{
downpoints=60;
}
/*else if(samespd==1)
{
downpoints=72;
}
else if(samespd==0)
{
downpoints=84;
}*/
en=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(downpoints>18)
{
for(i=0;i<8;i++)
SendPulse(acctime[samespd][i]);
for(i=0;i<downpoints-16;i++)
SendPulse(acctime[samespd][7]);
for(i=0;i<8;i++)
SendPulse(acctime[samespd][7-i]);
}
else
{
for(i=0;i<downpoints;i++)
SendPulse(40);
}
en=1;
PitchON(2);
riseerr=1;
rising=1;
Strobe();
return(0);
}
void External0(void) interrupt 0 using 2
{
EA=0;
dataen=0;
EA=1;
}
void main()
{
unsigned char i;
//int checkcom=1; //開機檢查通訊線全低 =1有錯誤 =0正常
//int checkcom2=1; //開機檢查通訊線全高 =1有錯誤 =0正常
run=0;
nowpos=1;
newp=0;
for(i=0;i<3;i++)
{
lamp=0;
delay44us(3000);
lamp=1;
delay44us(3000);
}
IT0=1;
EX0=1;
TR0=0;
ET0=0;
IE0=0;
EA=1;
dataen=1;
PitchON(2);
rising=1;
Strobe();
en=1; //提升電機使能
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
/*while(checkcom==1)
{
_nop_();
//locklamp=1; //有錯誤,熄燈
//mendlamp=1;
if(dataen==0)
{
revcmd=RevCommand();
dataen=1;
}
if(revcmd==13)
checkcom=DealCmdData(13);
if(checkcom==0)
{
PitchON(2);
riseerr=1;
Strobe();
}
}
while(checkcom2==1)
{
_nop_();
//locklamp=1; //有錯誤,熄燈
//mendlamp=1;
PitchON(2);
riseerr=0;
Strobe();
if(dataen==0)
{
revcmd=RevCommand();
dataen=1;
}
if(revcmd==13)
checkcom2=DealCmdData(13);
if(checkcom2==0)
{
PitchON(2);
riseerr=1;
Strobe();
}
}
lamp=0;*/
/*while(run==0)
{
speed=40;
ToUp(speed);
delay44us(10000);
ToDown();
delay44us(20000);
}*/
while(1)
{
revcmd=15;
/*//讀撥碼開關確定繡作時壓腳動作幅度編碼
PitchON(3);
Delay10us(10);
keycode=ad2;
keycode=(keycode<<1)|(unsigned int)ad1;
keycode=(keycode<<1)|(unsigned int)ad0;
keycode++;
if(keycode>6)
keycode=6;
if(keycode<1)
keycode=1;*/
if(dataen==0)
{
revcmd=RevCommand();
dataen=1;
PitchON(2);
sd0=1;
sd1=1;
sd2=1;
riseerr=1;
Strobe();
}
switch(revcmd)
{
case 15: //無效
break;
case 0: //提升
newp=DealCmdData(0); //工作位
if(newp>5)
newp=5;
if(newp<0)
newp=0;
if(newp==nowpos)
break;
ok=HeadUpOrDown(newp);
//nowpos=newp;
break;
case 1:
speed=DealCmdData(1);
if(speed>60)
speed=60;
if(speed<0)
speed=0;
if(speed==0)
{
TR1=0;
ET1=0;
TH1=0;
TL1=0;
step=1;
dir=1;
samespd=0;
}
break;
case 10://壓腳升降
upordown=DealCmdData(10);
//upordown
if(upordown==0)//升
ToUp(speed);
else
ToDown();
break;
case 11:
swingcode = DealCmdData(11);
if(swingcode > 40)
swingcode = 40;
if(swingcode < 10)
swingcode = 10;
break;
default:
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -