|
本帖最后由 閆明濤 于 2020-10-26 16:06 編輯
- #include <reg52.h>
- unsigned long beats=0;
- void startmotor(unsigned long angle);
- void main()
- {
- EA=1;
- TMOD=0X01;
- TH0=0XF8;//2ms
- TL0=0XCD;
- ET0=1;
- TR0=1;
- startmotor(360);
- while(1);
- }
- void startmotor(unsigned long angle)
- {
- EA=0;
- beats=(angle*4076)/360;//防止因為中斷導(dǎo)致計算錯誤,因為以后的中斷中需要使用到beats
- EA=1;
- }
- void InterruptTimer0() interrupt 1
- {
- unsigned char tmp;
- static unsigned char index=0;
- unsigned char code beetcode[8]={0xe,0xc,0xd,0x9,0xb,0x3,0x7,0x6};//節(jié)拍對應(yīng)IO口代碼
- TH0=0XF8;
- TL0=0XCD;
- if(beats!=0)
- {
- tmp=P1&0xf0;
- tmp|=beetcode[index];
- P1=tmp;
- index++;
- if(index>=8)index=0;
- beats--;
- }
- else
- P1=P1|0X0F;//關(guān)閉所有相位
- }
- 電機引腳接P1^0~P1^3;
- 主函數(shù)調(diào)用void startmotor(unsigned long angle);即可設(shè)置指定角度旋轉(zhuǎn)精度為1度
復(fù)制代碼
|
|