行列4*4键盘驱动程序
/****************************************************************
*行列4*4键盘驱动程序
*作者:Eqianli studio
*日期:2006/11/2
*说明:main()主函数,delay()延时,keyscan()键盘扫描,PinLamp()点灯
****************************************************************/
/****************************************************************
/////////////////////Beginning of Program////////////////////////
****************************************************************/
#include"AT89X52.H"
#include"stdio.h"
#define TRUE 1
#define FALSE 0
#define DELAY_VALUE 3
#define PinDrvKey1 P1_0
#define PinDrvKey2 P1_1
#define PinDrvKey3 P1_2
#define PinDrvKey4 P1_3
#define PinScanKey1 P1_4
#define PinScanKey2 P1_5
#define PinScanKey3 P1_6
#define PinScanKey4 P1_7
#define Lamp P2_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)
{
P2_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 1; }
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 7; }
PinScanKey4=TRUE;
if(PinScanKey4!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
return 10; }
PinDrvKey1=TRUE; //扫描完一列
PinDrvKey2=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 5; }
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 11; }
PinDrvKey2=TRUE; //扫描完二列
PinDrvKey3=FALSE;
PinScanKey1=TRUE;
if(PinScanKey1!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
return 3; }
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 9; }
PinScanKey4=TRUE;
if(PinScanKey4!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey4!=TRUE)
return 12; }
PinDrvKey3=TRUE; //扫描完三列
PinDrvKey4=FALSE;
PinScanKey1=TRUE;
if(PinScanKey1!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey1!=TRUE)
return 13; }
PinScanKey2=TRUE;
if(PinScanKey2!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey2!=TRUE)
return 14; }
PinScanKey3=TRUE;
if(PinScanKey3!=TRUE)
PinLamp();
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
{
delay(DELAY_VALUE);
if(PinScanKey3!=TRUE)
return 15; }
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=0;
int k=0;
k=i*DELAY_VALUE;
while(j
}