单片机中独立键盘和矩阵键盘如何一起使用 请用C语言写个程序说明,谢谢。
2个回答
展开全部
这个很好处理呀,比如以下举例,独立+矩阵,实现独立按键相当于类似SHIFT作用的效果。
#include<reg51.h>
#define uchar unsigned char
uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0到f
uchar keyval,num;
sbit skey=P1^0; //独立键P1.0
void delay(uchar a)
{
uchar i,j;
for(i=0;i<a;i++)
for(j=0;j<125;j++);
}
uchar kbscan(void) //矩阵键扫描程序
{
unsigned char sccode,recode;
P3=0x0f; //发0扫描,列线输入
if ((P3 & 0x0f) != 0x0f) //有键按下
{
delay(20); //延时去抖动
if ((P3&0x0f)!= 0x0f)
{
sccode = 0xef; //逐行扫描初值
while((sccode&0x01)!=0)
{
P3=sccode;
if((P3&0x0f)!=0x0f)
{
recode=(P3&0x0f)|0xf0;
while((P3&0x0f)!=0x0f);//等待键抬起
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return 0; //无键按下,返回0
}
void getkey(void)
{
unsigned char key;
key=kbscan();
if(key==0)
{
return;
}
switch(key)
{
case 0x11:keyval=7;break;
case 0x12:keyval=4;break;
case 0x14:keyval=1;break;
case 0x18:keyval=10;break;
case 0x21:keyval=8;break;
case 0x22:keyval=5;break;
case 0x24:keyval=2;break;
case 0x28:keyval=0;break;
case 0x41:keyval=9;break;
case 0x42:keyval=6;break;
case 0x44:keyval=3;break;
case 0x48:keyval=11;break;
case 0x81:keyval=12;break;
case 0x82:keyval=13;break;
case 0x84:keyval=14;break;
case 0x88:keyval=15;break;
default:keyval=0xff;break;
}
//以下处理独立按键
if(skey==0)
{
if(keyval!=0xff)keyval+=16; //如果独立键按下,键值加16
while(skey==0); //等待独立键释放
}
}
void t0isr() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
switch(num)
{
case 0:P2=0x01;break;
case 1:P2=0x02;break;
case 2:P2=0x04;break;
case 3:P2=0x08;break;
default:break;
}
if(keyval<16) P0=~tab[keyval]; //独立键未按正常显示
else P0=~(tab[keyval]|0x80); //独立键按下显示+DP
num++;
num&=0x03;
}
main()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TR0=1;
ET0=1;
EA=1;
while(1)
{
getkey();
}
}
#include<reg51.h>
#define uchar unsigned char
uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0到f
uchar keyval,num;
sbit skey=P1^0; //独立键P1.0
void delay(uchar a)
{
uchar i,j;
for(i=0;i<a;i++)
for(j=0;j<125;j++);
}
uchar kbscan(void) //矩阵键扫描程序
{
unsigned char sccode,recode;
P3=0x0f; //发0扫描,列线输入
if ((P3 & 0x0f) != 0x0f) //有键按下
{
delay(20); //延时去抖动
if ((P3&0x0f)!= 0x0f)
{
sccode = 0xef; //逐行扫描初值
while((sccode&0x01)!=0)
{
P3=sccode;
if((P3&0x0f)!=0x0f)
{
recode=(P3&0x0f)|0xf0;
while((P3&0x0f)!=0x0f);//等待键抬起
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return 0; //无键按下,返回0
}
void getkey(void)
{
unsigned char key;
key=kbscan();
if(key==0)
{
return;
}
switch(key)
{
case 0x11:keyval=7;break;
case 0x12:keyval=4;break;
case 0x14:keyval=1;break;
case 0x18:keyval=10;break;
case 0x21:keyval=8;break;
case 0x22:keyval=5;break;
case 0x24:keyval=2;break;
case 0x28:keyval=0;break;
case 0x41:keyval=9;break;
case 0x42:keyval=6;break;
case 0x44:keyval=3;break;
case 0x48:keyval=11;break;
case 0x81:keyval=12;break;
case 0x82:keyval=13;break;
case 0x84:keyval=14;break;
case 0x88:keyval=15;break;
default:keyval=0xff;break;
}
//以下处理独立按键
if(skey==0)
{
if(keyval!=0xff)keyval+=16; //如果独立键按下,键值加16
while(skey==0); //等待独立键释放
}
}
void t0isr() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
switch(num)
{
case 0:P2=0x01;break;
case 1:P2=0x02;break;
case 2:P2=0x04;break;
case 3:P2=0x08;break;
default:break;
}
if(keyval<16) P0=~tab[keyval]; //独立键未按正常显示
else P0=~(tab[keyval]|0x80); //独立键按下显示+DP
num++;
num&=0x03;
}
main()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TR0=1;
ET0=1;
EA=1;
while(1)
{
getkey();
}
}
追问
独立键盘和矩阵键盘的端口都为P3,怎么解决
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit du = P2^6;
sbit we = P2^7;
sbit s2 = P3^0;
sbit s3 = P3^1;
sbit s4 = P3^2;
sbit s5 = P3^3;
sbit led1 = P1^0;
uchar code t[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
uchar keyscan()
{
uchar cord_l,cord_h;
P3 = 0xf0;
if((P3 & 0xf0) != 0xf0)
{
delay(5);
if((P3 & 0xf0) != 0xf0)
{
cord_l = P3 & 0xf0;
P3 = cord_l | 0x0f;
cord_h = P3 & 0x0f;
while((P3 & 0x0f) != 0x0f);
P3 = 0xff;
return(cord_l + cord_h);
}
}
}
void keypro()
{
switch (keyscan())
{
case 0xee: P1 = t[1]; break;
case 0xde: P1 = t[2]; break;
case 0xbe: P1 = t[3]; break;
case 0x7e: P1 = t[4]; break;
case 0xed: P1 = t[5]; break;
case 0xdd: P1 = t[6]; break;
case 0xbd: P1 = t[7]; break;
case 0x7d: P1 = t[8]; break;
case 0xeb: P1 = t[9]; break;
case 0xdb: P1 = t[10]; break;
case 0xbb: P1 = t[11]; break;
case 0x7b: P1 = t[12]; break;
case 0xe7: P1 = t[13]; break;
case 0xd7: P1 = t[14]; break;
case 0xb7: P1 = t[15]; break;
case 0x77: P1 = t[16]; break;
}
}
void alonekey()
{
P3 = 0xff;
if(s2 == 0)
{
delay(5);
if(s2 == 0)
{
led1 = 0;
}
while(!s2);
}
}
void main()
{
while(1)
{
keypro();
alonekey();
}
}
我的也是同一个P3口
#define uchar unsigned char
#define uint unsigned int
sbit du = P2^6;
sbit we = P2^7;
sbit s2 = P3^0;
sbit s3 = P3^1;
sbit s4 = P3^2;
sbit s5 = P3^3;
sbit led1 = P1^0;
uchar code t[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
uchar keyscan()
{
uchar cord_l,cord_h;
P3 = 0xf0;
if((P3 & 0xf0) != 0xf0)
{
delay(5);
if((P3 & 0xf0) != 0xf0)
{
cord_l = P3 & 0xf0;
P3 = cord_l | 0x0f;
cord_h = P3 & 0x0f;
while((P3 & 0x0f) != 0x0f);
P3 = 0xff;
return(cord_l + cord_h);
}
}
}
void keypro()
{
switch (keyscan())
{
case 0xee: P1 = t[1]; break;
case 0xde: P1 = t[2]; break;
case 0xbe: P1 = t[3]; break;
case 0x7e: P1 = t[4]; break;
case 0xed: P1 = t[5]; break;
case 0xdd: P1 = t[6]; break;
case 0xbd: P1 = t[7]; break;
case 0x7d: P1 = t[8]; break;
case 0xeb: P1 = t[9]; break;
case 0xdb: P1 = t[10]; break;
case 0xbb: P1 = t[11]; break;
case 0x7b: P1 = t[12]; break;
case 0xe7: P1 = t[13]; break;
case 0xd7: P1 = t[14]; break;
case 0xb7: P1 = t[15]; break;
case 0x77: P1 = t[16]; break;
}
}
void alonekey()
{
P3 = 0xff;
if(s2 == 0)
{
delay(5);
if(s2 == 0)
{
led1 = 0;
}
while(!s2);
}
}
void main()
{
while(1)
{
keypro();
alonekey();
}
}
我的也是同一个P3口
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询