直流電機(jī)正反轉(zhuǎn)及調(diào)速控制
#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;//電機(jī)停止按鍵
sbit pwmh=P1^0;//PWMH輸出
sbit pwml=P1^1;//1正/0反轉(zhuǎn)
uchar num1,num2,flag1,flag2,flag3;
uint aa,bb,n;
void delay(uint z);
void monitor();
void intc();
void keyscan();
//======================
void main()
{
intc();
while(1)
{
keyscan();
if(flag3==0)
{if(flag1==0)
pwml=1;
else
pwml=0;}
if(flag3==1)
{pwmh=1;pwml=1;}
}
}
//===================
void delay(uint z)
{
uint x;
uchar y;
for(x=0;x
}
//=========================
void intc()
{n=50;//設(shè)定電機(jī)初始轉(zhuǎn)速10MS方波;
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
TR0=1;
}
//========================
void keyscan()
{
if(rl==0)
delay(10);
if(rl==0)
{
while(!rl);
flag1++;
if(flag1>1)
flag1=0;
}
if(add==0)
delay(10);
if(add==0)
{
while(!add);
n++;
if(n>100)
n=100;
}
if(sub==0)
delay(10);
if(sub==0)
{
while(!sub);
if(n<2)
n=1;
else
n--;
}
if(stop==0)
delay(10);
if(stop==0)
{
while(!stop);
flag3=1;
pwmh=1;
pwml=1;
}
}
//==========================
//==========第二種改變PWM波的方法======
void timer0() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
num1++;
if(num1
else
pwmh=0;
if(num1>100)
{num1=0;flag2=1;}
}
//=======================================
評(píng)論