我写了一个程序8个按键控制8个led,也通过了,这个是4X4矩阵键盘,P1口接键盘,当按一下P1_0时,led1闪烁,按一下P1.1时,led2闪烁。。。。。 但是现在却是要按键不放才能闪烁,找不出原因,请各位给看看,问题出在哪?
#include <AT89X51.H>
unsigned char keyscan() { unsigned char row,col,i; P1=0xf0; row=P1&0xf0; //row=P1&0xf0; (这一行可以不要,为什么有的教程上要多写一行??) P1=0x0f; col=P1&0x0f; //col=P1&0x0f; i=~(col|row); // while((P1&0xf0)!=0xf0 ); return i;} void delay10ms(void) { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--){;;} }
void delay02s(void)
{ unsigned char i;
for(i=20;i>0;i--)
{delay10ms();
}
}
void main(void) { unsigned char key; while(1) { key=keyscan(); switch(key)
{ case 0x18:
P0_0=~P0_0;
delay02s(); break;
case 0x14:
P0_1=~P0_1;
delay02s();
break;
case 0x12:
P0_2=~P0_2;
delay02s(); break; case 0x28: P0_3=~P0_3; delay02s(); break; case 0x24: P0_4=~P0_4; delay02s(); break; case 0x22: P0_5=~P0_5; delay02s(); break; case 0x48: P0_6=~P0_6; delay02s(); break; case 0x44: P0_7=~P0_7; delay02s(); break; case 0x42: P0=0xff; delay02s(); break; case 0x84: P0=0x0; delay02s(); break; }
} }
|