標(biāo)題:
基于51單片機的電機控制方向及速度程序
[打印本頁]
作者:
ml5780
時間:
2017-11-29 09:51
標(biāo)題:
基于51單片機的電機控制方向及速度程序
使用STC單片機控制步進(jìn)電機的方向及速度
0.png
(10.64 KB, 下載次數(shù): 125)
下載附件
2017-11-29 21:31 上傳
單片機源程序如下:
#include "user.h"
uint MotorTimer = 0;
void Timer0Init(void) //100us@11.0592MHz
{
AUXR &= 0x7F; //定時器時鐘12T模式
TMOD &= 0xF0; //設(shè)置定時器模式
TMOD |= 0x01; //設(shè)置定時器模式
TL0 = 0xA4; //設(shè)置定時初值
TH0 = 0xFF; //設(shè)置定時初值
TF0 = 0; //清楚TF1標(biāo)志
EA = 1;
ET0 = 1;
TR0 = 1;
}
/***************/
void Timer1Init(void) //20us@11.0592MHz
{
AUXR &= 0xBF; //定時器時鐘12T模式
TMOD &= 0x0F; //設(shè)置定時器模式
TMOD |= 0x10; //設(shè)置定時器模式
TL1 = 0xEE; //設(shè)置定時初值
TH1 = 0xFF; //設(shè)置定時初值
TF1 = 0; //清楚TF1標(biāo)志
ET1 = 1;
TR1 = 1;
}
/*************/
//定時器0中斷服務(wù)函數(shù)
//100us 定時器中斷服務(wù)函數(shù)
void Timer0Interrupt(void) interrupt 1
{
TF0 = 0;
TL0 = 0xA4; //設(shè)置定時初值
TH0 = 0xFF; //設(shè)置定時初值
if(UARTTimeout)
{
UARTTimeout--;
if(UARTTimeout==0)
{
if(RX1_Cnt >= RXNum)
{
RXEndFlag = 1;
}
}
}
}
//定時器1中斷服務(wù)函數(shù)
//50us 控制氣缸,光電開關(guān)輸入
/********************/
void Timer1Interrupt(void) interrupt 3
{
TF1 = 0;
TL1 = 0xEE; //設(shè)置定時初值
TH1 = 0xFF; //設(shè)置定時初值
/***********
if((ToBottomFlag)||(ToMiddleFlag)||(ToTopFlag))
{
MotorTimer ++;
}
else
{
MotorTimer = 0;
}
**************/
}
/***********************
if((ToBottomFlag)||(ToMiddleFlag)||(ToTopFlag))
{
MotorTimer ++;
}
else
{
MotorTimer = 0;
}
/*****************
if(ToMiddleFlag) //電機位置移動到中間脈沖計數(shù)
{
MotorTimer ++;
}
else
{
MotorTimer = 0;
}
if(ToTopFlag)
{
MotorTimer ++;
}
else
{
MotorTimer = 0;
}
/*******************
/////////////////////
if(MotorBottomFlag)
{
MotorTimerB ++;
}
else
{
MotorTimerB = 0;
}
if((MotorTimerB >= 10)&&(PulseCount >=SlowCount)) //40us脈沖翻轉(zhuǎn)一次
{
PulseCount--;
MotorTimerB = 0;
MotorPulse = ~MotorPulse;
}
if((MotorTimerB >= 20)&&(0 < PulseCount)&&(PulseCount < SlowCount)) //80us翻轉(zhuǎn)一次
{
if(PulseCount)
PulseCount--;
MotorTimerB = 0;
MotorPulse = ~MotorPulse;
}
////////////////////////////////////////////////////////
if(MotorMiddleFlag) //電機位置移動到中間脈沖計數(shù)
{
MotorTimerM ++;
}
else
{
MotorTimerM = 0;
}
if(MotorMiddleFlag == 1) //從底部運行至中間
{
if((MotorTimerM >= 10)&&(PulseCount <= (MiddleCount-SlowCount)))
{
PulseCount ++;
MotorTimerM = 0;
MotorPulse = ~MotorPulse;
}
if ((MotorTimerM >= 20)&&((MiddleCount-SlowCount)<PulseCount <MiddleCount))
{
PulseCount ++;
MotorTimerM = 0;
MotorPulse = ~MotorPulse;
}
}
if (MotorMiddleFlag == 2) //從頂部運行至中間
{
if((MotorTimerM >= 10)&&((MiddleCount+100) <= PulseCount))
{
PulseCount --;
MotorTimerM = 0;
MotorPulse = ~MotorPulse;
}
if ((MotorTimerM >= 20)&&(MiddleCount<PulseCount)&&( PulseCount<(MiddleCount+SlowCount)))
{
PulseCount --;
MotorTimerM = 0;
MotorPulse = ~MotorPulse;
}
}
/////////////////////////////////////////////////////////
if(MotorTopFlag)
{
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
FatpProgramProjectV21127.zip
(138.93 KB, 下載次數(shù): 76)
2017-11-29 09:51 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
噫~噫~~~噫!
時間:
2018-5-13 22:04
非常感謝
作者:
bbcs852
時間:
2020-3-20 05:26
學(xué)習(xí)了
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1