嗯,看起来不错,有没有写过矩形键盘的呢,我谢了个2行5个按键的键盘,还望赐教;
另外问一下松手检测是什么意思,是松手后再执行函数吗?
#include<reg51.h>
#include"delay/delay.h"
#define uchar unsigned char
#define uint unsigned int
sbit led_1 = P2^0;
sbit led_2 = P2^1;
sbit led_0 = P2^2;
sbit line1 = P3^3;
sbit line2 = P3^4;
uchar table_key[2][3]={{0xF6,0xF5,0xF3},{0xEE,0xED}}; //按键码
void keyscan(void)
{
#define f_longkey 30000
uchar i=0;
uint key_time=0;
//=================Line1=======================================
line2 = 1;
line1 = 0;
for(i=0;i<3;i++)
{
if((P3 |table_key[0])==table_key[0])
{
delayms(10);
if((P3 |table_key[0])==table_key[0])
{
while(((P3 |table_key[0])==table_key[0]) && (++key_time<f_longkey)); //等待释放
if(key_time<f_longkey) //短按
{
if(i==0) //按键0
{
led_1=0;
}
else if(i==1) //按键1
{
led_2=0;
}
else if(i==2) //按键2
{
led_0=1;
led_1=1;
led_2=1;
}
while((P3 |table_key[0])==table_key[0]); //等待释放
}
else //长按
{
if(i==0)
{
led_2=0;
}
else if(i==1)
{
led_0=0;
}
else if(i==2)
{
led_2=1;
led_1=1;
led_0=1;
}
while((P3 |table_key[0])==table_key[0]); //等待释放
}
}
}
}
//===============line2=====================================================
line2 = 0;
line1 = 1;
key_time = 0;
for(i=0;i<2;i++)
{
if((P3 |table_key[1])==table_key[1])
{
delayms(10);
if((P3 |table_key[1])==table_key[1])
{
while(((P3 |table_key[1])==table_key[1]) && (++key_time<f_longkey)); //等待释放
if(key_time<f_longkey)//短按
{
if(i==0) //按键0
{
led_0=1;
}
else if(i==1) //按键1
{
led_0=0;
}
while((P3 |table_key[1])==table_key[1]); //等待释放
}
else //长按
{
if(i==0) //按键0
{
led_2=1;
led_1=1;
led_0=1;
}
else if(i==1) //按键1
{
led_2=0;
led_1=0;
led_0=0;
}
while((P3 |table_key[1])==table_key[1]); //等待释放
}
}
}
}
}
void main()
{
init_delay();
while(1)
{
keyscan();
}
}
|