|
|
步進(jìn)電機(jī)任意角度控制并顯示- /*****************************************************
- 程序說(shuō)明
- 通過(guò)改變脈沖個(gè)數(shù)改變步進(jìn)電機(jī)的角度,通過(guò)按鍵可以讓步進(jìn)
- 電機(jī)停在任意角度及正反轉(zhuǎn) 并在數(shù)碼管顯示,正轉(zhuǎn)顯示1,反轉(zhuǎn)顯示2
- 單片機(jī) :STC12C5A60S2
- 晶振 :11.0592MHZ
- 步進(jìn)電機(jī)驅(qū)動(dòng)芯片:ULN2003A
- 作者 :蘇義江
- 時(shí)間 :2015年6月5日
- 八拍方式驅(qū)動(dòng) :順序?yàn)锳 AB B BC C CD D DA
- zhengzhuan[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
- fanzhuan[] ={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
- *****************************************************/
- #include<12c5a.H>
- //#include <reg52.h>
- //#define uchar unsigned char
- //#define uint unsigned int
- sbit key=P1^0; //按鍵控制步進(jìn)電機(jī)的正轉(zhuǎn)方向
- sbit key1=P1^1; //按鍵控制步進(jìn)電機(jī)的反轉(zhuǎn)方向
- sbit dula =P0^7; //段鎖存使能
- sbit wela =P0^6; //位鎖存使能
- uchar code zhengzhuan[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
- uchar code fanzhuan[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
- //共陽(yáng)段碼
- uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
- 0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,
- 0xa1,0x86,0x8e,0x89};
- uchar code tab_f[]={0x0c,0x06,0x03,0x09,
- 0x0c,0x06,0x03,0x09};//2相勵(lì)磁 反傳
- uchar code tab_z[]={0x09,0x03,0x06,0x0c,
- 0x09,0x03,0x06,0x0c};//2相勵(lì)磁 正傳
- void Delay( uint i)//延時(shí)
- {
- uint x,y;
- for(x=i;x>0;x--)
- for(y=300;y>0;y--);
- }
- void display()
- {
- wela=1;
- P2=0xfe;
- wela=0;
- dula =1;
- P2=0xf9;//正轉(zhuǎn)顯示1
- Delay( 10);
- P2=0xff;
- dula =0;
- }
- void display1()
- {
- wela=1;
- P2=0xf7;
- wela=0;
- dula =1;
- P2=0xa4;//反轉(zhuǎn)顯示2
- Delay( 10);
- P2=0xff;
- dula =0;
- }
- void main()
- {
- uint i,j;//j代表脈沖.i代表數(shù)據(jù)
- P0M1=0; //設(shè)置P0口為強(qiáng)推免輸出模式,下同
- P0M0=0XFF;
- while(1)
- {
-
- //j等于13個(gè)脈沖,電機(jī)旋轉(zhuǎn)一周。改變j的值就會(huì)改變電機(jī)的角度
- for(j=3;j>0;j--)//正轉(zhuǎn)4分之一周
- {
- for(i=0;i<8;i++)
- {
- display();
- P0=zhengzhuan[i];//正轉(zhuǎn)數(shù)組
- Delay(5);//5秒旋轉(zhuǎn)一周 改變延時(shí)改變速度
- }
- }
- Delay(200);//正反轉(zhuǎn)之間延時(shí)
- for(j=3;j>0;j--)//反轉(zhuǎn)4分之一周
- {
- for(i=0;i<8;i++)
- {
- display1();
- P0=fanzhuan[i];//反轉(zhuǎn)數(shù)組
- Delay(5);//5秒旋轉(zhuǎn)一周 改變延時(shí)改變速度
- }
- }
- Delay(200);
- }
- /********下面程序都正常控制步進(jìn)電機(jī)狀態(tài)*****************/
- /* while(1)
- {
- if(!key)
- Delay(10);
- {
- if(!key)
- {
- display();
- Delay(10);
- for(j=7;j>0;j--)//j=7個(gè)脈沖,正轉(zhuǎn)半周
- {
- for(i=0;i<8;i++)
- {
- P0=zhengzhuan[i];//正轉(zhuǎn)數(shù)組
- Delay(10);
- }
- }
- }
- while(!key);
- }
- if(!key1)
- Delay(10);
- {
- if(!key1)
- {
- display1();
- Delay(10);
- for(j=7;j>0;j--)//j=7個(gè)脈沖,反轉(zhuǎn)半周
- {
- for(i=0;i<8;i++)
- {
- P0=fanzhuan[i];//反轉(zhuǎn)數(shù)組
- Delay(10);
- }
- }
- }
- while(!key1);
- }
-
- }*/
- /* while(1)
- {
-
- //j等于13個(gè)脈沖,電機(jī)旋轉(zhuǎn)一周。改變j的值就會(huì)改變電機(jī)的角度
- for(j=13;j>0;j--)//正轉(zhuǎn)一周
- {
- for(i=0;i<8;i++)
- {
- display();
- P0=zhengzhuan[i];//正轉(zhuǎn)數(shù)組
- Delay(5);//6秒旋轉(zhuǎn)一周
- }
- }
- Delay(2000);
- for(j=13;j>0;j--)//反轉(zhuǎn)一周
- {
- for(i=0;i<8;i++)
- {
- display1();
- P0=fanzhuan[i];//反轉(zhuǎn)數(shù)組
- Delay(5);//6秒旋轉(zhuǎn)一周
- }
- }
- Delay(2000);
- }
- */
-
- /* while(1)
- {
-
- //j等于13個(gè)脈沖,電機(jī)旋轉(zhuǎn)一周。改變j的值就會(huì)改變電機(jī)的角度
- for(j=25;j>0;j--)//正轉(zhuǎn)二周
- {
- for(i=0;i<8;i++)
- {
- display();
-
- P0=zhengzhuan[i];//正轉(zhuǎn)數(shù)組
- Delay(3);
-
- }
- }
- Delay(2000);
- for(j=25;j>0;j--)//反轉(zhuǎn)一周
- {
- for(i=0;i<8;i++)
- {
- display1();
-
- P0=fanzhuan[i];//反轉(zhuǎn)數(shù)組
- Delay(3);
-
- }
- }
- Delay(2000);
- }*/
-
-
- /* while(1)
- {
-
- //j等于13個(gè)脈沖,電機(jī)旋轉(zhuǎn)一周。改變j的值就會(huì)改變電機(jī)的角度
- for(j=250;j>0;j--)//正轉(zhuǎn)二十周
- {
- for(i=0;i<8;i++)
- {
- display();
- P0=zhengzhuan[i];//正轉(zhuǎn)數(shù)組
- Delay(6); //延時(shí)控制轉(zhuǎn)速不能小于500
-
- }
- }
- Delay(2000);
- for(j=250;j>0;j--)//反轉(zhuǎn)二十周
- {
- for(i=0;i<8;i++)
- {
- display1();
- P0=fanzhuan[i];//反轉(zhuǎn)數(shù)組
- Delay(6); //延時(shí)控制轉(zhuǎn)速不能小于500
-
- }
- }
- Delay(20000);
- }*/
- /* while(1)
- {
-
- for(i=0;i<8;i++)
- {
- if(key) //按鍵未按下,正轉(zhuǎn)
- {
- P0=zhengzhuan[i];//正轉(zhuǎn)數(shù)組
- Delay(8);
- }
- else //按鍵按下,反轉(zhuǎn)
- {
- P0=fanzhuan[i];//反轉(zhuǎn)數(shù)組
- Delay(8);
- }
- }
- }*/
- }
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|