大家学会了独立键盘的操作之后可以操作矩阵键盘的工作原理:
#include "reg52.h"
#define bus P1
//#define dat P0
#define uint unsigned int
#define uchar unsigned char
uint k = 0;
uchar code tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
/**************************************************************************************
** Function Name: delay
** Descriptions: 延时
** Input Parameters: uint z
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void delayms(int w)
{
int i;
for(i = 0;i < w;i++)
{
;
}
}
/**************************************************************************************
** Function Name: display
** Descriptions: 数码管显示
** Input Parameters: void
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void display()
{
P2 = 0xF0;
P0 = tab[k];
// P0 = 0xC0;
}
/**************************************************************************************
** Function Name: keyscan
** Descriptions: 矩阵键盘扫描
** Input Parameters: void
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void keyscan()
{
uchar a;
bus=0xfe; //扫描第一行
delayms(600);
a=bus;
a=a&0xf0;
if(a!=0xf0)
{
delayms(200);
if(a!=0xf0)
{
switch(a)
{
case 0xe0:
k=0x01;
break;
case 0xd0:
k=0x02;
break;
case 0xb0:
k=0x03;
break;
case 0x70:
k=0x04;
break;
}
display();
while(a!=0xf0)
{
bus=0xf0;
delayms(200);
a=bus;
}
}
}
bus=0xfd; //扫描第二行
delayms(600);
a=bus;
a=a&0xf0;
if(a!=0xf0)
{
delayms(200);
if(a!=0xf0)
{
switch(a)
{
case 0xe0:
k=0x05;
break;
case 0xd0:
k=0x06;
break;
case 0xb0:
k=0x07;
break;
case 0x70:
k=0x08;
break;
}
display();
while(a!=0xf0)
{
bus=0xf0;
delayms(200);
a=bus;
}
}
}
bus=0xfb; //扫描第3行 delayms(6000);
a=bus;
a=a&0xf0;
if(a!=0xf0)
{
delayms(200);
if(a!=0xf0)
{
switch(a)
{
case 0xe0:
k=0x09;
break;
case 0xd0:
k=0x0a;
break;
case 0xb0:
k=0x0b;
break;
case 0x70:
k=0x0c;
break;
}
display();
while(a!=0xf0)
{
bus=0xf0;
delayms(200);
a=bus;
}
}
}
bus=0xf7; //扫描第4行
delayms(600);
a=bus;
a=a&0xf0;
if(a!=0xf0)
{
delayms(200);
if(a!=0xf0)
{
switch(a)
{
case 0xe0:
k=0x0d;
break;
case 0xd0:
k=0x0e;
break;
case 0xb0:
k=0x0f;
break;
case 0x70:
k=0x00;
break;
}
display();
while(a!=0xf0)
{
bus=0xf0;
delayms(200);
a=bus;
}
}
}
}
/**************************************************************************************
** Function Name: main
** Descriptions: 主函数
** Input Parameters: void
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void main()
{
while(1)
{
keyscan();
}
}
文章评论(0条评论)
登录后参与讨论