仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- define MAXFLOOR 6
- unsigned char code LED_CODES[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d};
- sbit F6D=P1^0;
- sbit F1U=P1^1;
- sbit F2D=P1^2;
- sbit F2U=P1^3;
- sbit F3D=P1^4;
- sbit F3U=P1^5;
- sbit F4D=P1^6;
- sbit F4U=P1^7;
- sbit F5D=P3^0;
- sbit F5U=P3^1;
- sbit F1=P2^0;
- sbit F2=P2^1;
- sbit F3=P2^2;
- sbit F4=P2^3;
- sbit F5=P2^4;
- sbit F6=P2^5;
- sbit ledu=P3^7;
- sbit ledd=P3^6;
- sbit ledx=P2^3;
- sbit a=P3^5;
- sbit a1=P2^4;
- sbit a2=P2^5;
- sbit a3=P2^6;
- sbit a4=P2^7;
- bit dir=1,stop=0;
- unsigned char nf=1;
- unsigned char cf=1;
- unsigned char df;
- unsigned char tf;
- unsigned char flag,count=0,i=0;
- unsigned int timer1=0,timer2=0;
- unsigned char call_floor[7]={0,0,0,0,0};
- unsigned char const sequencea[8]={0x2F,0x6F,0x4F,0xcF,0x8F,0x9F,0x1F,0x3F};
- void select_next();
- void step(bit dir);
- void delay(unsigned int z);
- void main(void)
- {P0=LED_CODES[1];
- TH0=0x3C;
- TL0=0xB0;
- TMOD=0x01;
- ET0=1;
- EA=1;
- EX0=1;
- IT0=1;
- a=0;
- while(1)
- { if(!flag&&!stop)
- {select_next();
- step(dir);
- a=1;
- delay(5000);
- a=0;
- }
- else if(stop)
- {timer2=0;
- TR0=1;
- while(timer2<100&&stop);
- TR0=0;
- timer2=0;
- stop=0;
-
- }
- }
- }
- void select_next()
- {char i;
-
- if(nf==MAXFLOOR)
- {
- dir=0;
- }
- else if(nf==1)
- {
- dir=1;
- }
- if(dir==0)
- {
- if(call_floor[nf]==1)
- {call_floor[nf]=0;
- stop=1;
- return;
- }
- for(i=nf-1;i>=1;i--)
- if(call_floor[i])
- {cf=i;return;}
- dir=1;
- for(i=nf+1;i<=MAXFLOOR;i++)
- if(call_floor[i])
- {cf=i;return;}
- dir=0;
- cf=1;
- }
- if(call_floor[nf]==1)
- {
- call_floor[nf]=0;
- stop=1;
- return;
- }
- for(i=nf+1;i<=MAXFLOOR;i++)
- if(call_floor[i])
- {cf=i;return;}
- if(i==7)
- {dir=0;
- }
- }
- void step(bit dir)
- {
- if(cf==nf)
- return;
- else if(!flag)
- {flag=1;
- delay(50);
- if(dir==1)
- {ledu=0;
- ledx=0;
- ledd=1;
- {i=i<8?i+1:0;
- switch(i)
- {
- case 0: a1=0;a2=1;a3=0;a4=0;break;
- case 1: a1=0;a2=1;a3=1;a4=0;break;
- case 2: a1=0;a2=1;a3=1;a4=0;break;
- case 3: a1=0;a2=0;a3=1;a4=1;break;
- case 4: a1=0;a2=0;a3=0;a4=1;break;
- case 5: a1=1;a2=0;a3=0;a4=1;break;
- case 6: a1=1;a2=1;a3=0;a4=0;break;
- case 7: a1=1;a2=1;a3=0;a4=0;break;}
- }
- }
- else
- {ledd=0;
- ledx=0;
- ledu=1;
- {i=i>0?i-1:7;
- switch(i)
- {
- case 0:a1=1;a2=1;a3=0;a4=0; break;
- case 1:a1=1;a2=0;a3=1;a4=0; break;
- case 2:a1=1;a2=0;a3=0;a4=1; break;
- case 3:a1=0;a2=0;a3=0;a4=1; break;
- case 4:a1=0;a2=0;a3=1;a4=1; break;
- case 5:a1=0;a2=0;a3=1;a4=0; break;
- case 6:a1=0;a2=1;a3=1;a4=0; break;
- case 7:a1=0;a2=1;a3=0;a4=0;break;}
- }
- }
- timer1=0;
- TR0=1;
- }
- }
- void delay(unsigned int z)
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- {
- for(y=125;y>0;y--)
- ;
- }
- }
- void time0_int() interrupt 1
- {
- TH0=0x3C;
- TL0=0xB0;
- timer1++;
- timer2++;
- if(flag)
- {
- if(timer1==20)
- {
- timer1=0;
- if(dir)
- nf++;
- else
- nf--;
- call_floor[nf]=0;
- flag=0;
- TR0=0;
- P0=LED_CODES[nf];
- if(cf==nf)
- {TR0=0;
- ledx=ledu=ledd=1;
- stop=1;
- return;
- }
- }
- }
- }
- void int0() interrupt 0
- {
- if(F6D==0)
- call_floor[6]=1;
- else if(F1U==0)
- call_floor[1]=1;
- else if(F2D==0||F2U==0)
- call_floor[2]=1;
- else if(F3D==0||F3U==0)
- call_floor[3]=1;
- else if(F4D==0||F4U==0)
- call_floor[4]=1;
- else if(F5D==0||F5U==0)
- call_floor[5]=1;
- else if(F6==0)
- {call_floor[6]=1;stop=0;}
- else if(F1==0)
- {call_floor[1]=1;stop=0;}
- else if(F2==0)
- {call_floor[2]=1;stop=0;}
- else if(F3==0)
- {call_floor[3]=1;stop=0;}
- else if(F4==0)
- {call_floor[4]=1;stop=0;}
- else if(F5==0)
- {call_floor[5]=1;stop=0;}
- }
复制代码
全部资料51hei下载地址:
二层电梯.rar
(190.76 KB, 下载次数: 20)
|