📄 4×4keyboard.c
字号:
/****************************************************************
*行列4*4键盘驱动程序
*作者:Eqianli studio
*日期:2006/12/1
*说明:main()主函数,delay()延时,keyscan()键盘扫描,PinLamp()点灯
****************************************************************/
/****************************************************************
/////////////////////Beginning of Program////////////////////////
****************************************************************/
#include <reg52.h>
//#include"AT89X52.H"
#include"stdio.h"
#include <absacc.h>
#define TRUE 1
#define FALSE 0
#define DELAY_VALUE 3
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
sbit P2_4=P2^4;
sbit P2_5=P2^5;
sbit P2_6=P2^6;
sbit P2_7=P2^7;
sbit P3_0=P3^0;
#define PinDrvKey1 P2_0
#define PinDrvKey2 P2_1
#define PinDrvKey3 P2_2
#define PinDrvKey4 P2_3
#define PinScanKey1 P2_4
#define PinScanKey2 P2_5
#define PinScanKey3 P2_6
#define PinScanKey4 P2_7
#define Lamp P3_0
void initial(void);
void delay(short i);
unsigned char keyscan(void);
void PinLamp(void);
void main(void)
{unsigned char keyword;
initial();
while(1)
{ keyword=keyscan();
}
}
void initial(void)
{
P3_0=TRUE; //发光三极管高为不亮
}
void PinLamp(void)
{
Lamp=FALSE;
delay(DELAY_VALUE);
delay(DELAY_VALUE);
delay(DELAY_VALUE);
delay(DELAY_VALUE);
delay(DELAY_VALUE);
Lamp=TRUE;
}
unsigned char keyscan(void)
{
PinDrvKey1=FALSE;
PinScanKey1=TRUE;
if(PinScanKey1!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
return 0; }
PinScanKey2=TRUE;
if(PinScanKey2!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
return 4; }
PinScanKey3=TRUE;
if(PinScanKey3!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
return 8; }
PinScanKey4=TRUE;
if(PinScanKey4!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
return 12; }
PinDrvKey1=TRUE; //扫描完一列
PinDrvKey2=FALSE;
PinScanKey1=TRUE;
if(PinScanKey1!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
return 1; }
PinScanKey2=TRUE;
if(PinScanKey2!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
return 5; }
PinScanKey3=TRUE;
if(PinScanKey3!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
return 9; }
PinScanKey4=TRUE;
if(PinScanKey4!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
return 13; }
PinDrvKey2=TRUE; //扫描完二列
PinDrvKey3=FALSE;
PinScanKey1=TRUE;
if(PinScanKey1!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
return 2; }
PinScanKey2=TRUE;
if(PinScanKey2!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
return 6; }
PinScanKey3=TRUE;
if(PinScanKey3!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
return 10; }
PinScanKey4=TRUE;
if(PinScanKey4!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
return 14; }
PinDrvKey3=TRUE; //扫描完三列
PinDrvKey4=FALSE;
PinScanKey1=TRUE;
if(PinScanKey1!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
return 4; }
PinScanKey2=TRUE;
if(PinScanKey2!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
return 8; }
PinScanKey3=TRUE;
if(PinScanKey3!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
return 12; }
PinScanKey4=TRUE;
if(PinScanKey4!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
return 16; }
PinDrvKey4=TRUE; //扫描完四列
}
void delay(short i)
{
int j=1;
int k=0;
k=i*DELAY_VALUE;
while(j)
{if(j==k)
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -