staticunsignedintcount;//計(jì)數(shù)staticintstep_index;//步進(jìn)索引數(shù),值為0-7staticbitturn;//步進(jìn)電機(jī)轉(zhuǎn)動(dòng)方向staticbitstop_fla">
歡迎來(lái)到天天文庫(kù)
瀏覽記錄
ID:37843220
大?。?11.00 KB
頁(yè)數(shù):4頁(yè)
時(shí)間:2019-06-01
《51單片機(jī)控制步進(jìn)電機(jī)程序及硬件電路圖》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、#includestaticunsignedintcount;//計(jì)數(shù)staticintstep_index;//步進(jìn)索引數(shù),值為0-7staticbitturn;//步進(jìn)電機(jī)轉(zhuǎn)動(dòng)方向staticbitstop_flag;//步進(jìn)電機(jī)停止標(biāo)志staticintspeedlevel;//步進(jìn)電機(jī)轉(zhuǎn)速參數(shù),數(shù)值越大速度越慢,最小值為1,速度最快staticintspcount;//步進(jìn)電機(jī)轉(zhuǎn)速參數(shù)計(jì)數(shù)voiddelay(unsignedintendcount);//延時(shí)函數(shù),延時(shí)為endcount*0.5毫秒voidgorun();//步進(jìn)電機(jī)控制步進(jìn)函數(shù)void
2、main(void){count=0;step_index=0;spcount=0;stop_flag=0;P1_0=0;P1_1=0;P1_2=0;P1_3=0;EA=1;//允許CPU中斷TMOD=0x11;//設(shè)定時(shí)器0和1為16位模式1ET0=1;//定時(shí)器0中斷允許TH0=0xFE;TL0=0x0C;//設(shè)定時(shí)每隔0.5ms中斷一次TR0=1;//開(kāi)始計(jì)數(shù)turn=0;speedlevel=2;delay(10000);speedlevel=1;do{speedlevel=2;delay(10000);speedlevel=1;delay(10000);stop_flag
3、=1;delay(10000);stop_flag=0;}while(1);}//定時(shí)器0中斷處理voidtimeint(void)interrupt1{TH0=0xFE;TL0=0x0C;//設(shè)定時(shí)每隔0.5ms中斷一次count++;spcount--;if(spcount<=0){spcount=speedlevel;gorun();}}voiddelay(unsignedintendcount){count=0;do{}while(count4、0;return;}switch(step_index){case0://0P1_0=1;P1_1=0;P1_2=0;P1_3=0;break;case1://0、1P1_0=1;P1_1=1;P1_2=0;P1_3=0;break;case2://1P1_0=0;P1_1=1;P1_2=0;P1_3=0;break;case3://1、2P1_0=0;P1_1=1;P1_2=1;P1_3=0;break;case4://2P1_0=0;P1_1=0;P1_2=1;P1_3=0;break;case5://2、3P1_0=0;P1_1=0;P1_2=1;P1_3=1;break;c5、ase6://3P1_0=0;P1_1=0;P1_2=0;P1_3=1;break;case7://3、0P1_0=1;P1_1=0;P1_2=0;P1_3=1;}if(turn==0){step_index++;if(step_index>7)step_index=0;}else{step_index--;if(step_index<0)step_index=7;}}
4、0;return;}switch(step_index){case0://0P1_0=1;P1_1=0;P1_2=0;P1_3=0;break;case1://0、1P1_0=1;P1_1=1;P1_2=0;P1_3=0;break;case2://1P1_0=0;P1_1=1;P1_2=0;P1_3=0;break;case3://1、2P1_0=0;P1_1=1;P1_2=1;P1_3=0;break;case4://2P1_0=0;P1_1=0;P1_2=1;P1_3=0;break;case5://2、3P1_0=0;P1_1=0;P1_2=1;P1_3=1;break;c
5、ase6://3P1_0=0;P1_1=0;P1_2=0;P1_3=1;break;case7://3、0P1_0=1;P1_1=0;P1_2=0;P1_3=1;}if(turn==0){step_index++;if(step_index>7)step_index=0;}else{step_index--;if(step_index<0)step_index=7;}}
此文檔下載收益歸作者所有