#include
#define uint unsigned int
#define uchar unsigned char
sbit rl=P3^0;//正回转按键
sbit add=P3^1;//增速按键
sbit sub=P3^2;//减速按键
sbit stop=P3^3;//电机中止按键
sbit pwmh=P1^0;//PWMH输出
sbit pwml=P1^1;//1正/0回转
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;//设定电机初始转速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;}
}
//=======================================