這是程序代碼
#include<reg52.h>
#include <intrins.h>
sbit wave=P0^0;
unsigned char Flag;//定義正反轉和停止標志位
//sbit KEY = P3^3;
unsigned char code F_Rotation[4]={0xf1,0xf2,0xf4,0xf8}; //正轉表格
unsigned char code B_Rotation[4]={0xf8,0xf4,0xf2,0xf1}; //反轉表格
void Delay(unsigned int i)//延時
{
while(--i);
}
void T0ISR(void) interrupt 1
{
unsigned char i;
unsigned char a;
a=TL1;
while(a>3&& a<11)
{
for(i=0;i<4;i++) //4相
{
P2=F_Rotation; //輸出對應的相 可以自行換成反轉表格
Delay(500); //改變這個參數可以調整電機轉速 ,數字越小,轉速越大
}
}
while(a<3||a>11)
{
for(i=0;i<4;i++) //4相
{
P2=B_Rotation; //輸出對應的相
Delay(500); //改變這個參數可以調整電機轉速 ,數字越小,轉速越大
}
}
a=0;
TL1=0;
TL0=0x18;
TH0=0x1c;
}
void delay1()
{
unsigned char a,b,c;
for(a=0;a<1;a++)
for(b=0;b<1;b++)
for(c=0;c<61;c++);
}
void main()
{
TMOD=0x40;
TL0=0x18;
TH0=0x1c;
TR0=1;
TR1=1;
ET0=1;
EX1=1; //外部中斷0開
IT1=1; //邊沿觸發
EA=1;
while(1)
{
wave=0;
delay1();
wave=1;
}
}
|