数码管电路图:
Pasted Graphic.jpg
4*4矩阵式键盘电路原理图:
Pasted Graphic.jpg
代码如下:
#include "reg52.h"
sbit oe = P1^3;
sbit le1 = P1^4;//传值
sbit le2 = P1^5;//片选
char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴极的code
char code select[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//延时函数
void delay(unsigned int ms){
unsigned int x,y;
for(x=0;x<ms;x++)
for(y=0;y<110;y++);
}
//数码管显示函数(传入uint的数字,在八位数码管显示相应的数字)
void display(unsigned int num){
oe = 0;
le1 = 0;
le2 = 0;
le1 = 1;
P0 = table[num];
le1 = 0;
le2 = 1;
P0 = select[0];
le2 = 0;
delay(5);
}
//按键检测函数(按下相应按键显示不同数字)
void button_check(){
unsigned int l,h,hex;
P2 = 0xF0;//列输入,行输出
delay(2);
h = P2;
delay(2);
P2 = 0x0F;/行输入,列输出
l = P2;
delay(2);
hex = l|h;//两个值相或的交点
delay(2);
switch(hex){
case 0xEE://第一个E标识列,第二个E表示行
display(0);
break;
case 0xDE:
display(1);
break;
case 0xBE:
display(2);
break;
case 0x7E:
display(3);
break;
case 0xED:
display(4);
break;
case 0xDD:
display(5);
break;
case 0xBD:
display(6);
break;
case 0x7D:
display(7);
break;
case 0xEB:
display(8);
break;
case 0xDB:
display(9);
break;
case 0xBB:
display(10);
break;
case 0x7B:
display(11);
break;
case 0xE7:
display(12);
break;
case 0xD7:
display(13);
break;
case 0xB7:
display(14);
break;
case 0x77:
display(15);
break;
}
}
void main(){
while(1){
button_check();
delay(5);
}
}
网友评论