本人看了郭天祥的书,看了他的4*4按键电路编程,然后自己想了想就编了这个自己觉得比较简化的程序,能写进单片机,但是就是实现不了功能!求指导!
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit duan=P2^6;
sbit wei=P2^7;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void yanshi(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void xian(uchar num)
{
P0=table[num];
duan=1;
duan=0;
}
void matrixkeyscan()
{
uchar key,kan;
P3=0xf0;
yanshi(10);
if(P3!=0xf0)
{
yanshi(10);
if(P3!=0xf0)
{
key=P3;
switch(key)
{
case 0xee:
kan=0;
break;
case 0xde:
kan=1;
break;
case 0xbe:
kan=2;
break;
case 0x7e:
kan=3;
break;
case 0xed:
kan=4;
break;
case 0xdd:
kan=5;
break;
case 0xbd:
kan=6;
break;
case 0x7d:
kan=7;
break;
case 0xeb:
kan=8;
break;
case 0xdb:
kan=9;
break;
case 0xbb:
kan=10;
break;
case 0x7b:
kan=11;
break;
case 0xe7:
kan=12;
break;
case 0xd7:
kan=13;
break;
case 0xb7:
kan=14;
break;
case 0x77:
kan=15;
break;
}
while(P3!=0xf0)
{
yanshi(10);
P3=0xf0;
yanshi(5);
}
xian(kan);
}
}
}
void main()
{
P0=0xff;
duan=1;
duan=0;
P0=0xc0;
wei=1;
wei=0;
while(1)
{
matrixkeyscan();
}
} |