目录
代码实现
1、独立按键的基础操作
(1)按键和灯的定义
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;
(2)HC573选择
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;
}
}
(3)按下灯亮,并提前做抖动处理
抖动处理:检测到按键了,延迟一下,看看一会还能不能检测到按键
void DelayK(unsigned char t)
{
while(t--);
}
void ScanKeys_Alone()
{
if(S7 == 0)
{
DelayK(100); //用来做抖动处理
if(S7 == 0) //确定真的是按下了
{
L1 = 0; //灯亮
while(S7==0); //如果一直按这S7就一直卡在这里循环
L1=1; //循环结束,即不按S7了,灯熄灭
}
}
}
while(S7 == 0);
: 这是一个循环语句,它不断地检测按键 S7 是否还处于按下状态,直到按键松开(即变为逻辑高电平,非 0)为止。这是一个忙等待(busy-wait)的方式,等待按键松开。最后循环执行完,松开之后L1熄灭。
(4)整体代码
#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;
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 DelayK(unsigned char t)
{
while(t--);
}
void ScanKeys_Alone()
{
if(S7 == 0)
{
DelayK(100);
if(S7 == 0)
{
L1 = 0;
while(S7==0);
L1=1;
}
}
if(S6 == 0)
{
DelayK(100);
if(S6 == 0)
{
L2 = 0;
while(S6==0);
L2=1;
}
}
if(S5 == 0)
{
DelayK(100);
if(S5 == 0)
{
L3 = 0;
while(S5==0);
L3=1;
}
}
if(S4 == 0)
{
DelayK(100);
if(S4 == 0)
{
L4 = 0;
while(S4==0);
L4=1;
}
}
}
void mian()
{
while(1)
{
SelectHC573(4);
ScanKeys_Alone();
}
}
2、独立按键的扩展操作
(1)按下S7按键
if(S7 == 0)
{
DelayK(100); //抖动处理
if(S7 == 0) //确认按下了S7
{
if(stat_k == 0) //没有控制按键按下
{
L1=0; //点两个L1
stat_k==1; //状态表明有S7按下了
}
else if(stat_k == 1) //说明S7按下了
{
L1=1; //熄灭L1
stat_k=0; //状态归零
}
while(S7==0); //如果手没离开S7,使S7一直按着,就卡在这里,直到S7没处在按下的状态
}
}
(2)按下S6按键
if(S6 == 0)
{
DelayK(100);
if(S6 == 0)
{
if(stat_k == 0)
{
L2 = 0;
stat_k == 2;
}
else if(stat_k == 2)
{
L2 = 1;
stat_k =0;
}
while(S6==0);
}
}
(3)按下S5
if(S5 == 0)
{
DelayK(100);
if(S5 == 0)
{
if(stat_k==1)
{
L3=0;
while(S5 ==0);
L3= 1;
}
else if(stat_k==2)
{
L5=0;
while(S5==0);
L5= 1;
}
}
}
(4)按下S4
if(S4 == 0)
{
DelayK(100);
if(S4 == 0)
{
if(stat_k==1)
{
L4=0;
while(S5 ==0);
L4 = 1;
}
else if(stat_k==2)
{
L6=0;
while(S5==0);
L6 = 1;
}
}
}
}
(5)整体代码
#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;
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 DelayK(unsigned char t)
{
while(t--);
}
unsigned char stat_k = 0; //¶¨ÒåÒ»¸ö״̬±äÁ¿
void ScanKeys_Alone()
{
if(S7 == 0)
{
DelayK(100);
if(S7 == 0)
{
if(stat_k == 0)
{
L1=0;
stat_k==1;
}
else if(stat_k == 1)
{
L1=1;
stat_k=0;
}
while(S7==0);
}
}
if(S6 == 0)
{
DelayK(100);
if(S6 == 0)
{
if(stat_k == 0)
{
L2 = 0;
stat_k == 2;
}
else if(stat_k == 2)
{
L2 = 1;
stat_k =0;
}
while(S6==0);
}
}
if(S5 == 0)
{
DelayK(100);
if(S5 == 0)
{
if(stat_k==1)
{
L3=0;
while(S5 ==0);
L3= 1;
}
else if(stat_k==2)
{
L5=0;
while(S5==0);
L5= 1;
}
}
}
if(S4 == 0)
{
DelayK(100);
if(S4 == 0)
{
if(stat_k==1)
{
L4=0;
while(S5 ==0);
L4 = 1;
}
else if(stat_k==2)
{
L6=0;
while(S5==0);
L6 = 1;
}
}
}
}
void mian()
{
while(1)
{
SelectHC573(4);
ScanKeys_Alone();
}
}