#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit PWM11 = P1^0; //motora
sbit PWM12 = P1^1;
sbit PWM21 = P1^2; //motorb
sbit PWM22 = P1^3;
sbit MC1 = P1^4; //motora enable
sbit MC2 = P1^5; //motorb enable
sbit LED = P1^6; //æ示ç¯
sbit FMQ = P1^7; //è鸣å¨
sbit key_qt = P0^0; //motor2 å¼è
sbit key_sph = P0^1; //åéé®
sbit key_spl = P0^2; //ééé®
sbit limita1 = P0^3; //çµæºæ£aéä½
sbit limita2 = P0^4; //çµæºè´aéä½
sbit limitb1 = P0^5; //çµæºæ£béä½
sbit limitb2 = P0^6; //çµæºè´béä½
uchar keyval,cnt=0,cnt1=0,pwm=50,num;
uchar m_mode=0; //çµæºè¿è¡æ¨¡å¼ï¼1-çµæºæ£åï¼2-çµæºbæ£ååï¼3=çµæºbååï¼4-çµæºaååï¼
bit flag=0; //延æ¶æ¶é´å°æ å¿
uchar runflag=0; //çµæºè¿è¡æ å¿
void delaynms(uint z)
{
uint i,j;
for(i = z;i > 0;i--)
for(j = 110;j > 0;j--);
}
void keychk(void)
{
keyval=0xff;
if(key_qt==0)
{
delaynms(5); //æ¶æ
if(key_qt==0) //ç¡®å®æé®æä¸
{
while(key_qt==0);
keyval=1;
}
}
if(!key_sph)
{
delaynms(5); //æ¶æ
if(!key_sph) //ç¡®å®æé®æä¸
{
while(!key_sph);
keyval=2;
}
}
if(!key_spl)
{
delaynms(5); //æ¶æ
if(!key_spl) //ç¡®å®æé®æä¸
{
while(!key_spl);
keyval=3;
}
}
}
void t0isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(runflag==1) //æ£å¸¸è¿è¡
{
cnt1++;
if(cnt1>=10){cnt1=0;LED=~LED;}
}
if(runflag==2) //éä½å»¶æ¶
{
LED=1;
cnt++;
if(cnt>=10)
{
flag=1;
cnt=0;
}
}
}
void t1isr() interrupt 3
{
num++;
if(num<pwm)
{
switch(m_mode)
{
case 1:PWM11=1;PWM12=0;break; //açµæºæ£è½¬
case 2:PWM21=1;PWM22=0;break; //bçµæºæ£è½¬
case 3:PWM21=0;PWM22=1;break; //bçµæºå转
case 4:PWM11=0;PWM12=1;break; //açµæºå转
default:break;
}
}
else if(num>=pwm)
{
PWM11=0;PWM12=0;PWM21=0;PWM22=0;
}
if(num>=100)num=0;
}
void main(void)
{
bit qt=0;
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=236; //æ¹å该å¼è°æ´PWMé¢ç
TL1=236;
TR0=1;
TR1=1;
ET1=1;
ET0=1;
EA=1;
P1|=0xc0;
flag=0;
while(1)
{
keychk();
switch(keyval)
{
case 1: qt=~qt;
if(qt)
{
runflag=1;
switch(m_mode)
{
case 0: m_mode=1; //åå¼å§å¯å¨ï¼ç½®æ¨¡å¼1
MC1=1;
FMQ=0; //è鸣å¨åä¸ä¸
delaynms(100);
FMQ=1;
break;
case 1:MC1=1;break; //çµæºaæ£è½¬
case 2:MC2=1;break; //çµæºbæ£è½¬
case 3:MC2=1;break; //çµæºbå转
case 4:MC1=1;break; //çµæºaå转
default:m_mode=0;break;
}
}
if(!qt)
{
MC1=0;
MC2=0;
LED=1;
runflag=0;
break;
}
break;
case 2:pwm+=5;if(pwm>100)pwm=100;break; //åé5个åä½
case 3:pwm-=5;if(pwm<10)pwm=10;break; //éé5个åä½
default:break;
}
if(limita1==0 && m_mode==1) //çµæºaæ£åéä½
{
delaynms(1);
if(limita1==0)
{
MC1=0;
runflag=2;
flag=0;
while(flag==0); //çå¾
延æ¶0.5Sç»æ
m_mode=2;
runflag=1;
MC2=1;
}
}
if(limitb1==0 && m_mode==2) //çµæºbæ£åéä½
{
delaynms(1);
if(limitb1==0)
{
MC2=0;
runflag=2;
flag=0;
while(flag==0);
qt=0;
m_mode=3;
}
}
if(limitb2==0 && m_mode==3) //çµæºbååéä½
{
delaynms(1);
if(limitb2==0)
{
MC2=0;
runflag=2;
flag=0;
while(flag==0);
m_mode=4;
runflag=1;
MC1=1;
}
}
if(limita2==0 && m_mode==4) //çµæºaååéä½
{
delaynms(1);
if(limita2==0)
{
MC1=0;
runflag=2;
flag=0;
while(flag==0);
flag=0;
m_mode=0;
runflag=0;
qt=0;
LED=1;
}
}
}
}
温馨提示:内容为网友见解,仅供参考