#include <reg52.h>
#define uchar unsigned char
long g_Beats = 0;
void Timer0Init();
void TurnMotor(long angle);
void main()
{
Timer0Init();
TurnMotor(360);
while(1);
}
void Timer0Init()
{
TMOD |= 0x01;
TH0 = 0xFC; //定时1个MS;
TL0 = 0x66;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void TurnMotor(long angle)
{
EA = 0;
g_Beats = (angle * 4076) / 360;
EA = 1;
}
void Timer0IRQ() interrupt 1
{
static uchar index = 0;
uchar temp;
uchar code BeatCode[8] = {0x01,0x03,0x02,0x06,
0x04,0x0C,0x08,0x09};
TH0 = 0xFC;
TL0 = 0x66;
if (g_Beats != 0)
{
temp = P1; //用temp暂存P1口的值
temp = temp & 0xF0; //只对P1口的低四位进行操作,要保护好高四位
P1 = temp | BeatCode[index]; //把节拍数送到P1口的低四位
index++;
index &= 0x07; //8个节拍后重新归0;
g_Beats--; //节拍数递减1;
}
else
P1 |= 0x0F; //节拍数等于零时,关闭步进电机。
}
|