#include <reg51.h> //函数头文件
unsigned char temp; //声明变量
static unsigned char index=0;
unsigned char code BeatCode[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //步进电机正转控制
unsigned char code DefeatCode[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
unsigned long beats=0; //输出节拍数
void StartMotor (unsigned long angle);
sbit S2=P3^0;
sbit S3=P3^1;
void main()
{
EA=1;
TMOD=0x01;
TH0=0xf8;
TL0=0xcd;
ET0=1;
TR0=1;
StartMotor(360);
while(1);
}
void StartMotor (unsigned long angle)
{
EA=0;
beats=(angle*4076)/360;
EA=1;
}
void InterruptTimer0() interrupt 1
{
TH0=0xf8;
TL0=0xcd;
if(S2==0)
{
temp=P1;
temp=temp&0xf0;
temp=temp|BeatCode[index];
P1=temp;
index++;
index=index&0x07;
beats--;
}
else if(S3==0)
{
temp=P1;
temp=temp&0xf0;
temp=temp|DefeatCode[index];
P1=temp;
index++;
index=index&0x07;
beats--;
}
}
|