/**************************************************************/
#include <AT89x051.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit pluse_1=P1^2;
sbit direc_1=P1^3;
sbit pluse_2=P1^4;
sbit direc_2=P1^5;
uchar num,flag;
uchar ret_val1,ret_val2,ret_val3;
uchar in_buf[12];
uchar out_buf[7]={0x7f,0x05,0x01,0x01,0x00,0x00,0x86};
/***********************************************************/
void send_SBU(uchar d)
{
SBUF=d;
while(!TI);
TI=0;
}
/***********************************************************/
void send_code()
{
uchar i;
for(i=0; i<7; i++)
{
send_SBU(out_buf[i]);
}
}
/***********************************************************/
void send_invain()
{
out_buf[4]=0x01;
out_buf[6]=0x87;
send_code();
}
/***********************************************************/
void send_right()
{
out_buf[4]=0x00;
out_buf[6]=0x86;
send_code();
}
/***********************************************************/
void send_error()
{
out_buf[4]=0x02;
out_buf[6]=0x88;
send_code();
}
/***********************************************************/
uchar compare_data(ret_val3)
{
uint incre1,incre2;
incre1=in_buf[5]*256+in_buf[6];
incre2=in_buf[8]*256+in_buf[9];
if((incre1<300)&&(incre2<300))
{
return 0;
}
else
{
return 0xff;
}
}
/***********************************************************/
uchar check_out(ret_val2) //数据校验函数
{
uchar i,check1, check2;
check1=0;
check2=0;
for(i=0; i<10; i++)
{
check1=check1+in_buf[i];
}
check2=in_buf[10]*256+in_buf[11];
if(check1==check2)
{
return 0;
}
else
{
return 0xff;
}
}
/***********************************************************/
void receive_data() //数据接收函数
{
uchar i;
for(i=0; i<12; i++)
{
while(!RI);
RI=0;
in_buf[i]=SBUF;
}
}
/***********************************************************/
/*void send_data()
{
uchar i;
for(i=0; i<12; i++)
{
send_SBU(in_buf[i]);
}
}*/
/***********************************************************/
uchar data_traffic(ret_val1) //数据传送函数
{
if(RI)
{
receive_data(); //数据接收
if(in_buf[0]==0x7f&&in_buf[1]==0x0A&&in_buf[2]==0x01&&in_buf[3]==0x02)
{
if(check_out(ret_val2)==0)
{
if(compare_data(ret_val3)==0)
{
send_right();
flag=1;
return 0;
}
else
{
send_invain();
return 0xff;
}
}
else
{
send_error();
return 0xff;
}
}
else
{
return 0xff;
}
}
}
/***********************************************************/
void init_steptime()
{
TMOD = 0x21; //定时器0为工作方式1,定时器T1使用工作方式2
TH1=0xFA;
TL1=0XFA;
TH0 = 0xFF; //50us初值
TL0 = 0xCE;
TR1=1; //启动定时器1
EA = 1; //开全局中断
ET0 = 0; //关定时器0中断
TR0 = 0; //关定时器0
PCON=0x80; //SMOD=1
SCON=0x50; //串口工作方式1,9600bit/s,允许接收
}
/***********************************************************/
void init_direct()
{
if(in_buf[4]==0x00) //转角方向1
{
direc_1=1;
}
else
direc_1=0;
if(in_buf[7]==0x00) //转角方向2
{
direc_2=1;
}
else
direc_2=0;
}
/***********************************************************/
void step_moto() //步进电机驱动
{
uint plu_num1, plu_num2;
plu_num1=0;
plu_num2=0;
num=0,
init_direct();
plu_num1=(in_buf[5]*256+in_buf[6])*24; //转角增量1
plu_num2=(in_buf[8]*256+in_buf[9])*24; //转角增量2
while(plu_num1!=0|plu_num2!=0)
{
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
REN=0;
if(num==5) //0.25ms,4,000 plu/sec
{
num=0;
if(plu_num1>0)
{
pluse_1=~pluse_1;
plu_num1--;
}
if(plu_num2>0)
{
pluse_2=~pluse_2;
plu_num2--;
}
}
}
ET0=0; //关定时器0中断
TR0=0; //关定时器0
REN=1;
}
/***********************************************************/
void T0_time() interrupt 1
{
TH0 = 0xFF;//50us初值
TL0 = 0xCE;
num++;
}
/***********************************************************/
void delay()
{
unsigned char a,b;
for(b=1;b>0;b--)
{
for(a=89;a>0;a--);
}
}
/***********************************************************/
void return_to_zero()
{
uint i;
direc_1=1;
direc_2=1;
for(i=7200; i>0; i--)
{
delay();
pluse_1=~pluse_1;
pluse_2=~pluse_2;
}
}
/***********************************************************/
void main()
{
uchar i;
return_to_zero();
init_steptime();
while(1)
{
if(data_traffic(ret_val1)==0&&flag==1)
{
step_moto();
flag=0;
}
else
{
for(i=0; i<12; i++)
{
in_buf[i]=0;
}
pluse_1=0;
pluse_2=0;
direc_1=0;
direc_2=0;
}
}
}
/***********************************************************/
At894051串口仪表控制
最新推荐文章于 2024-07-09 10:54:37 发布