0
本帖最后由 觉jiào醒 于 2017-6-24 12:42 编辑
模拟电梯:电梯外选择上楼或下楼键,进入电梯选择楼层,隔段时间,8X8点阵显示当前楼层
废话少说
代码:
- #include
- #include
- #define uint unsigned int
- #define uchar unsigned char
- //--定义要使用的IO口--//
- #define COMMONPORTS P0
- ***it SRCLK=P3^6;
- ***it RCLK=P3^5;
- ***it SER=P3^4;
- ***it Floor_1_UP=P3^1;
- ***it Floor_2_DOWN=P3^0;
- ***it Floor_2_UP=P3^2;
- ***it Floor_3_DOWN=P3^3;
- //定义标志位 0-有效 1-无效
- uchar F1_UP_flag=1;
- uchar F2_DOWN_flag=1;
- uchar F2_UP_flag=1;
- uchar F3_DOWN_flag=1;
- uchar F1_flag=1;
- uchar F2_flag=1;
- uchar F3_flag=1;
- ***it KEY=P1^7; //矩阵键盘第一行,置0
- ***it Floor_3=P1^1; //楼层三、二、一层
- ***it Floor_2=P1^2;
- ***it Floor_1=P1^3;
- //--列选通控制--//
- unsigned char code TAB[8] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- //--点阵字码--//
- unsigned char code CHARCODE[4][8]=
- {
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, //空
-
- {0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00}, //1
- {0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00}, //2
- {0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00}, //3
- };
- void delay(unsigned int time)
- {
- uint i,j;
- for(i=0;i
- for(j=0;j<121;j++);
- }
- void Hc595SendByte(unsigned char dat)
- {
- uchar a;
- SRCLK=0;
- RCLK=0;
- for(a=0;a<8;a++)
- {
- SER=dat>>7;
- dat<<=1;
- SRCLK=1;
- _nop_();
- _nop_();
- SRCLK=0;
- }
- RCLK=1;
- _nop_();
- _nop_();
- RCLK=0;
- }
- void Display(unsigned char floor)
- {
- uchar tab;
- for(tab=0;tab<8;tab++)
- {
- Hc595SendByte(0x00); //消隐
- COMMONPORTS = TAB[tab]; //输出字码
- Hc595SendByte(CHARCODE[floor][tab]);
- delay(2);
- }
- }
- void Key_Scan()
- {
- static uchar SaveF1_UP=1; //保存一层上楼键状态 0-按下,1-弹起
- static uchar SaveF2_DOWN=1; //保存二层下楼键状态 0-按下,1-弹起
- static uchar SaveF2_UP=1; //保存二层上楼键状态 0-按下,1-弹起
- static uchar SaveF3_DOWN=1; //保存三层下楼键状态 0-按下,1-弹起
-
- static uchar SaveF1=1; //保存一楼键状态 0-按下,1-弹起
- static uchar SaveF2=1; //保存二楼键状态 0-按下,1-弹起
- static uchar SaveF3=1; //保存三楼键状态 0-按下,1-弹起
-
- uchar F1_UP=1; //当前一层上楼按键状态 弹起
- uchar F2_DOWN=1; //当前二层下楼按键状态 弹起
- uchar F2_UP=1; //当前二层上楼按键状态 弹起
- uchar F3_DOWN=1; //当前三层下楼按键状态 弹起
-
- uchar F1=1; //当前一楼按键状态 弹起
- uchar F2=1; //当前二楼按键状态 弹起
- uchar F3=1; //当前三楼按键状态 弹起
-
- //获取各按键状态
- //电梯外
- if(Floor_1_UP==0) //一层上楼键按下
- {
- delay(1);
- if(Floor_1_UP==0)
- {
- F1_UP=0;
- }
- else
- {
- F1_UP=1;
- }
- }
- else
- {
- F1_UP=1;
- }
-
- if(Floor_2_DOWN==0) //二层下楼键按下
- {
- delay(1);
- if(Floor_2_DOWN==0)
- {
- F2_DOWN=0;
- }
- else
- {
- F2_DOWN=1;
- }
- }
- else
- {
- F2_DOWN=1;
- }
-
- if(Floor_2_UP==0) //二层上楼键按下
- {
- delay(1);
- if(Floor_2_UP==0)
- {
- F2_UP=0;
- }
- else
- {
- F2_UP=1;
- }
- }
- else
- {
- F2_UP=1;
- }
-
- if(Floor_3_DOWN==0) //三层下楼键按下
- {
- delay(1);
- if(Floor_3_DOWN==0)
- {
- F3_DOWN=0;
- }
- else
- {
- F3_DOWN=1;
- }
- }
- else
- {
- F3_DOWN=1;
- }
-
- //电梯内
- if(Floor_1==0) //一楼键按下
- {
- delay(1);
- if(Floor_1==0)
- {
- F1=0;
- }
- else
- {
- F1=1;
- }
- }
- else
- {
- F1=1;
- }
- if(Floor_2==0) //二楼键按下
- {
- delay(1);
- if(Floor_2==0)
- {
- F2=0;
- }
- else
- {
- F2=1;
- }
- }
- else
- {
- F2=1;
- }
- if(Floor_3==0) //三楼键按下
- {
- delay(1);
- if(Floor_3==0)
- {
- F3=0;
- }
- else
- {
- F3=1;
- }
- }
- else
- {
- F3=1;
- }
-
- //按键按下状态处理
- //电梯外
- if(SaveF1_UP!=F1_UP) //一层上楼键状态变化
- {
- if(F1_UP==0)
- {
- F1_UP_flag=0;
- }
- }
- if(SaveF2_DOWN!=F2_DOWN) //二层下楼键状态变化
- {
- if(F2_DOWN==0)
- {
- F2_DOWN_flag=0;
- }
- }
- if(SaveF2_UP!=F2_UP) //二层上楼键状态变化
- {
- if(F2_UP==0)
- {
- F2_UP_flag=0;
- }
- }
- if(SaveF3_DOWN!=F3_DOWN) //三层下楼键状态变化
- {
- if(F3_DOWN==0)
- {
- F3_DOWN_flag=0;
- }
- }
-
- //电梯内
- if(SaveF1!=F1) //一楼键状态变化
- {
- if(F1==0)
- {
- F1_flag=0;
- }
- }
- if(SaveF2!=F2) //二楼键状态变化
- {
- if(F2==0)
- {
- F2_flag=0;
- }
- }
- if(SaveF3!=F3) //三楼键状态变化
- {
- if(F3==0)
- {
- F3_flag=0;
- }
- }
-
- SaveF1_UP=F1_UP; //保存一层上楼键状态
- SaveF2_DOWN=F2_DOWN; //保存二层下楼键状态
- SaveF2_UP=F2_UP; //保存二层上楼键状态
- SaveF3_DOWN=F3_DOWN; //保存三层下楼键状态
-
- SaveF1=F1; //保存一楼键状态
- SaveF2=F2; //保存二楼键状态
- SaveF3=F3; //保存三楼键状态
- }
- void Play()
- {
- static uchar floor=1; //显示楼层
- if(floor==1)
- {
- if(F1_UP_flag==0)
- {
- if(F2_flag==0)
- {
- delay(1000);
- floor=2;
- F2_flag=1;
- F1_UP_flag=1;
- }
- else if(F3_flag==0)
- {
- delay(2000);
- floor=3;
- F3_flag=1;
- F1_UP_flag=1;
- }
- }
- }
- else if(floor==2)
- {
- if(F2_DOWN_flag==0)
- {
- if(F1_flag==0)
- {
- delay(1000);
- floor=1;
- F1_flag=1;
- F2_DOWN_flag=1;
- }
- }
- else if(F2_UP_flag==0)
- {
- if(F3_flag==0)
- {
- delay(1000);
- floor=3;
- F3_flag=1;
- F2_UP_flag=1;
- }
- }
- }
- else if(floor==3)
- {
- if(F3_DOWN_flag==0)
- {
- if(F2_flag==0)
- {
- delay(1000);
- floor=2;
- F2_flag=1;
- F3_DOWN_flag=1;
- }
- else if(F1_flag==0)
- {
- delay(2000);
- floor=1;
- F1_flag=1;
- F3_DOWN_flag=1;
- }
- }
- }
- Display(floor);
- }
- void main()
- {
- KEY=0; //键盘行选置0
-
- while(1)
- {
- Key_Scan();
- Play();
- }
- }
复制代码
用开发板做的
开发板原理图压缩包
|
|