新聞中心

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

STC89C52單片機步進電機調(diào)試

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

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

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

#define uchar unsigned char
#define uint unsigned int

#define MotorData P2 //步進電機控制接口定義

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

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

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

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

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



評論


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

關(guān)閉