使用4*4的矩阵键盘和一个数码管
在4*4的矩阵键盘上编码0-F的字符,和一个数码管显示每个键盘上输入的字符。
//4*4键盘检测程序,按下键后相应的代码显示在数码管上#include<reg51.h>typedef unsigned char UINT8;UINT8 i=100;UINT8 j,k,temp,key;void delay(unsigned char i){ for(j=i;j>0;j--) for(k=125;k>0;k--);}UINT8 code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};void display(unsigned char num){ P0=table[num];}void init_led(){ P0 = 0x00;} void main(){ while(1) { P1 = 0xef; temp = P1; temp = temp&0xf0; if (temp != 0xf0) { temp = P1; switch(temp) { case 0xee: key=0; break; case 0xed: key=1; break; case 0xeb: key=2; break; case 0xe7: key=3; break; default: break; } display(key); P1=0xfe; } P1 = 0xdf; temp = P1; temp = temp&0x0f; if(temp != 0x0f) { temp = P1; switch(temp) { case 0xde: key = 4; break; case 0xdd: key = 5; break; case 0xdb: key = 6; break; case 0xd7: key = 7; break; default: break; } display(key); } P1=0xbf; temp=P1; temp=temp&0x0f; if(temp != 0x0f) { temp=P1; switch(temp) { case 0xbe: key=8; break; case 0xbd: key=9; break; case 0xbb: key=10; break; case 0xb7: key=11; break; default: break; } display(key); } P1=0x7f; temp=P1; temp=temp&0x0f; if(temp!=0x0f) { temp=P1; switch(temp) { case 0x7e: key=12; break; case 0x7d: key=13; break; case 0x7b: key=14; break; case 0x77: key=15; break; default: break; } display(key); } } }这个上面有一个bug就是每进入的时候数码管不是空的而是现实“0”这个没有解决。