新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 步進(jìn)電機(jī)正反轉(zhuǎn)程序

步進(jìn)電機(jī)正反轉(zhuǎn)程序

作者: 時間:2016-11-24 來源:網(wǎng)絡(luò) 收藏

#include //51芯片管腳定義頭文件
#include //內(nèi)部包含延時函數(shù) _nop_();
#define uchar unsigned char
#define uint unsigned int
uchar code FFW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //四相八拍正轉(zhuǎn)編碼
uchar 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; //蜂鳴器
/********************************************************/
/*
/* 延時t毫秒
/* 11.0592MHz時鐘,延時約1ms
/*
/********************************************************/
void delay(uint t)
{
uint k;
while(t--)
{
for(k=0; k<125; k++)
{ }
}
}

本文引用地址:http://butianyuan.cn/article/201611/320779.htm

/**********************************************************/
void delayB(uchar x) //x*0.14MS
{
uchar i;
while(x--)
{
for (i=0; i<13; i++)
{ }
}
}

/**********************************************************/
void beep()
{
uchar i;
for (i=0;i<100;i++)
{
delayB(4);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //關(guān)閉蜂鳴器
}

/********************************************************/
/*
/*步進(jìn)電機(jī)正轉(zhuǎn)
/*
/********************************************************/
void motor_ffw()
{
uchar i;
uint j;
for (j=0; j<8; j++) //轉(zhuǎn)1*n圈
{
if(K3==0)
{break;} //退出此循環(huán)程序
for (i=0; i<8; i++) //一個周期轉(zhuǎn)45度
{
P1 = FFW[i]; //取數(shù)據(jù)
delay(2); //調(diào)節(jié)轉(zhuǎn)速
}
}
}

/********************************************************/
/*
/*步進(jìn)電機(jī)反轉(zhuǎn)
/*
/********************************************************/
void motor_rev()
{
uchar i;
uint j;
for (j=0; j<8; j++) //轉(zhuǎn)1×n圈
{
if(K3==0)
{break;} //退出此循環(huán)程序
for (i=0; i<8; i++) //一個周期轉(zhuǎn)45度
{
P1 = REV[i]; //取數(shù)據(jù)
delay(2); //調(diào)節(jié)轉(zhuǎn)速
}
}
}

/********************************************************
*
* 主程序
*
*********************************************************/

main()
{
uchar r,N=64; //N 步進(jìn)電機(jī)運轉(zhuǎn)圈數(shù)
while(1)
{
if(K1==0)
{
beep();
for(r=0;r {
motor_ffw(); //電機(jī)正轉(zhuǎn)
if(K3==0)
{beep();break;} //退出此循環(huán)程序
}
}
else if(K2==0)
{
beep();
for(r=0;r {
motor_rev(); //電機(jī)反轉(zhuǎn)
if(K3==0)
{beep();break;} //退出此循環(huán)程序
}
}
else
P1 = 0xf0;
}
}

/********************************************************/



評論


技術(shù)專區(qū)

關(guān)閉