電梯單片機源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code Table_of_Digits[]=
- {0x00,0x3e,0x41,0x41,0x41,0x3e,0x00,0x00, //0
- 0x00,0x00,0x00,0x21,0x7f,0x01,0x00,0x00, //1
- 0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00, //2
- 0x00,0x22,0x49,0x49,0x49,0x36,0x00,0x00, //3
- 0x00,0x0c,0x14,0x24,0x7f,0x04,0x00,0x00, //4
- 0x00,0x72,0x51,0x51,0x51,0x4e,0x00,0x00, //5
- 0x00,0x3e,0x49,0x49,0x49,0x26,0x00,0x00, //6
- 0x00,0x40,0x40,0x40,0x4f,0x70,0x00,0x00, //7
- 0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00, //8
- };
- uchar i=0,t=0,j=0,a=0; //i掃t延
- char offset=0;
- static int Current_floor=0,Pressed_key_up=0,Pressed_key_down=0,b=0,c=0,max=0,min=0; //Current_floor當前樓層Pressed_key_up(down)上(下)升要求
- int flag;
- int floor_up[8]={9,9,9,9,9,9,9,9};
- int floor_down[8]={0,0,0,0,0,0,0,0}; //上下降請求存放數組
- sbit KEY1=P1^0; //定義P1口按鍵
- sbit KEY2=P1^1;
- sbit KEY3=P1^2;
- sbit KEY4=P1^3;
- sbit KEY5=P1^4;
- sbit KEY6=P1^5;
- sbit KEY7=P1^6;
- sbit KEY8=P1^7;
- sbit LED=P3^7; //定義LED跟蜂鳴器
- sbit SPEAK=P3^6;
- void Key_up_comparison(); //把floor_up[]按從小到大排列
- void Key_down_comparison(); //把floor_down[]按從大到小排列
- void max_min_comparison(); //比較出floor_up[]的最大值(max)和floor_down[]的最小值(min)
- //主函數
- void main()
- {
- P2=0x80; //初始化
- LED=0;
- SPEAK=0;
- TMOD=0x01; //T0方式
- TH0=(-6000)/256; //6ms定時
- TL0=(-6000)%256;
- IT0=1;
- IE=0x83;
- TR0=1;
- ET0=1;
- Pressed_key_up=floor_up[0];
- while(1) //循環排列
- {
- Key_up_comparison();
- Key_down_comparison();
- max_min_comparison();
- }
- }
- void LED_Screen_Display() interrupt 1 //進入中斷,顯示樓層
- {
- TH0=(-6000)/256; //6ms
- TL0=(-6000)%256;
- P0=0xff;
- Pressed_key_up=floor_up[0];
- Pressed_key_down=floor_down[0];
-
- if(flag==1) //上升標志
- {
- if(Pressed_key_up!=9) //有上升請求
- {
- if(Pressed_key_up>Current_floor) //點陣平滑上升顯示
- {
- P0=~Table_of_Digits[Current_floor*8+i];
- P2=_crol_(P2,1);
- if(++i==8)
- { i=0; P2=0x80; }
- if(++t==250) //延遲
- { t=0;
-
- ++Current_floor;}
- }
- }
- else if(Pressed_key_up<Current_floor);
-
- else if(Pressed_key_up==Current_floor) //到達請求樓層
- {
- P0=~Table_of_Digits[Current_floor*8+i];
- P2=_crol_(P2,1);
- if(++i==8)
- {i=0; P2=0x80; }
- LED=~LED;
- SPEAK=~SPEAK; //蜂鳴器響,LED閃爍
- if(++t==250)
- {
- t=0;
- if(Current_floor==Pressed_key_up)
- { Current_floor=Pressed_key_up;
- if(Current_floor==max)
- { flag=0; }
- floor_up[0]=9;
- }
- }
- }
- }
- else if(Pressed_key_up==9) //無請求,掃描顯示當前樓層
- {
- P0=~Table_of_Digits[Current_floor*8+i];
- P2=_crol_(P2,1);
- if(++i==8)
- {
- i=0;
- P2=0x80;
- }
- if(++t==250)
- t=0;
- }
- }
- else if(flag==0) //下降標志
- {
- if(Pressed_key_down!=0) //有下降請求
- {
- if(Pressed_key_down>Current_floor);
- else if(Pressed_key_down<Current_floor)
- //點陣平滑下降顯示
- {
- P0=~Table_of_Digits[Current_floor*8+i];
- P2=_crol_(P2,1);
- if(++i==8)
- {
- i=0;
- P2=0x80;
- }
- if(++t==250)
- {
- t=0;
- if (++offset==8)
- {
- offset = 0;
- --Current_floor;
- }
- }
- }
- else if(Pressed_key_down==Current_floor) //到達請求樓層
- {
- P0=~Table_of_Digits[Current_floor*8+i];
- P2=_crol_(P2,1);
- if(++i==8)
- {
- i=0;
- P2=0x80;
- }
- LED=~LED;SPEAK=~SPEAK; //蜂鳴器響,LED閃爍
- if(++t==250)
- {t=0;
- if(Current_floor==Pressed_key_down)
- {
- Current_floor=Pressed_key_down;
- if(Current_floor==min)
- {flag=1;}
- floor_down[0]=0;
- }
- }
- }
- }
- else if(Pressed_key_down==0) //無請求,掃描顯示當前樓層
- {
- P0=~Table_of_Digits[Current_floor*8+i];
- P2=_crol_(P2,1);
- if(++i==8)
- {
- i=0;
- P2=0x80;
- }
- if(++t==250)
- t=0;
- }
- }
-
- void Key_detection() interrupt 0 //按下按鍵,進入外部中斷,將上升請求跟下降請求放入數組,并判斷標志flag
- {
- P0=0xff;
- P2=0x80;
- i=0; //防抖
- if(KEY1==0) j=1;
- else if(KEY2==0) j=2; //判斷請求
- else if(KEY3==0) j=3;
- else if(KEY4==0) j=4;
- else if(KEY5==0) j=5;
- else if(KEY6==0) j=6;
- else if(KEY7==0) j=7;
- else if(KEY8==0) j=8;
- if(j>Current_floor) //將上升請求放入數組
- {
- if(floor_down[0]==0)
- flag=1;
- floor_up[b++]=j;
- if(b==8)
- b=0;
- }
- else if(j<Current_floor) //將下降請求放入數組
- {
- if(floor_up[0]==9)
- flag=0;
- floor_down[c++]=j;
- if(c==8)
- c=0;
- }
- else if(j==Current_floor);
- }
- void Key_up_comparison() //把floor_up[]按從小到大排列
- {
- int a1,a2,a3=0;
- for(a2=0;a2<7;a2++)
- {
- for(a1=0;a1<7;++a1)
- {
- if(floor_up[a1]>floor_up[a1+1])
- {
- a3=floor_up[a1];
- floor_up[a1]=floor_up[a1+1];
- floor_up[a1+1]=a3;
- }
- if(floor_up[a1]==floor_up[a1+1])
- floor_up[a1+1]=9;
- }
- }
- }
- void Key_down_comparison() //把floor_down[]按從大到小排列
- {
- int a1,a2,a3=0;
- for(a2=0;a2<7;a2++)
- {
- for(a1=0;a1<7;++a1)
- {
- if(floor_down[a1]<floor_down[a1+1])
- {
- a3=floor_down[a1];
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
電梯.zip
(26.37 KB, 下載次數: 5)
2017-6-17 00:33 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|