|
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint tt0;
uchar temp,i=0,shu,flag,keynum;
uchar code table1[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //zhen
uchar code table2[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //fang
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
{
keynum=0;
EA=1;
TMOD=0X01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
ET0=1;
TR0=1;
}
void keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(1);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:shu=1;
break;
case 0xde:shu=2;
break;
case 0xbe:shu=3;
break;
case 0x7e:shu=4;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
if(shu==1&flag==1)
{
flag=0;
P0=table1[i];
i++;
if(i==8)
i=0;
}
if(shu==2&flag==1)
{
flag=0;
P0=table2[i];
i++;
if(i==8)
i=0;
}
if(shu==3&flag==1)
{
flag=0;
P0=0;
}
}
}
void timer0() interrupt 1
{
TH0=(65535-5000)/256;
TL0=(65535-5000)%256;
tt0++;
if(tt0==20)
{
flag=1;
tt0=0;
}
}
这个是最基本的控制步进电机正反的程序,但是如何调节速度呢?求各位分享一下!
|
|