#include <reg51.h> //函数头文件
#define uint unsigned int
#define uchar unsigned char
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 delay(uint z);
void main() //主函数
{
EA=1; //开启总中断
TMOD=0x01; //选择定时器0,工作方式1
TH0=0xf8; //装在初始值
TL0=0xcd;
ET0=1; //定时器T0中断
TR0=1; //开启定时器0
StartMotor(360);
while(1);
}
void StartMotor (unsigned long angle) //控制函数
{
EA=0; //关闭总中断
beats=(angle*4076)/360; //计算输出节拍数
EA=1; //开启总中断
}
void InterruptTimer0() interrupt 1 //中断工作方式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--;
}
}
void delkay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
|