注意了:这个代码你是没法直接运行的,但是如果你看得懂,随便改一改不超过1分钟就可以用
#include "reg51.h"
#include "myheader.h"
void displayNumber(unsigned char num)
{if(num==1){P1=0XFF;P10=P11=P14=P15=P16=0;}else if(num==0){P1=0XFF;P10=P13=P14=P15=P16=P17=0;}sleep(50000);P1=0XFF;
}
void judgeKey()
{int keydown=0;unsigned char state=0;unsigned char tag=0;unsigned char row=0,col=0;while(1){P2=0XFF;P3=0;keydown=0;while(keydown==0){if(P2!=0XFF)//说明没有键按下{keydown=1;}}if(keydown==1)//说明有按键按下了{row=0;col=0;//判断是哪一个按键//判断行P3=0;P2=0XFF;for(state=~P2&0X0F,tag=0;state!=0X01;state=state>>1 ){tag++;}row=tag;//判断列//逻辑错误:P2的高电平接到P3的低电平是不会把低电平变为高电平的,P2=0;P3=0XFF;for(state=~P3&0X0F,tag=0;state!=0X01;state=state>>1 ){tag++;}col=tag;if(row==1&&col==3){P34=1;displayNumber(1);}else{P34=0;displayNumber(0);}sleep(5000);while(keydown==1){if(P3&&0x0f==0X0F)//说明没有键按下{keydown=0;}}}}
}
int main()
{judgeKey();return 0;
}
void sleep(long int a)
{long int i=0;for(i=0;i<a;i++){;;;}
}