|
不要用扫描太占资源了,用中断
4*4键盘吗,用int0中断口
给你一个c语言的
#include "INTRINS.H"
#include "REG51.H"
typedef unsigned char UCHAR;
typedef unsigned int UINT;
typedef void (*keyfun)(void);
#ifndef __keyboarddefinition_h__
#define __keyboarddefinition_h__
#define NULL 0
#define YES 1
#define NO 0
#define ON 1
#define OFF 0
#define KeyPort P2
#define keyrowscan 0x0f
#define keycolumnscan 0xf0
#define key1value 0xe7
#define key2value 0xeb
#define key3value 0xed
#define key4value 0xee
#define key5value 0xd7
#define key6value 0xdb
#define key7value 0xdd
#define key8value 0xde
#define key9value 0xb7
#define key10value 0xbb
#define key11value 0xbd
#define key12value 0xbe
#define key13value 0x77
#define key14value 0x7b
#define key15value 0x7d
#define key16value 0x7e
#endif
keyfun key[17];
UCHAR axthevalueofkey=0x77;
sbit P30=P3^0;
bit keydown=0;
void delay();
//void register(keyfun temp,void *a)
//{temp=a;
//}
// register(key[1],key1valuefun) ;
void key1valuefun()//这个函数合格.
{ UCHAR m;
P1=0;
m=0xfe;
while(1)
if(axthevalueofkey==key1value)
{P1=_cror_(m,1) ;
delay();}
}
void keyboardinitialize()//合格
{KeyPort=keyrowscan;
IE=IE|0x01;
IP|=0x01;
}
void delay() //合格
{ int i;
for (i=0;i<200;i++);
}
keyactiondeal() interrupt 0 //这个函数合格.
{ UCHAR Firstkeyvalue;
UCHAR secondkeyvalue;
// UCHAR thirdkeyvalue;
UCHAR tempkeyvalue;
IE&=0Xfe;
Firstkeyvalue=KeyPort&keyrowscan ;
KeyPort=keyrowscan;
delay();
secondkeyvalue=KeyPort&keyrowscan;
if (Firstkeyvalue==secondkeyvalue)
{ tempkeyvalue=secondkeyvalue;
KeyPort=keycolumnscan;
tempkeyvalue|=KeyPort;
axthevalueofkey=tempkeyvalue;
KeyPort=keycolumnscan;
while ((KeyPort&keycolumnscan)!=keycolumnscan)
{ KeyPort=keycolumnscan;
}
IE|=0x01;
}
}
void main()
{ IE=90;
P30=0;
SP=0x60;
key[1]=key1valuefun;
keyboardinitialize();
while (1)
{ switch (axthevalueofkey)
{
case key1value:
{(*key[1])(); break;}
case key2value:
{(*key[2])(); break;}
/**************用时开启**
case: key2value
(*key[3])(); break;
case: key2value
(*key[4])(); break;
case: key2value
(*key[5])(); break;
case: key2value
(*key[6])(); break;
case: key2value
(*key[7])(); break;
case: key2value
(*key[8])(); break;
case: key2value
(*key[9])(); break;
case: key2value
(*key[10])(); break;
case: key2value
(*key[11])(); break;
case: key2value
(*key[12])(); break;
case: key2value
(*key[13])(); break;
case: key2value
(*key[14])(); break;
case: key2value
(*key[15])(); break;
case: key2value
(*key[16])(); break;
*****/
}
}
} |
|