一.原理图
二.原理图分析
如上图,当跳线帽连接J5中的2和3时,此时为独立按键模式(左侧四个按键有效)。按键的左端与GND相连,右端分别与P3.0~4相连。当有按键按下时,对应的I/O口将变为低电平。例如,当S6按下时,使得按键两端相连,P3.1为低电平。
三.代码
一.三行代码
#define KEYPORT P3
unsigned char Trg;//判断哪个按键被按下
unsigned char Cont;//判断按键是否弹回
void Key_Read(void)
{
unsigned char ReadData=KEYPORT^0xff;//KEYPORT为输入,^为异或即不同是为1
Trg=ReadData&(ReadData^Cont);
Cont=ReadData;
}
代码解释:
(1).No key: ReadData=0;Trg=0;Cont=0;
(2).P30 is 0: KEYPORT=0xfe;ReadDate=0x01;Trg=0x01& (0x01^0x00)=0x01;Cont=0x01;
P31 is 0: KEYPORT=0xfd;ReadDate=0x02;Trg=0x02&(0x02^0x00)=0x02;Cont=0x02;
(3).when P30 is always 0: Trg=0x01&(0x01^0x01)=0;Cont=0x01;
when P31 is always 0: Trg=0x02&(0x02^0x02)=0;Cont=0x02;
(4).when P30 is 1:Trg=0x00&(0x00^0x01)=0;Cont=0;
when P31 is 1:Trg=0x00&(0x00^0x01)=0;Cont=0;
二.状态机
#define key_input P3
#define key_state_0 0 //判断按键是否按下
#define key_state_1 1 //判断按键是否是抖动
#define key_state_2 2 //判断按键是否弹起
#define key_mask 0x0f //屏蔽不需要的IO
char Key_Read(void)
{
static char key_state=0;
unsigned char key_press,key_return=0;
key_press=key_input&key_mask; //屏蔽P3.4~7
switch(key_state)
{
case key_state_0:
if(key_press!=key_mask) key_state=key_state_1;
break;
case key_state_1:
if(key_press!=key_mask)
{
if(key_press==0x0e) key_return=7; //S7
if(key_press==0x0d) key_return=6; //S6
if(key_press==0x0b) key_return=5; //S5
if(key_press==0x07) key_return=4; //S4
key_state=key_state_2;
}
else
key_state=key_state_0;
break;
case key_state_2:
if(key_press==0x0f)
key_state=key_state_0;
break;
}
return key_return;
}
以上两种代码需要配合定时器使用,通过10ms进行消抖:
unsigned char key_return; //按键返回值
unsigned char key_flag; //消抖标记
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void main()
{
Timer0Init(); // 1ms定时器
EA=1; //打开总中断
ET0=1; //打开定时器0中断
while(1)
{
if(key_flag==1)
{
key_flag=0;
key_return=Key_Read();
switch(key_return)
{
case 4:
case 5:
case 6:
case 7:
}
}
}
}
void Time0_Interrupt(void) interrupt 1
{
static unsigned char count=0;
count++;
if(count==10) //10ms
{
count=0;
key_flag=1;
}
}
三.扩展方法
sbit KEY7=P3^0
sbit KEY6=P3^1
sbit KEY5=P3^2
sbit KEY4=P3^3
//Mode=1时支持连续按
unsigned char Key_Read(unsigned char mode)
{
static unsigned char key_value=1;
if(mode) key_value=1; //如果选择支持连续按键的模式,则每次都会返回按键对应的值
if(key_value&&(KEY4==0||KEY5==0||KEY6==0||KEY7==0))
{
Delay10ms();
key_value=0; //此值清零后,如果按住不放,下次扫描按键也不会返回按键对应的值,即只记第一次按下。
if(KEY4==0) return 4; //S4
if(KEY5==0) return 5; //S5
if(KEY6==0) return 6; //S6
if(KEY7==0) return 7; //S7
}
else if(KEY4==1&&KEY5==1&&KEY5==1&&KEY7==1) key_value=1;
return 0;
}
void main()
{
unsigned char key_return;
while(1)
{
key_return=Key_Read(0); //不使用连续按键
switch(key_return):
{
case 4:
case 5:
case 6:
case 7:
}
}
}
补充:实际测试后,状态机比三行代码的更加准确灵敏。