电梯单片机源程序如下:
- #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)
|