|
- #include <reg52.h> //51芯片管腳定義頭文件
- #include <intrins.h> //內(nèi)部包含延時(shí)函數(shù) _nop_();
- unsigned char code FFW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //正轉(zhuǎn)相序代碼
- unsigned char code REV[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //反轉(zhuǎn)相序代碼
- sbit K1 = P3^2; //正轉(zhuǎn)
- sbit K2 = P3^3; //反轉(zhuǎn)
- sbit K3 = P3^4; //停止
- sbit BEEP = P3^6; //蜂鳴器
- /*********************************************************
- 1ms延時(shí)
- *********************************************************/
- void delay(unsigned int t)
- {
- unsigned int k;
- while(t--)
- for(k=0; k<125; k++);
- }
- /**********************************************************
- 0.1ms延時(shí)
- **********************************************************/
- void delayB(unsigned char n)
- {
- unsigned char i;
- while(n--)
- for (i=0; i<13; i++);
- }
- /**********************************************************/
- /*蜂鳴器*/
- void beep()
- {
- unsigned char i;
- for(i=0;i<100;i++){
- BEEP=~BEEP;
- delayB(4);
- }
- BEEP=1;
- }
- /********************************************************/
- /*
- /*步進(jìn)電機(jī)正轉(zhuǎn)
- /*
- /********************************************************/
- void motor_ffw()
- {
- unsigned char i;
- unsigned int j;
- for (j=0; j<8; j++) //轉(zhuǎn)1*n圈
- {
- if(K3==0){
- delay(15); //消陡
- if(K3==0)
- break;
- } //退出此循環(huán)程序
- for (i=0; i<8; i++) //一個(gè)周期轉(zhuǎn)45度
- {
- P1 = FFW[i]; //取數(shù)據(jù)
- delay(2); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- /********************************************************/
- /*
- /*步進(jìn)電機(jī)反轉(zhuǎn)
- /*
- /********************************************************/
- void motor_rev()
- {
- unsigned char i;
- unsigned int j;
- for (j=0; j<8; j++) //轉(zhuǎn)1×n圈
- {
- if(K3==0){
- delay(15); //消陡
- if(K3==0)
- break;
- } //退出此循環(huán)程序
- for (i=0; i<8; i++) //一個(gè)周期轉(zhuǎn)45度
- {
- P1 = REV[i]; //取數(shù)據(jù)
- delay(2); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- }
- /********************************************************
- *
- * 主程序
- *
- *********************************************************/
- void main()
- {
-
- unsigned char r,N=64; //N 步進(jìn)電機(jī)運(yùn)轉(zhuǎn)圈數(shù) 因?yàn)椴竭M(jìn)電機(jī)是減速步進(jìn)電機(jī) 減速比是1/64 所以這里N=64時(shí) 步進(jìn)電機(jī)外部的主軸轉(zhuǎn)1圈
- while(1)
- {
- if(K1==0)
- {
- delay(10);
- if(K1==0){
- beep();
- for(r=0;r<N;r++)
- {
- motor_ffw(); //電機(jī)正轉(zhuǎn)
- if(K3==0){
- delay(10);
- if(K3==0){
- beep();
- break;
- }
- } //退出此循環(huán)程序
- }
- }
- }
- else if(K2==0)
- {
- delay(10);
- if(K2==0){
- beep();
- for(r=0;r<N;r++)
- {
- motor_rev(); //電機(jī)反轉(zhuǎn)
- if(K3==0){
- delay(10);
- if(K3==0){
- beep();
- break;
- }
- } //退出此循環(huán)程序
- }
- }
- }
- else
- P1 = 0xf0; //關(guān)閉電機(jī)
- }
-
- }
- /********************************************************/
-
復(fù)制代碼
|
|