>1、电路
2、延时、显示、蜂鸣器程序
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=123;j>0;j--); }
3、逐行检测程序
#include <REGX51.H> #define uchar unsigned char #define uint unsigned int void delay(uint ms); void display(uchar shu,uchar wei); void didi(uint di); sbit bz=P1^5; uchar tab[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0x40}; void main() { uchar key,v; while(1) { display(v/10%10,4); display(v/1%10,3); P1=0x7f; if(P1!=0x7f) { delay(10); if(P1!=0x7f) { key=P1; while(P1!=0x7f) {} didi(10); switch(key) { case 0x77:v=1;break; case 0x7b:v=2;break; case 0x7d:v=3;break; case 0x7e:v=4;break; default:v=0;break; } } } P1=0xbf; if(P1!=0xbf) { delay(10); if(P1!=0xbf) { key=P1; while(P1!=0xbf) {} didi(10); switch(key) { case 0xb7:v=5;break; case 0xbb:v=6;break; case 0xbd:v=7;break; case 0xbe:v=8;break; default:v=0;break; } } } P1=0xdf; if(P1!=0xdf) { delay(10); if(P1!=0xdf) { key=P1; while(P1!=0xdf) {} didi(10); switch(key) { case 0xd7:v=9;break; case 0xdb:v=10;break; case 0xdd:v=11;break; case 0xde:v=12;break; default:v=0;break; } } } P1=0xef; if(P1!=0xef) { delay(10); if(P1!=0xef) { key=P1; while(P1!=0xef) {} didi(10); switch(key) { case 0xe7:v=13;break; case 0xeb:v=14;break; case 0xed:v=15;break; case 0xee:v=16;break; default:v=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=123;j>0;j--); }
4、正反检测程序
#include <REGX51.H> #define uchar unsigned char #define uint unsigned int void delay(uint ms); void display(uchar shu,uchar wei); void didi(uint di); sbit bz=P1^5; uchar tab[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0x40}; void main() { uchar v,key; while(1) { display(v/10%10,5); display(v/1%10,4); P1=0xf0; if(P1!=0xf0) { delay(10); if(P1!=0xf0) { key=P1; P1=0x0f; key=key+P1; while(P1!=0x0f) {} didi(10); switch(key) { case 0x77:v=1;break; case 0x7b:v=2;break; case 0x7d:v=3;break; case 0x7e:v=4;break; case 0xb7:v=5;break; case 0xbb:v=6;break; case 0xbd:v=7;break; case 0xbe:v=8;break; case 0xd7:v=9;break; case 0xdb:v=10;break; case 0xdd:v=11;break; case 0xde:v=12;break; case 0xe7:v=13;break; case 0xeb:v=14;break; case 0xed:v=15;break; case 0xee:v=16;break; default:v=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=123;j>0;j--); }
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |