#define GPIO_KEY P1 #define GPIO_LED P0
void Delay10ms(unsigned int c); unsigned char Key_Scan(); void main(void) {
Unsigned char ledValue, keyNum; ledValue = 0x01; while (1) {
keyNum = Key_Scan(); switch (keyNum) {
case(0xFE) : ledValue = 0x01; break;
case(0xFD) : ledValue = 0x02; break;
case(0xFB) : ledValue = 0x04; break;
case(0xF7) : ledValue = 0x08; break;
case(0xEF) : ledValue = 0x10; break;
case(0xDF) : ledValue = 0x20; break;
case(0xBF) : ledValue = 0x40; break;
case(0x7F) : ledValue = 0x80; break; default: break; }
GPIO_LED = ledValue; } }
unsigned char Key_Scan() {
unsigned char keyValue = 0 , i; if (GPIO_KEY != 0xFF) {
Delay10ms(1);
if (GPIO_KEY != 0xFF) {
keyValue = GPIO_KEY; i = 0;
while ((i<50) && (GPIO_KEY != 0xFF)) {
Delay10ms(1); i++; } } }
return keyValue; }
void Delay10ms(unsigned int c) {
unsigned char a, b;
for (;c>0;c--) {
for (b=38;b>0;b--) {
for (a=130;a>0;a--); }
} }
2、矩阵键盘(静态数码管显示矩阵键盘键值)
#include#define GPIO_DIG P0 #define GPIO_KEY P1
unsigned char code DIG_CODE[17]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,
0x79,0x71};
unsigned char KeyValue;
void Delay10ms(unsigned int c); void KeyDown(); void main(void) {
while(1) {
KeyDown(); GPIO_DIG ~DIG_CODE[KeyValue]; } }
void KeyDown(void) {
char a = 0;
GPIO_KEY=0x0f; if(GPIO_KEY!=0x0f)
= {
Delay10ms(1);
if(GPIO_KEY!=0x0f) {
GPIO_KEY=0X0F; switch(GPIO_KEY) {
case(0X07): KeyValue=0;break; case(0X0b): KeyValue=4;break; case(0X0d): KeyValue=8;break; case(0X0e): KeyValue=12;break; }
GPIO_KEY=0XF0; switch(GPIO_KEY)
{
case(0X70):
KeyValue=KeyValue+3;break; case(0Xb0):
KeyValue=KeyValue+2;break; case(0Xd0):
KeyValue=KeyValue+1;break; case(0Xe0):
KeyValue=KeyValue;break; }
while((a<50) && (GPIO_KEY!=0xf0)) {
Delay10ms(1); a++; } } } }
void Delay10ms(unsigned int c) {
unsigned char a, b; for (;c>0;c--) {
for (b=38;b>0;b--) {
for (a=130;a>0;a--); } }