目录
1、独立按键的基本应用
代码:
while用法:
1、按下灯亮,松开熄灭
2、按下灯亮,再次按下熄灭
2、独立按键的扩展应用
1、独立按键的基本应用
(独立按键:J5跳线帽接到2、3脚)
J5跳线帽接到2、3引脚,使按键S4~S7四个按键的另外一段接地从而成为4个独立键盘。在扫描按键的过程中,发现有按键触发信号后,先做消抖处理,当确认按键按下时,才进行相应的功能处理。
按下S7、S6、S5、S4分别点亮L1、L2、L3、L4指示灯,松开按键熄灭,消抖处理。
代码:
#include "reg52.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
sbit HC138_A = P2^5;
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;
void DelayKey(unsigned char t)
{
while(t--);
}
void InitHC138(unsigned char n)
{
switch (n)
{
case 4://8路LED
HC138_C = 1;
HC138_B = 0;
HC138_A = 0;
break;
case 5://蜂鸣器与继电器
HC138_C = 1;
HC138_B = 0;
HC138_A = 1;
break;
case 6://数码管位置
HC138_C = 1;
HC138_B = 1;
HC138_A = 0;
break;
case 7://数码管段选
HC138_C = 1;
HC138_B = 1;
HC138_A = 1;
break;
}
}
void ScanKeys_Alone()
{
InitHC138(4);
P0 = 0xff;
if (S7 == 0)
{
DelayKey(100);
if(S7 == 0)
{
L1 = 0;
while(!S7);//条件成立,为真1,程序停留在while(1)处
L1 = 1; //条件不成立,为假0,while(0)不执行
}
}
if (S6 == 0)
{
DelayKey(100);
if(S6 == 0)
{
L2 = 0;
while(!S6);
L2 = 1;
}
}
if (S5 == 0)
{
DelayKey(100);
if(S5 == 0)
{
L3 = 0;
while(!S5);
L3 = 1;
}
}
if (S4 == 0)
{
DelayKey(100);
if(S4 == 0)
{
L4 = 0;
while(!S4);
L4 = 1;
}
}
}
void main()
{
while(1)
{
ScanKeys_Alone();
}
}
while用法:
1、按下灯亮,松开熄灭
if (S7 == 0)
{
DelayKey(100);
if(S7 == 0)
{
L1 = 0;
while(!S7);//条件成立,为真1,程序停留在while(1)处
L1 = 1; //条件不成立,为假0,while(0)不执行
}
}
if (S5 == 0)
{
DelayKey(100);
if (S5 == 0)
{
if (flat == 1)
{
L3 = 0;
while(!S5);
L3 = 1;
}
else if (flat == 2)
{
L5 = 0;
while(!S5);
L5 = 1;
}
}
}
2、按下灯亮,再次按下熄灭
if (S7 == 0)
{
DelayKey(100);
if (S7 == 0)
{
if (flat == 0)
{
L1 = 0; //S7按下,L1亮
flat = 1; //L1亮的状态
}
else if (flat == 1)
{
L1 = 1; //L1灭
flat = 0;
}
while(!S7);
}
while(条件);
①条件成立,为真(1),一直停留在while(1)处执行循环;
(S7=0按键按下。!S7=1,while(1)死循环一直执行,不执行L1=1,灯亮)
②条件不成立,为假(0),跳出循环,不执行while(0)。
(S7=1按键没有按下。!S7=0,while(0)循环不执行跳出循环,执行L1=1,灯灭)
2、独立按键的扩展应用
S7、S6为选择键,确定控制键控制哪组LED指示灯;S5、S4为控制键,按下点亮指定的LED指示灯,松开熄灭。
①按下S7点亮L1,L1点亮后,S6不响应操作,S5控制L3,S4控制L4;
再次按下S7,L1熄灭,S6可响应操作。
②按下S6点亮L2,L2点亮后,S7不响应操作,S5控制L5,S4控制L6;
再次按下S6,L2熄灭,S7可响应操作。
③S7、S6未按下时,即L1、L2未点亮时,S5、S4不响应操作(未作LED区域选择时,控制键不能操作)
#include "reg52.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit HC138_A = P2^5;
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
unsigned char flat = 0;
void SelectHC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void InitHC138(unsigned char n)
{
switch (n)
{
case 4://8路LED
HC138_C = 1;
HC138_B = 0;
HC138_A = 0;
break;
case 5://蜂鸣器与继电器
HC138_C = 1;
HC138_B = 0;
HC138_A = 1;
break;
case 6://数码管位置
HC138_C = 1;
HC138_B = 1;
HC138_A = 0;
break;
case 7://数码管段选
HC138_C = 1;
HC138_B = 1;
HC138_A = 1;
break;
}
}
void DelayKey(unsigned char t)
{
while(t--);
}
void InitSystem()
{
InitHC138(5);
P0 = 0x00;
}
void ScanKeys_()
{
//按下S7点亮L1;再次按下S7,L1熄灭
if (S7 == 0)
{
DelayKey(100);
if (S7 == 0)
{
if (flat == 0)
{
L1 = 0; //S7按下,L1亮
flat = 1; //L1亮的状态
}
else if (flat == 1)
{
L1 = 1; //L1灭
flat = 0;
}
while(!S7);
}
}
//按下S6点亮L2;再次按下S6,L2熄灭
if (S6 == 0)
{
DelayKey(100);
if (S6 == 0)
{
if (flat == 0)
{
L2 = 0; //S6按下,L2亮
flat = 2; //L2亮的状态
}
else if (flat == 2)
{
L2 = 1; //L2灭
flat = 0;
}
while(!S6);
}
}
//S5,控制L3(S7,flat=1)、L5(S6,flat=2)
if (S5 == 0)
{
DelayKey(100);
if (S5 == 0)
{
if (flat == 1) //按下S7,S5控制L3
{
L3 = 0;
while(!S5);
L3 = 1;
}
else if (flat == 2)//按下S6,S5控制L5
{
L5 = 0;
while(!S5);
L5 = 1;
}
}
}
//S4,控制L4(S7,flat=1)、L6(S6,flat=2)
if (S4 == 0)
{
DelayKey(100);
if (S4 == 0)
{
if (flat == 1) //按下S7,S5控制L3
{
L4 = 0;
while(!S5);
L4 = 1;
}
else if (flat == 2)//按下S6,S5控制L5
{
L6 = 0;
while(!S5);
L6 = 1;
}
}
}
}
void main()
{
// InitSystem();
SelectHC573(4);
// InitHC138(4);
while(1)
{
ScanKeys_();
}
}
|