本來感覺矩陣鍵盤很簡單的,沒有想到編了一個程序,編了一個下午,哎,難怪有人說,如果技術和科學有你理想那么容易,那它就不是科學了。在參考http://www.zg4o1577.cn/mcuteach/226.html 后終于把矩陣鍵盤里的16個按鍵控制數碼管顯示0至16的效果實現了出來,呵呵,功夫不負有心人,雖然編了好久,好累,好困。但還是一個字,值!下面看下程序吧,如果各位高手看到了我程序有所不足的,麻煩指點。
下面是我的程序:#include<reg52.h>
unsigned char temp;
char x;
unsigned char number[]={
0x3f,
0x06,
0x5b,
0x4f,
0x66,
0x6d,
0x7d,
0x07,
0x7f,
0x6f,
0x77,
0x7c,
0x39,
0x5e,
0x79,
0x71
}; //¶¨ÒåÊý×é0ÖÁ15
void delay()
{
int i,j;
for(i=5;i<0;i--)
for(j=120;j<0;j--);
} //ÑÓʱ³ÌÐò
void main()
{
P1 = 0x00; //³ÌÐò¿ªÊ¼Ê±ÊýÂë¹Ü²»ÏÔʾ
while(1)
{
P3 = 0x7f; //ʹ°´¼üS1ÖÁS4ÏÔʾ0ÖÁ3
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
delay();
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
temp = P3;
switch(temp)
{
case 0x7e:x=0;
break;
case 0x7d:x=1;
break;
case 0x7b:x=2;
break;
case 0x77:x=3;
break;
}
while(temp!=0x0f) //Ìø³ö
{
temp=P3;
temp=temp&0x0f;
}
P1 = number[x];
}
}
//cond
P3 = 0xbf; //ʹ°´¼üS5ÖÁS8ÏÔʾ4ÖÁ7
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
delay();
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
temp = P3;
switch(temp)
{
case 0xbe:x=4;
break;
case 0xbd:x=5;
break;
case 0xbb:x=6;
break;
case 0xb7:x=7;
break;
}
while(temp!=0x0f) //Ìø³ö
{
temp=P3;
temp=temp&0x0f;
}
P1 = number[x];
}
}
//thirth
P3 = 0xdf; //ʹS9ÖÁS12ÏÔʾ8ÖÁb
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
delay();
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
temp = P3;
switch(temp)
{
case 0xde:x=8;
break;
case 0xdd:x=9;
break;
case 0xdb:x=10;
break;
case 0xd7:x=11;
break;
}
while(temp!=0x0f) //Ìø³ö
{
temp=P3;
temp=temp&0x0f;
}
P1 = number[x];
}
}
//four
P3 = 0xef; //ʹS13ÖÁS16ÏÔʾCÖÁF
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
delay();
temp = P3;
temp = temp & 0x0f;
while(temp != 0x0f)
{
temp = P3;
switch(temp)
{
case 0xee:x=12;
break;
case 0xed:x=13;
break;
case 0xeb:x=14;
break;
case 0xe7:x=15;
break;
}
while(temp!= 0x0f)
{
temp = P3;
temp = temp&0x0f;
}
while(temp!=0x0f) //Ìø³ö
{
temp=P3;
temp=temp&0x0f;
}
P1 = number[x];
}
}
}
}
感覺好長,如果厲害點的話,應該可以做編得更短的,有時間只有再把它完善了。