单片机源程序:
#include <reg52.h>
#define u8 unsigned char
unsigned char seg_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
unsigned char run,floor;
bit ud,stop=0;
sbit P06=P0^6;
sbit P07=P0^7;
sbit ulc=P2^0;
sbit dlc=P2^1;
sbit start=P2^2;
sbit alrl=P2^3;
sbit alrb=P2^4;
void input()
{
run|=((~P0)&0x3f);
if((P06==0)&&(ud==1))
run|=0x02;
if((P06==0)&&(ud==0))
run|=0x04;
if((P07==0)&&(ud==1))
run|=0x08;
if((P07==0)&&(ud==0))
run|=0x10;
}
void delay(int j)
{
int i;
for(;j>0;j--)
{
input();
for(i=200;i>0;i--)
{
if(stop==1)
break;
}
if(stop==1)
break;
}
}
void StepMotor(bit a,u8 b)
{
u8 x=0x80,i;
if(a==1)
{
ulc=0;
dlc=1;
for(i=0;i<b;i++)
{
if(x==0x10)
x=0x80;
else
x=x/2;
P3|=(x&0xf0);
delay(100);
}
}
else
{
ulc=1;
dlc=0;
for(i=0;i<b;i++)
{
if(x==0x80)
x=0x10;
else
x=x*2;
P3|=(x&0xf0);
delay(100);
}
}
}
void alarm()
{
alrl=0;
while(1)
{
alrb = ~alrb;
delay(200);
}
}
void main()
{
floor=1;
P1=seg_table[floor];
P0=0xff;
ud=1;
run=0;
ulc=1;
dlc=1;
alrl=1;
alrb=1;
TCON=0x01; //打开外部中断0,并设置为下降沿触发
IE=0x81; //开总中断
while(1)
{
while(start==1)
{
P1=seg_table[10];
ulc=1;
dlc=1;
}
input();
switch(floor)
{
case 1: //1
{
P1=seg_table[floor];
if((run&0x01)==0x01)
{
delay(600);
run&=0xfe;
}
if((run&0x3e)!=0)
{
StepMotor(1,5);
floor=2;
ud=1;
}
else
{
ulc=1;
dlc=1;
}
} break;
case 2:
{
P1=seg_table[floor];
if(ud==1) //2上
{
if((run&0x02)==0x02)
{
delay(600);
run&=0xfd;
}
if((run&0x38)!=0)
{
StepMotor(1,5);
floor=3;
}
else
{
ud=0;
ulc=1;
dlc=1;
}
}
else //2下
{
if((run&0x04)==0x04)
{
delay(600);
run&=0xfb;
}
if((run&0x01)!=0)
{
StepMotor(0,5);
floor=1;
}
else
{
ud=1;
ulc=1;
dlc=1;
}
}
} break;
case 3:
{
P1=seg_table[floor];
if(ud==1) //3上
{
if((run&0x08)==0x08)
{
delay(600);
run&=0xf7;
}
if((run&0x20)!=0)
{
StepMotor(1,5);
floor=4;
}
else
{
ud=0;
ulc=1;
dlc=1;
}
}
else //3下
{
if((run&0x10)==0x10)
{
delay(600);
run&=0xef;
}
if((run&0x07)!=0)
{
StepMotor(0,5);
floor=2;
}
else
{
ud=1;
ulc=1;
dlc=1;
}
}
} break;
case 4:
{
P1=seg_table[floor];
if((run&0x20)==0x20)
{
delay(600);
run&=0xdf;
}
if((run&0x1f)!=0)
{
StepMotor(0,5);
floor=3;
ud=0;
}
else
{
ulc=1;
dlc=1;
}
}
}
if(stop==1)
{
P1=seg_table[10];
ulc=1;
dlc=1;
alarm();
}
}
}
void int0() interrupt 0
{
stop=1;
}
|