仿真电路如下:
电路阐明:
键盘由P1口引出,123是列线,4567是行线。P2和P3口的LED是检测按键的。
程序如下:
#include
#include
#define uchar unsigned char
uchar key;
delay (void)
{
unsigned int i=1000;
while(i–);
}
output (void)
{
switch(key)
{
case 0: P2=0xfe; break;
case 1: P2=0xfd; break;
case 2: P2=0xfb; break;
case 3: P2=0xf7; break;
case 4: P3=0xfe; break;
case 5: P3=0xfd; break;
case 6: P3=0xfb; break;
case 7: P3=0xf7; break;
case 8: P3=0xef; break;
case 9: P3=0xdf; break;
case 10: P3=0xbf; break;
case 11: P3=0x7f; break;
case 12: P3=0xff; break;
}
}
keyscan (void)
{
uchar temp=0;
P1=0xF0;
delay ();
if(P1!=0xF0)
{
P1=0xF0;
delay();
temp=P1;
temp=temp&0xF0;
temp=~((temp>>4)|0xF0);
if(temp==1)key=0;
else if(temp==2) key=1;
else if(temp==4) key=2;
else if(temp==8) key=3;
else key=12;
P1=0x0F;
delay();
temp=P1;
temp=temp&0x0F;
temp=~(temp|0xF0);
if(temp==2) key=key+0;
else if(temp==4) key=key+4;
else if(temp==8) key=key+8;
else key=12;
output ();
}
else return;
}
main (void)
{
keyscan ();
}
程序阐明:
函数界说很清楚,吃饭了,不多说了。