正式正反转的程序 :#include <REG52.h>
sbit led_n=P0^0;
sbit led_r=P0^1;
sbit led_s=P0^2;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
unsigned char key;
unsigned char t,s,speed=50;
unsigned int lev;
bit turun,m_on=0;
void delaylev()
{
unsigned int i;
unsigned char j;
for(i=lev;i>0;i--)
for(j=110;j>0;j--);
}
void delayms(unsigned char xms)
{
unsigned char i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void keyscan()
{
unsigned char temp;
if(P3!=0xff) \
delayms(10);
if(P3!=0xff)
{
temp=P3;
switch(temp)
{
case 0xfe:
P0=0xff;
led_n=0;
m_on=1;
turun=0;
break;
case 0xfd:
P0=0xff;
led_r=0;
m_on=1;
turun=1;
break;
case 0xfb:
P0=0xff;
m_on=0;
led_s=0;
break;
case 0xf7:
P0=0xff;
speed=speed-10;
if(speed<10)
speed=2;
break;
case 0xef:
P0=0xff;
speed=speed+10;
if(speed>240)
speed=255;
break;
}
}
while(P3!=0xff);
}
void gorun(unsigned char step)
{
switch(step)
{
case 1: //0
P1_0 = 1;
P1_1 = 1;
P1_2 = 0;
P1_3 = 0;
break;
case 2:
P1_0 = 0;
P1_1 = 1;
P1_2 = 1;
P1_3 = 0;
break;
case 3:
P1_0 = 0;
P1_1 = 0;
P1_2 = 1;
P1_3 = 1;
break;
case 4:
P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
break;
case 5:
P1_0 = 1;
P1_1 = 1;
P1_2 = 0;
P1_3 = 0;
break;
case 6:
P1_0 = 0;
P1_1 = 1;
P1_2 = 1;
P1_3 = 0;
break;
case 7:
P1_0 = 0;
P1_1 = 0;
P1_2 = 1;
P1_3 = 1;
break;
case 8:
P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
break;
}
}
void main()
{
TMOD = 0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(m_on==0)
{
P1=0x00;
}
else
gorun(s);
delaylev();
keyscan();
}
}
void timeint(void) interrupt 1
{
TR0=0;
TH0 = 254;
TL0 = 255-speed;
t++;
if(t==10)
{
if(turun==0)
{
s++;
led_s=~led_s;
if(s>8)
s=1;
}
else
{
s--;
led_s=~led_s;
if(s==0)
s=8;
}
}
P2=speed;
TR0=1;
} |