在51單片機的p1口上面接8個獨立按鍵,我們可以按照如下的思路來檢測按鍵.
#include<reg51.h>
main()
{
unsigned char sw;
//unsigned char P1;
//unsigned char P3;
unsigned int t;
while(1)
{
sw=P3|0xf0;
switch(sw)
{
case 0xfe:P1=0xfe;
break ;
for (t=0;t<<60000;t++);
case 0xfd:P1=0xf9;
for (t=0;t<<60000;t++);
break;
case 0xfb:P1=0xc7;
break;
case 0xf7:P1=0x0f;
for (t=0;t<<60000;t++);
break;
default: P1=0xff;
for (t=0;t<<60000;t++);
break;
}
}
}