#include <reg52.h>
void TURNMOTOR(unsigned long angle);
void main()
{
TURNMOTOR(360*25);
while(1);
}
void delay()
{
unsigned int i=500;//定时5009us=4.5ms
while(i--);
}
void TURNMOTOR(unsigned long angle)
{
unsigned char tmp; //临时变量
unsigned char index = 0; //节拍输出索引
unsigned long beats = 0; //所需节拍总数
unsigned char code BeatCode[]={
0xe, 0xc, 0xd, 0x9, 0xb, 0x3 ,0x7, 0x6
//0x1,0x3,0x2,0x6,0x4,0xc,0x8,0x9
};
beats= (angle*64) /360;
while (beats--)
{
tmp=P1;
tmp=tmp & 0xf0;
tmp=tmp | BeatCode[index];
P1= tmp;
index++;
//index = index & 0x07;
while(index==8) {index=0;}
delay();
}
P1=P1 & 0xfF;//关闭 电动机所有的相位
}
|