新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STC89C52單片機(jī)步進(jìn)電機(jī)調(diào)試

STC89C52單片機(jī)步進(jìn)電機(jī)調(diào)試

作者: 時(shí)間:2016-11-21 來(lái)源:網(wǎng)絡(luò) 收藏
///////////////////////////////////////////////////////////////////////////

實(shí)現(xiàn)功能: 先讓實(shí)驗(yàn)板上的步進(jìn)電機(jī)從正向加速——勻速——減速,然后讓步進(jìn)電機(jī)
停止,再讓步進(jìn)電機(jī)從方向加速——勻速——減速,然后停止,然后就這
樣不停的循環(huán)
實(shí)驗(yàn)板型號(hào):BS-XYD-C52
實(shí)驗(yàn)名稱:步進(jìn)電機(jī)測(cè)試實(shí)驗(yàn)
編寫人: 謝應(yīng)東
編寫日期: 2012-4-30
///////////////////////////////////////////////////////////////////////////
#include

本文引用地址:http://www.2s4d.com/article/201611/319223.htm

#define uchar unsigned char
#define uint unsigned int

#define MotorData P2 //步進(jìn)電機(jī)控制接口定義

uchar code Phase_Forward[4]={0xf8,0xf4,0xf2,0xf1};//正轉(zhuǎn) 電機(jī)導(dǎo)通相序 D-C-B-A
uchar code Phase_Reverse[4]={0xf1,0xf2,0xf4,0xf8};//反轉(zhuǎn) 電機(jī)導(dǎo)通相序 A-B-C-D
uchar speed;
///////////////////////////////////////////////////////////////////////////
函數(shù)名稱:毫秒延時(shí)函數(shù)
函數(shù)功能:實(shí)現(xiàn)毫秒級(jí)的延時(shí)
參數(shù)介紹:Delay_MS: 定義需要延時(shí)的毫秒的數(shù)值
iNumber: 記錄Delay_MS的數(shù)值,以for語(yǔ)句實(shí)現(xiàn)所要求的延時(shí)
iValue: 要延時(shí)毫秒所要進(jìn)行的循環(huán)數(shù)值,本數(shù)值為實(shí)際測(cè)得
返回值: 無(wú)
注意事項(xiàng):本實(shí)驗(yàn)是在所用晶振為12M的前提下實(shí)現(xiàn)的毫秒延時(shí),本函數(shù)是通過(guò)循環(huán)的形
式完成,所以如果改變了晶振的頻率,請(qǐng)做相應(yīng)的改變
///////////////////////////////////////////////////////////////////////////
void DelayMs(uint Delay_MS)
{
uint iNumber,iValue;
for(iNumber=0;iNumber{
iValue=107;
while(iValue--);
}
}
///////////////////////////////////////////////////////////////////////////
函數(shù)名稱:Motor_Forward
函數(shù)功能:完成步進(jìn)電機(jī)的正向加速——勻速——減速的功能
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):無(wú)
///////////////////////////////////////////////////////////////////////////

void Motor_Forward(void)
{
uint cNumber,cTempValue;
speed=25; //定義步進(jìn)電機(jī)加速的初值
cTempValue=1000; //定義正向加速后勻速的時(shí)間
do
{
for(cNumber=0;cNumber<4;cNumber++) //驅(qū)動(dòng)步進(jìn)電機(jī)正向轉(zhuǎn)動(dòng)
{
MotorData=Phase_Forward[cNumber];
DelayMs(speed);//調(diào)整步進(jìn)電機(jī)的速度
}
speed--; //正向加速
}while(speed!=3);
do//驅(qū)動(dòng)步進(jìn)電機(jī)勻速轉(zhuǎn)動(dòng)
{
for(cNumber=0;cNumber<4;cNumber++)
{
MotorData=Phase_Forward[cNumber];
DelayMs(speed);
}
cTempValue--;
}while(cTempValue!=1);

do //驅(qū)動(dòng)步進(jìn)電機(jī)反向轉(zhuǎn)動(dòng)
{
for(cNumber=0;cNumber<4;cNumber++)
{
MotorData=Phase_Forward[cNumber];
DelayMs(speed);
}
speed++; //調(diào)整步進(jìn)電機(jī)的速度
}while(speed!=25);
}
///////////////////////////////////////////////////////////////////////////

函數(shù)名稱:Motor_Reverse
函數(shù)功能:完成步進(jìn)電機(jī)的方向加速——勻速——減速的功能
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):無(wú)
///////////////////////////////////////////////////////////////////////////
void Motor_Reverse(void)
{
uint cNumber,cTempValue;
speed=25; //定義步進(jìn)電機(jī)加速的初始值
cTempValue=1000; //定義步進(jìn)電機(jī)勻速的時(shí)間
do //完成步進(jìn)的電機(jī)的方向加速
{
for(cNumber=0;cNumber<4;cNumber++) //驅(qū)動(dòng)步進(jìn)電機(jī)
{
MotorData=Phase_Reverse[cNumber];
DelayMs(speed); //調(diào)整步進(jìn)電機(jī)的速度
}
speed--;
}while(speed!=3);
do //完成步進(jìn)電機(jī)的勻速過(guò)程
{
for(cNumber=0;cNumber<4;cNumber++)
{
MotorData=Phase_Reverse[cNumber];
DelayMs(speed);
}
cTempValue--;
}while(cTempValue!=1);

do //完成步進(jìn)電機(jī)的反向減速過(guò)程
{
for(cNumber=0;cNumber<4;cNumber++)
{
MotorData=Phase_Reverse[cNumber];
DelayMs(speed); //調(diào)整步進(jìn)電機(jī)的速度
}
speed++;
}while(speed!=25);
}
///////////////////////////////////////////////////////////////////////////
函數(shù)名稱:MotorStop
函數(shù)功能:讓步進(jìn)電機(jī)停止轉(zhuǎn)動(dòng),然后再延時(shí)0.5秒
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):無(wú)
///////////////////////////////////////////////////////////////////////////
void Motor_Stop(void)
{
MotorData=0xf0;
DelayMs(500);
}
///////////////////////////////////////////////////////////////////////////
函數(shù)名稱:主函數(shù)
函數(shù)功能:完成點(diǎn)亮一個(gè)發(fā)光二級(jí)管,延時(shí)0.3秒,然后熄滅0.3秒,這樣依次循環(huán)
參數(shù)介紹:無(wú)
返回值: 無(wú)
注意事項(xiàng):無(wú)
///////////////////////////////////////////////////////////////////////////
void main(void)
{
DelayMs(50);//等待系統(tǒng)穩(wěn)定
while(1)
{
Motor_Forward();//步進(jìn)電機(jī)正向加速——勻速——減速的函數(shù)
Motor_Stop(); //步進(jìn)電機(jī)停止轉(zhuǎn)動(dòng)
Motor_Reverse();//步進(jìn)電機(jī)反向加速——勻速——減速的函數(shù)
Motor_Stop();//步進(jìn)電機(jī)停止轉(zhuǎn)動(dòng)
}
}



評(píng)論


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

關(guān)閉