#include <reg51.h>
#define uchar unsigned char
uchar sn=10; //步数,控制时,每段分为10步
uchar idata nowrate[2]={0x00,0x00};//匀速定时器赋初值,匀速
uchar idata *pp; //状态分为加速,匀速,减速共三种状态
是什么意思 bit pf=1; //中断标志
void contrl(direct,mode)
是什么意思 bit direct; //方向
uchar mode; //速度控制
{
uchar i;
uchar idata *cmode;
uchar idata rise[20]; //加速
uchar idata fall[20]; //减速
uchar idata plus[7]={0x01,0x03,0x02,0x06,0x04,0x05,0x00};
uchar idata minu[7]={0x05,0x04,0x06,0x02,0x03,0x01,0x00};
if (direct) cmode=plus;
else cmode =minu;
switch(mode)
{
case 1:
pp=rise; //PP指向定时器的值
break;
case 2:
pp=nowrate;
break;
case 3:
pp=fall;
break;
default:
pp=nowrate;
break;
}
do
{
P1=cmode[i]; //输出转动控制字
if(cmode[++i]==0x00)i=0;
while(pf); //等待定时器的时间到
pf=1;
if((mode==1)||(mode==3))pp+=2; //定时器的初值变化
}
while(sn--);
}
void intt0()interrupt 1 //T/C0中断服务程序
{
TL0=*pp; //赋初值,
nowrate[0]=TL0; //并记录定时器的初值
TH0=*(pp+1);
nowrate[1]=TH0;
pf=0;
}
void main()
{
TMOD=0x01;
TH0=0x00;
TL0=0x00;
TR0=1;
ET0=1;
EA=1;
contrl(1,1);
sn=10;
contrl(1,2);
sn=10;
contrl(1,3);
}
发表时间:2003年6月5日8:42:23