硬件要求:
矩阵按键连接至P3口;
数码管段选和位选各用1个锁存器;
段选连P2^6,位选连P2^7,用P0口控制。
C程序:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar num,temp;
void delay(uint z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<117;y++);
}void keyscan();
void xs(uchar num1);
void init();
void main()
{
init();
while(1)
{
keyscan();
xs(num);
}
}
void init()
{
num=17;
dula=1;
P0=0;
dula=0;
wela=1;
P0=0xc0;
wela=0;
}
void keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:num=1;
break;
case 0xd0:num=2;
break;
case 0xb0:num=3;
break;
case 0x70:num=4;
break;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:num=5;
break;
case 0xd0:num=6;
break;
case 0xb0:num=7;
break;
case 0x70:num=8;
break;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:num=9;
break;
case 0xd0:num=10;
break;
case 0xb0:num=11;
break;
case 0x70:num=12;
break;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:num=13;
break;
case 0xd0:num=14;
break;
case 0xb0:num=15;
break;
case 0x70:num=16;
break;
}
}
}
}
void xs(uchar num1)
{
dula=1;
P0=table[num1-1];
dula=0;
} |