新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 步進(jìn)電機(jī)正反轉(zhuǎn)可調(diào)速度

步進(jìn)電機(jī)正反轉(zhuǎn)可調(diào)速度

作者: 時(shí)間:2016-11-18 來(lái)源:網(wǎng)絡(luò) 收藏
//步進(jìn)電機(jī)正反轉(zhuǎn)可調(diào)速度

//================================
//P3^0;正反轉(zhuǎn)
//P3^1;增速
//P3^2;減速
//P3^3;停止

#include
#define uint unsigned int
#define uchar unsigned char
sbit rl=P3^0;//正反轉(zhuǎn)
sbit add=P3^1;//增速
sbit sub=P3^2;//減速
sbit stop=P3^3;//停止
uchar num1,num2,flag1,flag2,flag3,n;
uchar code table1[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};//正轉(zhuǎn)表
uchar code table2[]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};//反轉(zhuǎn)表
//電機(jī)轉(zhuǎn)動(dòng)表
void delay(uint z);
void keyscan();
void main()
{ uchar i;
n=50;
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
TR0=1;
while(1)
{
keyscan();
if(!flag3)
{ if(flag1==1)
{flag1=0;
if(flag2==0)
P1=table1[i];
else
{P1=0;
P1=table2[i];}
i++;
if(i>8)
i=0;
}
}
}

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

}
//========================
void delay(uint z)
{
uint x;
uchar y;
for(x=0;xfor(y=0;y<100;y++);
}
//====================
void timer0() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
num1++;
if(num1==n)
{num1=0;flag1=1;}

}

//================
void keyscan()
{
if(rl==0)
delay(10);
if(rl==0)
{
while(!rl);
flag2++;
flag3=0;
if(flag2>1)
flag2=0;
}
if(add==0)
delay(10);
if(add==0)
{
while(!add);
n=n++;
if(n>80)
n=80;
}
if(sub==0)
delay(10);
if(sub==0)
{
while(!sub);
n=n--;
if(n<10)
n=10;
}
if(stop==0)
delay(10);
if(stop==0)
{
while(!stop);
P1=0;
flag3=1;
}
}



評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉