sbit m0=P3^0;
sbit m1=P3^1;
sbit m2=P3^3;
sbit m3=P3^4;
sbit m4=P3^2;
sbit m5=P3^5;
sbit n0=P1^0;
sbit n1=P1^1;
sbit n2=P1^2;
sbit n3=P1^3;
sbit PWM=P1^4;
unsigned char tp=0;
unsigned int count=0;
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf}; //段码0~f
char data xxx[4]={0x80,0x40,0x20,0x10}; //位码
void led(unsigned int);
void delay(unsigned int);
void main() //主程序
{while(1)
{
EA=1; //开总中断
EX0=1; //开外部中断0
TMOD=0x02;
TH0=0x06;
TL0=0x06;
ET0=1;
TR0=1;
if(m3==0) //停止
{n0=n1=0;
led(3);
}
else if(m0==0) //正转
{n0=1;
n1=0;
led(0);
}
else if(m1==0) // 反转
{n0=0;
n1=1;
led(1);
}
else if(m2==0) //加速
{delay(100);
if(m2==0)
{n0=1;
n1=0;
if(tp>99)
tp=100;
else
tp=tp+10;
}}
else if(m5==0) //减速
{delay(100);
if(m5==0)
{
if(tp<1)
tp=0;
else tp=tp-10;
}
}
P0=0xff;
}
}
void led(unsigned int j) //数码管显示程序
{
if(j==0)
{
P2=xxx[0];
P0=tab[0];
delay(1);
P2=xxx[1];
P0=tab[6];
delay(1);
P2=xxx[2];
P0=tab[4];
delay(1);
P2=xxx[3];
P0=tab[0];
delay(1);
}
else if(j==1)
{P2=xxx[0];
P0=tab[0];
delay(1);
P2=xxx[1];
P0=tab[6];
delay(1);
P2=xxx[2];
P0=tab[4];
delay(1);
P2=xxx[3];
P0=tab[16];
delay(1);
}
else if(j==2)
{P2=xxx[0];
P0=tab[1];
delay(1);
P2=xxx[1];
P0=tab[0];
delay(1);
P2=xxx[2];
P0=tab[0];
delay(1);
P2=xxx[3];
P0=tab[1];
delay(1);
}
else if(j==3)
{
P2=xxx[0];
P0=tab[0];
delay(1);
P2=xxx[1];
P0=tab[0];
delay(1);
P2=xxx[2];
P0=tab[0];
delay(1);
P2=xxx[3];
P0=tab[0];
delay(1);
}
else if(j==4)
{
P2=xxx[0];
P0=tab[1];
delay(1);
P2=xxx[1];
P0=tab[1];
delay(1);
P2=xxx[2];
P0=tab[1];
delay(1);
P2=xxx[3];
P0=tab[1];
delay(1);
}
}
void INT0_time() interrupt 0 //外部中断0程序
{
unsigned int h;
for(h=9;h>0;h--)
{P2=xxx[0];
P0=tab[h];
delay(1000);
P0=0x00;
}
P0=0x00;
n0=n1=n2=n3=0;
EX0=0;}
void t0(void) interrupt 1
{
if(count>100)
count=0;
if(count>tp)
PWM=0;
else
PWM=1;
count++;
}
void delay(unsigned int k) //延时程序
{
unsigned int i,y;
for(i=k;i>0;i--)
{for(y=100;y>0;y--);
}
}