標(biāo)題:
單片機(jī)按鍵控制步進(jìn)電機(jī)的源碼
[打印本頁(yè)]
作者:
流浪的熊貓
時(shí)間:
2018-5-31 18:37
標(biāo)題:
單片機(jī)按鍵控制步進(jìn)電機(jī)的源碼
單片機(jī)源程序如下:
/*接線方式:
IN1 ---- P00
IN2 ---- P01
IN3 ---- P02
IN4 ---- P03
*********************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define MotorData P0 //步進(jìn)電機(jī)控制接口定義
uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正轉(zhuǎn) 電機(jī)導(dǎo)通相序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反轉(zhuǎn) 電機(jī)導(dǎo)通相序 A-B-C-D
uchar count,count1=20;//定義全局變量
sbit s1=P1^0;// 順時(shí)針轉(zhuǎn)
sbit s2=P1^1;// 逆時(shí)針轉(zhuǎn)
sbit s3=P1^2;// 停止
sbit s4=P1^3;// 加速
sbit s5=P1^4;// 減速
//ms延時(shí)函數(shù)
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
//順時(shí)針轉(zhuǎn)動(dòng)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void MotorCW(void)
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phasecw[i];
Delay_xms(count1);//轉(zhuǎn)速調(diào)節(jié)
}
}
//逆時(shí)針轉(zhuǎn)動(dòng)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void MotorCCW(void)
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phaseccw[i];
Delay_xms(count1);//轉(zhuǎn)速調(diào)節(jié)
}
}
//停止轉(zhuǎn)動(dòng)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void MotorStop(void)
{
MotorData=0x00;
}
//按鍵掃描
void keyscan()
{
if(s1==0)
{Delay_xms(5);
if(s1==0)
{while(!s1);
count=1; //選擇順時(shí)針轉(zhuǎn)動(dòng)
}
}
if(s2==0)
{Delay_xms(5);
if(s2==0)
{while(!s2);
count=2; //選擇逆時(shí)針轉(zhuǎn)動(dòng)
}
}
if(s3==0)
{Delay_xms(5);
if(s3==0)
{while(!s3);
count=3; //選擇停止轉(zhuǎn)動(dòng)
}
}
if(s4==0)
{Delay_xms(5);
if(s4==0)
{while(!s4);
count1--; //count減小會(huì)加速
if(count1<=4)
count1=4;
}
}
if(s5==0)
{Delay_xms(5);
if(s5==0)
{while(!s5);
count1++; //count增加會(huì)減速
if(count1>=50)
count1=50;
}
}
}
//主函數(shù)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void main(void)
{
Delay_xms(50);//等待系統(tǒng)穩(wěn)定
while(1)
{ keyscan();
switch(count)
{
case 1: MotorCW(); //順時(shí)針轉(zhuǎn)動(dòng)
break;
case 2: MotorCCW(); //逆時(shí)針轉(zhuǎn)動(dòng)
break;
case 3: MotorStop(); //停止轉(zhuǎn)動(dòng)
break;
}
}
}
復(fù)制代碼
所有資料51hei提供下載:
按鍵控制步進(jìn)電機(jī).zip
(1.64 MB, 下載次數(shù): 40)
2018-5-31 18:37 上傳
點(diǎn)擊文件名下載附件
程序
下載積分: 黑幣 -5
作者:
無(wú)痕12
時(shí)間:
2019-4-17 23:08
樓主,你這個(gè)有電路圖嗎?或者說(shuō)你這個(gè)按鍵需要外接上拉電阻嗎?
作者:
無(wú)痕12
時(shí)間:
2019-4-17 23:09
請(qǐng)問(wèn)這個(gè)按鍵需要外加上拉電阻嗎?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1