- #include <reg52.h>
- unsigned char Flag;//定義正反轉(zhuǎn)和停止標志位
- sbit KEY = P3^3;
- unsigned char code F_Rotation[4]={0xf1,0xf2,0xf4,0xf8}; //正轉(zhuǎn)表格
- unsigned char code B_Rotation[4]={0xf8,0xf4,0xf2,0xf1}; //反轉(zhuǎn)表格
- /******************************************************************/
- /* 延時函數(shù) */
- /******************************************************************/
- void Delay(unsigned int i)//延時
- {
- while(--i);
- }
- /******************************************************************/
- /* 主函數(shù) */
- /******************************************************************/
- main()
- {
- unsigned char i;
- EX1=1; //外部中斷0開
- IT1=1; //邊沿觸發(fā)
- EA=1; //全局中斷開
- while(Flag==0)
- {
- P0=0x71;//顯示 F 標示正轉(zhuǎn)
- for(i=0;i<4;i++) //4相
- {
- P1=F_Rotation[i]; //輸出對應的相 可以自行換成反轉(zhuǎn)表格
- Delay(500); //改變這個參數(shù)可以調(diào)整電機轉(zhuǎn)速 ,數(shù)字越小,轉(zhuǎn)速越大
- }
- }
- while(Flag==1)
- {
- P0=0x7C;//顯示 b 標示反轉(zhuǎn)
- for(i=0;i<4;i++) //4相
- {
- P1=B_Rotation[i]; //輸出對應的相
- Delay(500); //改變這個參數(shù)可以調(diào)整電機轉(zhuǎn)速 ,數(shù)字越小,轉(zhuǎn)速越大
- }
- }
- while(Flag==2) //停止
- {
- P0=0x6D;// 顯示 S
- P1=0;
- }
- }
- /******************************************************************/
- /* 中斷入口函數(shù) */
- /******************************************************************/
- void ISR_Key(void) interrupt 2 using 1
- {
- Delay(500);
- if(!KEY)
- {
- Flag++; //s3按下觸發(fā)一次
- if(Flag==3)
- Flag=0;
- }
- }
復制代碼 |