矩阵键盘控制0到F

来源:互联网 发布:淘宝链接转化短链接 编辑:程序博客网 时间:2024/05/19 22:43

矩阵键盘控制0到F

#include<reg52.h>

 
code unsigned char segcode[16] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e };
 void delay(unsigned int t );
 unsigned char keyscan(void);
 void onedisplay(unsigned char p,unsigned char n);


void main()
{
unsigned char keycode;
while(1)
 {
  keycode = keyscan();


if(keycode !=0 )
onedisplay(7,keycode -1);
 }
}
 void delay(unsigned int t )
 {
  while( t -- );
 }
void onedisplay(unsigned char p,unsigned char n)
{
P0 = 0xff;
P2 = ~(0x01<<p);
P0 = segcode[n];
}
unsigned char keyscan(void)
{
unsigned char state;
unsigned char key = 0;


 //扫描第1行
P1 = 0xfe;
if( P1 != 0xfe )
{
    state = P1;


switch (state)
{
case 0xee : key = 1; break;
case 0xde : key = 2; break;
case 0xbe : key = 3; break;
case 0x7e : key = 4; break;
}
}
 //扫描第2行
P1 = 0xfd;
if( P1 != 0xfd )
{
    state = P1;


switch (state)
{
case 0xed : key = 5; break;
case 0xdd : key = 6; break;
case 0xbd : key = 7; break;
case 0x7d : key = 8; break;
}


   }
 //扫描第3行
P1 = 0xfb;
if( P1 != 0xfb )
{
    state = P1;


switch (state)
{
case 0xeb : key = 9; break;
case 0xdb : key = 10; break;
case 0xbb : key = 11; break;
case 0x7b : key = 12; break;
}


}
    //扫描第4行
P1 = 0xf7;
if( P1 != 0xf7 )
{
    state = P1;


switch (state)
{
case 0xe7 : key = 13; break;
case 0xd7 : key = 14; break;
case 0xb7 : key = 15; break;
case 0x77 : key = 16; break;
}
 }
return key;


}
原创粉丝点击