>一、电路
二、显示、延时、蜂鸣器程序
void display(uchar shu,uchar wei) { P2=(P2&0xe3)+(wei<<2); P0=tab[shu]; delay(2); P0=0x00; }
void delay(uint ms) { uint i,j; for(i=ms;i>0;i--) for(j=100;j>0;j--); }
//************didi****************** void didi(uint di) { while(di--) { bz=0; delay(1); bz=1; delay(1); } }
三、循环扫描原理
1、第一行
P1=0x7f; if(P1!=0x7f) { delay(20); if(P1!=0x7f) { v=P1; while(P1!=0x7f); switch(v) { case 0x77:key=1;break; case 0x7b:key=2;break; case 0x7d:key=3;break; case 0x7e:key=4;break; default: key=0;break; } } }
2、第二行
P1=0xbf; if(P1!=0xbf) { delay(20); if(P1!=0xbf) { v=P1; while(P1!=0xbf); switch(v) { case 0xb7:key=5;break; case 0xbb:key=6;break; case 0xbd:key=7;break; case 0xbe:key=8;break; default:key=0;break; } } }
3、第三行
P1=0xdf; if(P1!=0xdf) { delay(20); if(P1!=0xdf) { v=P1; while(P1!=0xdf); switch(v) { case 0xd7:key=9;break; case 0xdb:key=10;break; case 0xdd:key=11;break; case 0xde:key=12;break; default:key=0;break; } } }
4、第四行
P1=0xef; if(P1!=0xef) { delay(20); if(P1!=0xef) { v=P1; while(P1!=0xef); switch(v) { case 0xe7:key=13;break; case 0xeb:key=14;break; case 0xed:key=15;break; case 0xee:key=16;break; default:key=0;break; } } }
四、完整程序
#include <REGX52.H> #define uchar unsigned char #define uint unsigned int void delay(uint ms); void display(uchar shu,uchar wei); void didi(uint di); uchar tab[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; sbit bz=P1^5; void main() { uchar key,v; while(1) { display(key/10%10,7); display(key/1%10,6); P1=0x7f; if(P1!=0x7f) { delay(20); if(P1!=0x7f) { v=P1; while(P1!=0x7f); didi(50); switch(v) { case 0x77:key=1;break; case 0x7b:key=2;break; case 0x7d:key=3;break; case 0x7e:key=4;break; default: key=0;break; } } } P1=0xbf; if(P1!=0xbf) { delay(20); if(P1!=0xbf) { v=P1; while(P1!=0xbf); didi(50); switch(v) { case 0xb7:key=5;break; case 0xbb:key=6;break; case 0xbd:key=7;break; case 0xbe:key=8;break; default:key=0;break; } } } P1=0xdf; if(P1!=0xdf) { delay(20); if(P1!=0xdf) { v=P1; while(P1!=0xdf); didi(50); switch(v) { case 0xd7:key=9;break; case 0xdb:key=10;break; case 0xdd:key=11;break; case 0xde:key=12;break; default:key=0;break; } } } P1=0xef; if(P1!=0xef) { delay(20); if(P1!=0xef) { v=P1; while(P1!=0xef); didi(50); switch(v) { case 0xe7:key=13;break; case 0xeb:key=14;break; case 0xed:key=15;break; case 0xee:key=16;break; default:key=0;break; } } } } }
void didi(uint di) { while(di--) { bz=0; delay(1); bz=1; delay(1); } }
void display(uchar shu,uchar wei) { P2=(P2&0xe3)+(wei<<2); P0=tab[shu]; delay(2); P0=0x00; }
void delay(uint ms) { uint i,j; for(i=ms;i>0;i--) for(j=100;j>0;j--); }
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |